package vertx.mon;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.mysqlclient.MySQLBuilder;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.SqlClient;
import io.vertx.sqlclient.Tuple;
import io.vertx.tracing.zipkin.HttpSenderOptions;
import io.vertx.tracing.zipkin.ZipkinTracingOptions;
public class API01 {
public static void main(String[] args) {
String senderEndpoint = "http://172.18.240.73:9411/api/v2/spans";
Vertx vertx = Vertx.vertx(new VertxOptions().setTracingOptions(new ZipkinTracingOptions()
.setSenderOptions(new HttpSenderOptions().setSenderEndpoint(senderEndpoint)).setServiceName("API01")));
MySQLConnectOptions connectOptions = new MySQLConnectOptions()
.setHost("127.0.0.1").setPort(3306)
.setUser("root").setPassword("Passw0rd")
.setDatabase("hr").setConnectTimeout(2000)
.addProperty("autoReconnect", "true")
.addProperty("useSSL","false")
.addProperty("rewriteBatchedStatements", "true");
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
SqlClient client = MySQLBuilder.client().using(vertx)
.with(poolOptions)
.connectingTo(connectOptions)
.build();
HttpServer server = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route(HttpMethod.GET, "/api/v2/api01/:empNo").handler(routingContext ->{
String en = routingContext.pathParam("empNo");
int empNo = 0;
try {
empNo = Integer.parseInt(en);
} catch(Exception e) {}
String sqlText = "select empno, ename, job from emp where empno = ?";
client.preparedQuery(sqlText).execute(Tuple.of(empNo)).onSuccess(rows -> {
JsonArray result = new JsonArray();
for (Row row : rows) {
JsonObject json = row.toJson();
result.add(json);
}
HttpServerResponse response = routingContext.response();
response.putHeader("content-type", "application/json");
response.end(result.toString());
}).onFailure(exception -> {
routingContext.fail(exception);
});
});
server.requestHandler(router).listen(8001);
}
}