use of io.vertx.rxjava.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class Transaction method start.
@Override
public void start() throws Exception {
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
String sql = "CREATE TABLE colors (" + "id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, " + "name VARCHAR(255), " + "datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)";
JDBCClient client = JDBCClient.createShared(vertx, config);
// Connect to the database
client.rxGetConnection().flatMap(conn -> conn.rxSetAutoCommit(false).flatMap(autoCommit -> conn.rxExecute(sql)).flatMap(executed -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE"))).flatMap(updateResult -> conn.rxCommit().map(commit -> updateResult)).onErrorResumeNext(ex -> conn.rxRollback().onErrorResumeNext(ex2 -> Single.error(new CompositeException(ex, ex2))).flatMap(ignore -> Single.error(ex))).flatMap(updateResult -> conn.rxQuery("SELECT * FROM colors")).doAfterTerminate(conn::close)).subscribe(resultSet -> {
// Subscribe to get the final result
System.out.println("Results : " + resultSet.getRows());
}, Throwable::printStackTrace);
}
use of io.vertx.rxjava.ext.jdbc.JDBCClient in project vertx-openshift-it by cescoffier.
the class GatewayVerticle method start.
@Override
public void start() throws Exception {
dnsClient = vertx.createHttpClient(new HttpClientOptions().setDefaultHost(ENDPOINT_NAME).setDefaultPort(8080).setKeepAlive(false));
dnsWeb = WebClient.create(vertx, new WebClientOptions().setDefaultHost(ENDPOINT_NAME).setDefaultPort(8080).setKeepAlive(false));
dnsDatabase = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:postgresql://my-database:5432/my_data").put("driver_class", "org.postgresql.Driver").put("user", "luke").put("password", "secret"));
Router router = Router.router(vertx);
router.get("/health").handler(rc -> rc.response().end("OK"));
router.get("/services/http").handler(this::invokeHttpService);
router.get("/services/web").handler(this::invokeWebService);
router.get("/services/db").handler(this::checkDb);
router.get("/dns/http").handler(this::invokeHttpServiceWithDns);
router.get("/dns/web").handler(this::invokeWebServiceWithDns);
router.get("/dns/db").handler(this::checkDbWithDns);
router.get("/ref/http").handler(this::invokeHttpServiceWithRef);
router.get("/ref/web").handler(this::invokeWebServiceWithRef);
router.put("/webclient").handler(this::webclient);
ServiceDiscovery.create(vertx, discovery -> {
this.discovery = discovery;
Single<WebClient> svc1 = HttpEndpoint.rxGetWebClient(discovery, svc -> svc.getName().equals(ENDPOINT_NAME), new JsonObject().put("keepAlive", false));
Single<HttpClient> svc2 = HttpEndpoint.rxGetClient(discovery, svc -> svc.getName().equals(ENDPOINT_NAME), new JsonObject().put("keepAlive", false));
Single<JDBCClient> svc3 = JDBCDataSource.rxGetJDBCClient(discovery, svc -> svc.getName().equals("my-database"), new JsonObject().put("url", "jdbc:postgresql://my-database:5432/my_data").put("driver_class", "org.postgresql.Driver").put("user", "luke").put("password", "secret"));
Single.zip(svc1, svc2, svc3, (wc, hc, db) -> {
this.web = wc;
this.client = hc;
this.database = db;
return vertx.createHttpServer().requestHandler(router::accept).listen(8080);
}).subscribe();
});
}
use of io.vertx.rxjava.ext.jdbc.JDBCClient in project vertx-openshift-it by cescoffier.
the class MySQLVerticle method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.route("/api/vegetables/:id").handler(this::validateId);
router.get("/api/vegetables").handler(this::getAll);
router.post("/api/vegetables").handler(this::addOne);
router.get("/api/vegetables/:id").handler(this::getOne);
router.put("/api/vegetables/:id").handler(this::updateOne);
router.delete("/api/vegetables/:id").handler(this::deleteOne);
router.get("/healthcheck").handler(rc -> rc.response().end("OK"));
JsonObject config = TestUtils.allocateDatabase("mysql", isExternalDB);
JDBCClient jdbcClient = JDBCClient.createShared(vertx, config);
initDatabase(vertx, jdbcClient).andThen(initHttpServer(router, jdbcClient)).subscribe((http) -> System.out.println("Server ready on port " + http.actualPort()), Throwable::printStackTrace);
}
use of io.vertx.rxjava.ext.jdbc.JDBCClient in project vertx-openshift-it by cescoffier.
the class PostgreSQLVerticle method start.
@Override
public void start() throws Exception {
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.route("/api/vegetables/:id").handler(this::validateId);
router.get("/api/vegetables").handler(this::getAll);
router.post("/api/vegetables").handler(this::addOne);
router.get("/api/vegetables/:id").handler(this::getOne);
router.put("/api/vegetables/:id").handler(this::updateOne);
router.delete("/api/vegetables/:id").handler(this::deleteOne);
router.get("/healthcheck").handler(rc -> rc.response().end("OK"));
JsonObject config = TestUtils.allocateDatabase("postgresql", isExternalDB);
JDBCClient jdbcClient = JDBCClient.createShared(vertx, config);
initDatabase(vertx, jdbcClient).andThen(initHttpServer(router, jdbcClient)).subscribe((http) -> System.out.println("Server ready on port " + http.actualPort()), Throwable::printStackTrace);
}
use of io.vertx.rxjava.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
JDBCClient jdbc = JDBCClient.createShared(vertx, config);
// Connect to the database
jdbc.rxGetConnection().flatMap(conn -> {
// Now chain some statements using flatmap composition
Single<ResultSet> resa = conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))").flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")).flatMap(result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")).flatMap(result -> conn.rxQuery("SELECT * FROM test"));
return resa.doAfterTerminate(conn::close);
}).subscribe(resultSet -> {
// Subscribe to the final result
System.out.println("Results : " + resultSet.getRows());
}, err -> {
System.out.println("Database problem");
err.printStackTrace();
});
}
Aggregations