Skip to content

Commit

Permalink
New builder for building pool and clients
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Sep 22, 2023
1 parent 43c46a2 commit f844aa8
Show file tree
Hide file tree
Showing 84 changed files with 1,434 additions and 507 deletions.
2 changes: 1 addition & 1 deletion vertx-db2-client/src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Once you are done with the connection you must close it to release it to the poo

== Pool versus pooled client

The {@link io.vertx.db2client.DB2Pool} allows you to create a pool or a pooled client
The {@link io.vertx.db2client.DB2Builder} allows you to create a pool or a pooled client

[source,$lang]
----
Expand Down
50 changes: 40 additions & 10 deletions vertx-db2-client/src/main/java/examples/DB2ClientExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import io.vertx.core.Vertx;
import io.vertx.core.net.ClientSSLOptions;
import io.vertx.core.net.JksOptions;
import io.vertx.db2client.DB2Builder;
import io.vertx.db2client.DB2ConnectOptions;
import io.vertx.db2client.DB2Connection;
import io.vertx.db2client.DB2Pool;
import io.vertx.docgen.Source;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
Expand Down Expand Up @@ -53,7 +53,10 @@ public void gettingStarted() {
.setMaxSize(5);

// Create the client pool
DB2Pool client = DB2Pool.pool(connectOptions, poolOptions);
Pool client = DB2Builder.pool()
.with(poolOptions)
.connectingTo(connectOptions)
.build();

// A simple query
client
Expand Down Expand Up @@ -86,7 +89,11 @@ public void configureFromDataObject(Vertx vertx) {
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);

// Create the pool from the data object
DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions);
Pool pool = DB2Builder.pool()
.with(poolOptions)
.connectingTo(connectOptions)
.using(vertx)
.build();

