-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New builder for building pool and clients
- Loading branch information
Showing
84 changed files
with
1,434 additions
and
507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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 -> { | ||
|
@@ -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) | ||
|
@@ -109,7 +119,7 @@ public void configureFromUri(Vertx vertx) { | |
}); | ||
} | ||
|
||
public void connecting01() { | ||
public void connecting01(Vertx vertx) { | ||
|
||
// Connect options | ||
DB2ConnectOptions connectOptions = new DB2ConnectOptions() | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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 -> { | ||
|
@@ -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(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
vertx-db2-client/src/main/java/io/vertx/db2client/DB2Builder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.