Skip to content

Commit f9e17ed

Browse files
committed
New builder for building pool and clients
1 parent db3d74e commit f9e17ed

File tree

67 files changed

+1200
-304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1200
-304
lines changed

vertx-db2-client/src/main/asciidoc/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Once you are done with the connection you must close it to release it to the poo
9999

100100
== Pool versus pooled client
101101

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

104104
[source,$lang]
105105
----

vertx-db2-client/src/main/java/examples/DB2ClientExamples.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import io.vertx.core.Future;
2323
import io.vertx.core.Vertx;
2424
import io.vertx.core.net.JksOptions;
25+
import io.vertx.db2client.DB2Builder;
2526
import io.vertx.db2client.DB2ConnectOptions;
2627
import io.vertx.db2client.DB2Connection;
27-
import io.vertx.db2client.DB2Pool;
2828
import io.vertx.docgen.Source;
2929
import io.vertx.sqlclient.Pool;
3030
import io.vertx.sqlclient.PoolOptions;
@@ -52,7 +52,10 @@ public void gettingStarted() {
5252
.setMaxSize(5);
5353

5454
// Create the client pool
55-
DB2Pool client = DB2Pool.pool(connectOptions, poolOptions);
55+
Pool client = DB2Builder.pool()
56+
.with(poolOptions)
57+
.connectingTo(connectOptions)
58+
.build();
5659

5760
// A simple query
5861
client
@@ -85,7 +88,11 @@ public void configureFromDataObject(Vertx vertx) {
8588
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
8689

8790
// Create the pool from the data object
88-
DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions);
91+
Pool pool = DB2Builder.pool()
92+
.with(poolOptions)
93+
.connectingTo(connectOptions)
94+
.using(vertx)
95+
.build();
8996

9097
pool.getConnection()
9198
.onComplete(ar -> {
@@ -99,7 +106,10 @@ public void configureFromUri(Vertx vertx) {
99106
String connectionUri = "db2://dbuser:[email protected]:50000/mydb";
100107

101108
// Create the pool from the connection URI
102-
DB2Pool pool = DB2Pool.pool(connectionUri);
109+
Pool pool = DB2Builder.pool()
110+
.connectingTo(connectionUri)
111+
.using(vertx)
112+
.build();
103113

104114
// Create the connection from the connection URI
105115
DB2Connection.connect(vertx, connectionUri)
@@ -108,7 +118,7 @@ public void configureFromUri(Vertx vertx) {
108118
});
109119
}
110120

111-
public void connecting01() {
121+
public void connecting01(Vertx vertx) {
112122

113123
// Connect options
114124
DB2ConnectOptions connectOptions = new DB2ConnectOptions()
@@ -123,7 +133,11 @@ public void connecting01() {
123133
.setMaxSize(5);
124134

125135
// Create the pooled client
126-
SqlClient client = DB2Pool.client(connectOptions, poolOptions);
136+
SqlClient client = DB2Builder.client()
137+
.with(poolOptions)
138+
.connectingTo(connectOptions)
139+
.using(vertx)
140+
.build();
127141
}
128142

129143
public void connecting02(Vertx vertx) {
@@ -140,7 +154,11 @@ public void connecting02(Vertx vertx) {
140154
PoolOptions poolOptions = new PoolOptions()
141155
.setMaxSize(5);
142156
// Create the pooled client
143-
SqlClient client = DB2Pool.client(vertx, connectOptions, poolOptions);
157+
SqlClient client = DB2Builder.client()
158+
.with(poolOptions)
159+
.connectingTo(connectOptions)
160+
.using(vertx)
161+
.build();
144162
}
145163

146164
public void connecting03(SqlClient client) {
@@ -164,7 +182,11 @@ public void connecting04(Vertx vertx) {
164182
.setMaxSize(5);
165183

166184
// Create the pooled client
167-
DB2Pool client = DB2Pool.pool(vertx, connectOptions, poolOptions);
185+
Pool client = DB2Builder.pool()
186+
.with(poolOptions)
187+
.connectingTo(connectOptions)
188+
.using(vertx)
189+
.build();
168190

169191
// Get a connection from the pool
170192
client.getConnection().compose(conn -> {
@@ -194,13 +216,21 @@ public void connecting04(Vertx vertx) {
194216
public void poolVersusPooledClient(Vertx vertx, String sql, DB2ConnectOptions connectOptions, PoolOptions poolOptions) {
195217

196218
// Pooled client
197-
SqlClient client = DB2Pool.client(vertx, connectOptions, poolOptions);
219+
SqlClient client = DB2Builder.client()
220+
.with(poolOptions)
221+
.connectingTo(connectOptions)
222+
.using(vertx)
223+
.build();
198224

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

202228
// Connection pool
203-
DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions);
229+
Pool pool = DB2Builder.pool()
230+
.with(poolOptions)
231+
.connectingTo(connectOptions)
232+
.using(vertx)
233+
.build();
204234

205235
// Not pipelined
206236
Future<RowSet<Row>> res2 = pool.query(sql).execute();

vertx-db2-client/src/main/java/examples/SqlClientExamples.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020
import io.vertx.core.Future;
2121
import io.vertx.core.Vertx;
2222
import io.vertx.core.tracing.TracingPolicy;
23+
import io.vertx.db2client.DB2Builder;
2324
import io.vertx.db2client.DB2ConnectOptions;
24-
import io.vertx.db2client.DB2Pool;
25-
import io.vertx.db2client.spi.DB2Driver;
2625
import io.vertx.docgen.Source;
2726
import io.vertx.sqlclient.*;
28-
import io.vertx.sqlclient.spi.ConnectionFactory;
2927

3028
import java.util.ArrayList;
3129
import java.util.Arrays;
@@ -360,12 +358,16 @@ public void tracing01(DB2ConnectOptions options) {
360358
options.setTracingPolicy(TracingPolicy.ALWAYS);
361359
}
362360

363-
public void poolConfig01(DB2ConnectOptions server1, DB2ConnectOptions server2, DB2ConnectOptions server3, PoolOptions options) {
364-
DB2Pool pool = DB2Pool.pool(Arrays.asList(server1, server2, server3), options);
361+
public void poolConfig01(Vertx vertx, DB2ConnectOptions server1, DB2ConnectOptions server2, DB2ConnectOptions server3, PoolOptions options) {
362+
Pool pool = DB2Builder.pool()
363+
.with(options)
364+
.connectingTo(Arrays.asList(server1, server2, server3))
365+
.using(vertx)
366+
.build();
365367
}
366368

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

376378
public void poolSharing1(Vertx vertx, DB2ConnectOptions database, int maxSize) {
377-
DB2Pool pool = DB2Pool.pool(database, new PoolOptions().setMaxSize(maxSize));
379+
Pool pool = DB2Builder.pool()
380+
.with(new PoolOptions().setMaxSize(maxSize))
381+
.connectingTo(database)
382+
.using(vertx)
383+
.build();
378384
vertx.deployVerticle(() -> new AbstractVerticle() {
379385
@Override
380386
public void start() throws Exception {
@@ -385,36 +391,48 @@ public void start() throws Exception {
385391

386392
public void poolSharing2(Vertx vertx, DB2ConnectOptions database, int maxSize) {
387393
vertx.deployVerticle(() -> new AbstractVerticle() {
388-
DB2Pool pool;
394+
Pool pool;
389395
@Override
390396
public void start() {
391397
// Get or create a shared pool
392398
// this actually creates a lease to the pool
393399
// when the verticle is undeployed, the lease will be released automaticaly
394-
pool = DB2Pool.pool(database, new PoolOptions()
395-
.setMaxSize(maxSize)
396-
.setShared(true)
397-
.setName("my-pool"));
400+
pool = DB2Builder.pool()
401+
.with(new PoolOptions()
402+
.setMaxSize(maxSize)
403+
.setShared(true)
404+
.setName("my-pool"))
405+
.connectingTo(database)
406+
.using(vertx)
407+
.build();
398408
}
399409
}, new DeploymentOptions().setInstances(4));
400410
}
401411

402412
public static void poolSharing3(Vertx vertx, DB2ConnectOptions database, int maxSize) {
403-
DB2Pool pool = DB2Pool.pool(database, new PoolOptions()
404-
.setMaxSize(maxSize)
405-
.setShared(true)
406-
.setName("my-pool")
407-
.setEventLoopSize(4));
413+
Pool pool = DB2Builder.pool()
414+
.with(new PoolOptions()
415+
.setMaxSize(maxSize)
416+
.setShared(true)
417+
.setName("my-pool")
418+
.setEventLoopSize(4))
419+
.connectingTo(database)
420+
.using(vertx)
421+
.build();
408422
}
409423

410424
public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) {
411-
DB2Pool pool = DB2Pool.pool(vertx, () -> {
412-
Future<DB2ConnectOptions> connectOptions = retrieveOptions();
413-
return connectOptions;
414-
}, poolOptions);
425+
Pool pool = DB2Builder.pool()
426+
.with(poolOptions)
427+
.connectingTo(() -> {
428+
Future<SqlConnectOptions> connectOptions = retrieveOptions();
429+
return connectOptions;
430+
})
431+
.using(vertx)
432+
.build();
415433
}
416434

417-
private Future<DB2ConnectOptions> retrieveOptions() {
435+
private Future<SqlConnectOptions> retrieveOptions() {
418436
return null;
419437
}
420438
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2017 Julien Viet
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package io.vertx.db2client;
19+
20+
import io.vertx.codegen.annotations.VertxGen;
21+
import io.vertx.core.Future;
22+
import io.vertx.core.Handler;
23+
import io.vertx.core.Vertx;
24+
import io.vertx.db2client.impl.Db2PoolOptions;
25+
import io.vertx.db2client.spi.DB2Driver;
26+
import io.vertx.sqlclient.*;
27+
import io.vertx.sqlclient.impl.ClientBuilderBase;
28+
29+
import java.util.function.Supplier;
30+
31+
/**
32+
* Entry point for building DB2 clients.
33+
*/
34+
@VertxGen
35+
public interface DB2Builder {
36+
37+
/**
38+
* Build a pool with the specified {@code block} argument.
39+
* The {@code block} argument is usually a lambda that configures the provided builder
40+
* <p>
41+
* Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));}
42+
*
43+
* @return the pool as configured by the code {@code block}
44+
*/
45+
static Pool pool(Handler<ClientBuilder<Pool>> block) {
46+
return ClientBuilder.pool(DB2Driver.INSTANCE, block);
47+
}
48+
49+
/**
50+
* Provide a builder for DB2 pool of connections
51+
* <p>
52+
* Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()}
53+
*/
54+
static ClientBuilder<Pool> pool() {
55+
return ClientBuilder.pool(DB2Driver.INSTANCE);
56+
}
57+
58+
/**
59+
* Build a client backed by a connection pool with the specified {@code block} argument.
60+
* The {@code block} argument is usually a lambda that configures the provided builder
61+
* <p>
62+
* Example usage: {@code SqlClient client = PgBuilder.client(builder -> builder.connectingTo(connectOptions));}
63+
*
64+
* @return the client as configured by the code {@code block}
65+
*/
66+
static SqlClient client(Handler<ClientBuilder<SqlClient>> handler) {
67+
ClientBuilder<SqlClient> builder = client();
68+
handler.handle(builder);
69+
return builder.build();
70+
}
71+
72+
/**
73+
* Provide a builder for DB2 client backed by a connection pool.
74+
* <p>
75+
* Example usage: {@code SqlClient client = PgBuilder.client().connectingTo(connectOptions).build()}
76+
*/
77+
static ClientBuilder<SqlClient> client() {
78+
return new ClientBuilderBase<SqlClient>(DB2Driver.INSTANCE) {
79+
@Override
80+
public ClientBuilder<SqlClient> with(PoolOptions options) {
81+
if (options != null) {
82+
options = new Db2PoolOptions(options).setPipelined(true);
83+
}
84+
return super.with(options);
85+
}
86+
87+
@Override
88+
protected SqlClient create(Vertx vertx, Supplier<Future<SqlConnectOptions>> databases, PoolOptions poolOptions) {
89+
return driver.createPool(vertx, databases, poolOptions);
90+
}
91+
};
92+
}
93+
}

vertx-db2-client/src/main/java/io/vertx/db2client/DB2ConnectOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import io.vertx.sqlclient.SqlConnectOptions;
4343

4444
/**
45-
* Connect options for configuring {@link DB2Connection} or {@link DB2Pool}.
45+
* Connect options for configuring {@link DB2Connection} or {@link DB2Builder}.
4646
*/
4747
@DataObject(generateConverter = true)
4848
public class DB2ConnectOptions extends SqlConnectOptions {

vertx-db2-client/src/main/java/io/vertx/db2client/DB2Pool.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import io.vertx.core.Future;
2323
import io.vertx.core.Handler;
2424
import io.vertx.core.Vertx;
25+
import io.vertx.core.net.NetClientOptions;
2526
import io.vertx.db2client.impl.Db2PoolOptions;
2627
import io.vertx.db2client.spi.DB2Driver;
2728
import io.vertx.sqlclient.Pool;
2829
import io.vertx.sqlclient.PoolOptions;
2930
import io.vertx.sqlclient.SqlClient;
3031
import io.vertx.sqlclient.SqlConnection;
32+
import io.vertx.sqlclient.impl.Utils;
3133

3234
import java.util.Collections;
3335
import java.util.List;
@@ -39,6 +41,7 @@
3941
/**
4042
* A pool of DB2 connections.
4143
*/
44+
@Deprecated
4245
@VertxGen
4346
public interface DB2Pool extends Pool {
4447

@@ -108,7 +111,7 @@ static DB2Pool pool(List<DB2ConnectOptions> databases, PoolOptions options) {
108111
* {@link Vertx} instance.
109112
*/
110113
static DB2Pool pool(Vertx vertx, List<DB2ConnectOptions> databases, PoolOptions options) {
111-
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, options);
114+
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options);
112115
}
113116

114117
/**
@@ -199,7 +202,7 @@ static SqlClient client(List<DB2ConnectOptions> databases, PoolOptions options)
199202
* {@link Vertx} instance.
200203
*/
201204
static SqlClient client(Vertx vertx, List<DB2ConnectOptions> databases, PoolOptions options) {
202-
return DB2Driver.INSTANCE.createPool(vertx, databases, new Db2PoolOptions(options).setPipelined(true));
205+
return DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), new Db2PoolOptions(options).setPipelined(true));
203206
}
204207

205208
@Override

0 commit comments

Comments
 (0)