pool.getConnection()
.onComplete(ar -> {
Expand All @@ -100,7 +107,10 @@ public void configureFromUri(Vertx vertx) {
String connectionUri = "db2://dbuser:[email protected]:50000/mydb";

// Create the pool from the connection URI
DB2Pool pool = DB2Pool.pool(connectionUri);
Pool pool = DB2Builder.pool()
.connectingTo(connectionUri)
.using(vertx)
.build();

// Create the connection from the connection URI
DB2Connection.connect(vertx, connectionUri)
Expand All @@ -109,7 +119,7 @@ public void configureFromUri(Vertx vertx) {
});
}

public void connecting01() {
public void connecting01(Vertx vertx) {

// Connect options
DB2ConnectOptions connectOptions = new DB2ConnectOptions()
Expand All @@ -124,7 +134,11 @@ public void connecting01() {
.setMaxSize(5);

// Create the pooled client
SqlClient client = DB2Pool.client(connectOptions, poolOptions);
SqlClient client = DB2Builder.client()
.with(poolOptions)
.connectingTo(connectOptions)
.using(vertx)
.build();
}

public void connecting02(Vertx vertx) {
Expand All @@ -141,7 +155,11 @@ public void connecting02(Vertx vertx) {
PoolOptions poolOptions = new PoolOptions()
.setMaxSize(5);
// Create the pooled client
SqlClient client = DB2Pool.client(vertx, connectOptions, poolOptions);
SqlClient client = DB2Builder.client()
.with(poolOptions)
.connectingTo(connectOptions)
.using(vertx)
.build();
}

public void connecting03(SqlClient client) {
Expand All @@ -165,7 +183,11 @@ public void connecting04(Vertx vertx) {
.setMaxSize(5);

// Create the pooled client
DB2Pool client = DB2Pool.pool(vertx, connectOptions, poolOptions);
Pool client = DB2Builder.pool()
.with(poolOptions)
.connectingTo(connectOptions)
.using(vertx)
.build();

// Get a connection from the pool
client.getConnection().compose(conn -> {
Expand Down Expand Up @@ -195,13 +217,21 @@ public void connecting04(Vertx vertx) {
public void poolVersusPooledClient(Vertx vertx, String sql, DB2ConnectOptions connectOptions, PoolOptions poolOptions) {

// Pooled client
SqlClient client = DB2Pool.client(vertx, connectOptions, poolOptions);
SqlClient client = DB2Builder.client()
.with(poolOptions)
.connectingTo(connectOptions)
.using(vertx)
.build();

// Pipelined
Future<RowSet<Row>> res1 = client.query(sql).execute();

// Connection pool
DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions);
Pool pool = DB2Builder.pool()
.with(poolOptions)
.connectingTo(connectOptions)
.using(vertx)
.build();

// Not pipelined
Future<RowSet<Row>> res2 = pool.query(sql).execute();
Expand Down
64 changes: 41 additions & 23 deletions vertx-db2-client/src/main/java/examples/SqlClientExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.db2client.DB2Builder;
import io.vertx.db2client.DB2ConnectOptions;
import io.vertx.db2client.DB2Pool;
import io.vertx.db2client.spi.DB2Driver;
import io.vertx.docgen.Source;
import io.vertx.sqlclient.*;
import io.vertx.sqlclient.spi.ConnectionFactory;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -360,12 +358,16 @@ public void tracing01(DB2ConnectOptions options) {
options.setTracingPolicy(TracingPolicy.ALWAYS);
}

public void poolConfig01(DB2ConnectOptions server1, DB2ConnectOptions server2, DB2ConnectOptions server3, PoolOptions options) {
DB2Pool pool = DB2Pool.pool(Arrays.asList(server1, server2, server3), options);
public void poolConfig01(Vertx vertx, DB2ConnectOptions server1, DB2ConnectOptions server2, DB2ConnectOptions server3, PoolOptions options) {
Pool pool = DB2Builder.pool()
.with(options)
.connectingTo(Arrays.asList(server1, server2, server3))
.using(vertx)
.build();
}

public void poolConfig02(DB2Pool pool, String sql) {
pool.connectHandler(conn -> {
public void poolConfig02(ClientBuilder<?> builder, String sql) {
builder.withConnectHandler(conn -> {
conn.query(sql).execute().onSuccess(res -> {
// Release the connection to the pool, ready to be used by the application
conn.close();
Expand All @@ -374,7 +376,11 @@ public void poolConfig02(DB2Pool pool, String sql) {
}

public void poolSharing1(Vertx vertx, DB2ConnectOptions database, int maxSize) {
DB2Pool pool = DB2Pool.pool(database, new PoolOptions().setMaxSize(maxSize));
Pool pool = DB2Builder.pool()
.with(new PoolOptions().setMaxSize(maxSize))
.connectingTo(database)
.using(vertx)
.build();
vertx.deployVerticle(() -> new AbstractVerticle() {
@Override
public void start() throws Exception {
Expand All @@ -385,36 +391,48 @@ public void start() throws Exception {

public void poolSharing2(Vertx vertx, DB2ConnectOptions database, int maxSize) {
vertx.deployVerticle(() -> new AbstractVerticle() {
DB2Pool pool;
Pool pool;
@Override
public void start() {
// Get or create a shared pool
// this actually creates a lease to the pool
// when the verticle is undeployed, the lease will be released automaticaly
pool = DB2Pool.pool(database, new PoolOptions()
.setMaxSize(maxSize)
.setShared(true)
.setName("my-pool"));
pool = DB2Builder.pool()
.with(new PoolOptions()
.setMaxSize(maxSize)
.setShared(true)
.setName("my-pool"))
.connectingTo(database)
.using(vertx)
.build();
}
}, new DeploymentOptions().setInstances(4));
}

public static void poolSharing3(Vertx vertx, DB2ConnectOptions database, int maxSize) {
DB2Pool pool = DB2Pool.pool(database, new PoolOptions()
.setMaxSize(maxSize)
.setShared(true)
.setName("my-pool")
.setEventLoopSize(4));
Pool pool = DB2Builder.pool()
.with(new PoolOptions()
.setMaxSize(maxSize)
.setShared(true)
.setName("my-pool")
.setEventLoopSize(4))
.connectingTo(database)
.using(vertx)
.build();
}

public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) {
DB2Pool pool = DB2Pool.pool(vertx, () -> {
Future<DB2ConnectOptions> connectOptions = retrieveOptions();
return connectOptions;
}, poolOptions);
Pool pool = DB2Builder.pool()
.with(poolOptions)
.connectingTo(() -> {
Future<SqlConnectOptions> connectOptions = retrieveOptions();
return connectOptions;
})
.using(vertx)
.build();
}

private Future<DB2ConnectOptions> retrieveOptions() {
private Future<SqlConnectOptions> retrieveOptions() {
return null;
}
}
94 changes: 94 additions & 0 deletions vertx-db2-client/src/main/java/io/vertx/db2client/DB2Builder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2017 Julien Viet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package io.vertx.db2client;

import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.net.NetClientOptions;
import io.vertx.db2client.impl.Db2PoolOptions;
import io.vertx.db2client.spi.DB2Driver;
import io.vertx.sqlclient.*;
import io.vertx.sqlclient.impl.ClientBuilderBase;

import java.util.function.Supplier;

/**
* Entry point for building DB2 clients.
*/
@VertxGen
public interface DB2Builder {

/**
* Build a pool with the specified {@code block} argument.
* The {@code block} argument is usually a lambda that configures the provided builder
* <p>
* Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));}
*
* @return the pool as configured by the code {@code block}
*/
static Pool pool(Handler<ClientBuilder<Pool>> block) {
return ClientBuilder.pool(DB2Driver.INSTANCE, block);
}

/**
* Provide a builder for DB2 pool of connections
* <p>
* Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()}
*/
static ClientBuilder<Pool> pool() {
return ClientBuilder.pool(DB2Driver.INSTANCE);
}

/**
* Build a client backed by a connection pool with the specified {@code block} argument.
* The {@code block} argument is usually a lambda that configures the provided builder
* <p>
* Example usage: {@code SqlClient client = PgBuilder.client(builder -> builder.connectingTo(connectOptions));}
*
* @return the client as configured by the code {@code block}
*/
static SqlClient client(Handler<ClientBuilder<SqlClient>> handler) {
ClientBuilder<SqlClient> builder = client();
handler.handle(builder);
return builder.build();
}

/**
* Provide a builder for DB2 client backed by a connection pool.
* <p>
* Example usage: {@code SqlClient client = PgBuilder.client().connectingTo(connectOptions).build()}
*/
static ClientBuilder<SqlClient> client() {
return new ClientBuilderBase<SqlClient>(DB2Driver.INSTANCE) {
@Override
public ClientBuilder<SqlClient> with(PoolOptions options) {
if (options != null) {
options = new Db2PoolOptions(options).setPipelined(true);
}
return super.with(options);
}

@Override
protected SqlClient create(Vertx vertx, Supplier<Future<SqlConnectOptions>> databases, PoolOptions poolOptions, NetClientOptions transportOptions) {
return driver.createPool(vertx, databases, poolOptions, transportOptions);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

import io.vertx.codegen.annotations.DataObject;
Expand All @@ -33,7 +32,7 @@
import io.vertx.sqlclient.SqlConnectOptions;

/**
* Connect options for configuring {@link DB2Connection} or {@link DB2Pool}.
* Connect options for configuring {@link DB2Connection} or {@link DB2Builder}.
*/
@DataObject(generateConverter = true)
public class DB2ConnectOptions extends SqlConnectOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.net.NetClientOptions;
import io.vertx.db2client.impl.Db2PoolOptions;
import io.vertx.db2client.spi.DB2Driver;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlClient;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.impl.Utils;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -107,7 +109,7 @@ static DB2Pool pool(List<DB2ConnectOptions> databases, PoolOptions options) {
* {@link Vertx} instance.
*/
static DB2Pool pool(Vertx vertx, List<DB2ConnectOptions> databases, PoolOptions options) {
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, options);
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options, new NetClientOptions());
}

/**
Expand All @@ -127,7 +129,7 @@ static DB2Pool pool(Supplier<Future<DB2ConnectOptions>> databases, PoolOptions p
* Like {@link #pool(Supplier, PoolOptions)} with a specific {@link Vertx} instance.
*/
static DB2Pool pool(Vertx vertx, Supplier<Future<DB2ConnectOptions>> databases, PoolOptions poolOptions) {
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, poolOptions);
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, poolOptions, new NetClientOptions());
}

/**
Expand Down Expand Up @@ -196,7 +198,7 @@ static SqlClient client(List<DB2ConnectOptions> databases, PoolOptions options)
* {@link Vertx} instance.
*/
static SqlClient client(Vertx vertx, List<DB2ConnectOptions> databases, PoolOptions options) {
return DB2Driver.INSTANCE.createPool(vertx, databases, new Db2PoolOptions(options).setPipelined(true));
return DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), new Db2PoolOptions(options).setPipelined(true), new NetClientOptions());
}

@Override
Expand Down
Loading

0 comments on commit f844aa8

Please sign in to comment.