Search in sources :

Example 1 with JDBCClient

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);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Single(rx.Single) CompositeException(rx.exceptions.CompositeException) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) JsonArray(io.vertx.core.json.JsonArray) CompositeException(rx.exceptions.CompositeException) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Example 2 with JDBCClient

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();
    });
}
Also used : WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpResponse(io.vertx.rxjava.ext.web.client.HttpResponse) Router(io.vertx.rxjava.ext.web.Router) RoutingContext(io.vertx.rxjava.ext.web.RoutingContext) HttpEndpoint(io.vertx.rxjava.servicediscovery.types.HttpEndpoint) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) WebClient(io.vertx.rxjava.ext.web.client.WebClient) HttpClient(io.vertx.rxjava.core.http.HttpClient) Single(rx.Single) JDBCDataSource(io.vertx.rxjava.servicediscovery.types.JDBCDataSource) ServiceDiscovery(io.vertx.rxjava.servicediscovery.ServiceDiscovery) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) BodyCodec(io.vertx.rxjava.ext.web.codec.BodyCodec) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) HttpClient(io.vertx.rxjava.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.rxjava.ext.web.Router) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) WebClient(io.vertx.rxjava.ext.web.client.WebClient) HttpClientOptions(io.vertx.core.http.HttpClientOptions)

Example 3 with JDBCClient

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);
}
Also used : Router(io.vertx.rxjava.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Example 4 with JDBCClient

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);
}
Also used : Router(io.vertx.rxjava.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Example 5 with JDBCClient

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();
    });
}
Also used : Single(rx.Single) ResultSet(io.vertx.ext.sql.ResultSet) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) Single(rx.Single) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)7 JDBCClient (io.vertx.rxjava.ext.jdbc.JDBCClient)7 AbstractVerticle (io.vertx.rxjava.core.AbstractVerticle)4 Router (io.vertx.rxjava.ext.web.Router)4 Runner (io.vertx.example.util.Runner)3 Single (rx.Single)3 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 JsonArray (io.vertx.core.json.JsonArray)1 ResultSet (io.vertx.ext.sql.ResultSet)1 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)1 HttpClient (io.vertx.rxjava.core.http.HttpClient)1 RoutingContext (io.vertx.rxjava.ext.web.RoutingContext)1 HttpResponse (io.vertx.rxjava.ext.web.client.HttpResponse)1 WebClient (io.vertx.rxjava.ext.web.client.WebClient)1 BodyCodec (io.vertx.rxjava.ext.web.codec.BodyCodec)1 ServiceDiscovery (io.vertx.rxjava.servicediscovery.ServiceDiscovery)1 HttpEndpoint (io.vertx.rxjava.servicediscovery.types.HttpEndpoint)1 JDBCDataSource (io.vertx.rxjava.servicediscovery.types.JDBCDataSource)1 Completable (rx.Completable)1 CompositeException (rx.exceptions.CompositeException)1