Search in sources :

Example 6 with JDBCClient

use of io.vertx.rxjava.ext.jdbc.JDBCClient in project vertx-examples by vert-x3.

the class Streaming 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);
    jdbc.rxGetConnection().flatMapObservable(conn -> {
        // With the connection...
        return // ...create test table
        conn.rxUpdate("CREATE TABLE test(col VARCHAR(20))").flatMap(// ...insert a row
        result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val1')")).flatMap(// ...another one
        result -> conn.rxUpdate("INSERT INTO test (col) VALUES ('val2')")).flatMap(// ...get values stream
        result -> conn.rxQueryStream("SELECT * FROM test")).flatMapObservable(sqlRowStream -> {
            return // Transform the stream into an Observable...
            sqlRowStream.toObservable().doOnTerminate(// ...and close the connection when the stream is fully read or an error occurs
            conn::close);
        });
    }).subscribe(row -> System.out.println("Row : " + row.encode()));
}
Also used : 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) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Example 7 with JDBCClient

use of io.vertx.rxjava.ext.jdbc.JDBCClient in project vertx-openshift-it by cescoffier.

the class OracleVerticle 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("oracle", true);
    JDBCClient jdbcClient = JDBCClient.createShared(vertx, config);
    vegetableTableExists(jdbcClient).subscribe(VegetableTableExists -> {
        Completable firstAction = initDatabase(vertx, jdbcClient);
        if (VegetableTableExists) {
            firstAction = dropVegetableTable(jdbcClient).andThen(initDatabase(vertx, jdbcClient));
        }
        firstAction.andThen(initHttpServer(router, jdbcClient)).subscribe((http) -> System.out.println("Server ready on port " + http.actualPort()), Throwable::printStackTrace);
    });
}
Also used : Completable(rx.Completable) Router(io.vertx.rxjava.ext.web.Router) 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