Skip to content

Commit f844aa8

Browse files
committed
New builder for building pool and clients
1 parent 43c46a2 commit f844aa8

File tree

84 files changed

+1434
-507
lines changed

Some content is hidden

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

84 files changed

+1434
-507
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
@@ -23,9 +23,9 @@
2323
import io.vertx.core.Vertx;
2424
import io.vertx.core.net.ClientSSLOptions;
2525
import io.vertx.core.net.JksOptions;
26+
import io.vertx.db2client.DB2Builder;
2627
import io.vertx.db2client.DB2ConnectOptions;
2728
import io.vertx.db2client.DB2Connection;
28-
import io.vertx.db2client.DB2Pool;
2929
import io.vertx.docgen.Source;
3030
import io.vertx.sqlclient.Pool;
3131
import io.vertx.sqlclient.PoolOptions;
@@ -53,7 +53,10 @@ public void gettingStarted() {
5353
.setMaxSize(5);
5454

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

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

8891
// Create the pool from the data object
89-
DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions);
92+
Pool pool = DB2Builder.pool()
93+
.with(poolOptions)
94+
.connectingTo(connectOptions)
95+
.using(vertx)
96+
.build();
9097

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

102109
// Create the pool from the connection URI
103-
DB2Pool pool = DB2Pool.pool(connectionUri);
110+
Pool pool = DB2Builder.pool()
111+
.connectingTo(connectionUri)
112+
.using(vertx)
113+
.build();
104114

105115
// Create the connection from the connection URI
106116
DB2Connection.connect(vertx, connectionUri)
@@ -109,7 +119,7 @@ public void configureFromUri(Vertx vertx) {
109119
});
110120
}
111121

112-
public void connecting01() {
122+
public void connecting01(Vertx vertx) {
113123

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

126136
// Create the pooled client
127-
SqlClient client = DB2Pool.client(connectOptions, poolOptions);
137+
SqlClient client = DB2Builder.client()
138+
.with(poolOptions)
139+
.connectingTo(connectOptions)
140+
.using(vertx)
141+
.build();
128142
}
129143

130144
public void connecting02(Vertx vertx) {
@@ -141,7 +155,11 @@ public void connecting02(Vertx vertx) {
141155
PoolOptions poolOptions = new PoolOptions()
142156
.setMaxSize(5);
143157
// Create the pooled client
144-
SqlClient client = DB2Pool.client(vertx, connectOptions, poolOptions);
158+
SqlClient client = DB2Builder.client()
159+
.with(poolOptions)
160+
.connectingTo(connectOptions)
161+
.using(vertx)
162+
.build();
145163
}
146164

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

167185
// Create the pooled client
168-
DB2Pool client = DB2Pool.pool(vertx, connectOptions, poolOptions);
186+
Pool client = DB2Builder.pool()
187+
.with(poolOptions)
188+
.connectingTo(connectOptions)
189+
.using(vertx)
190+
.build();
169191

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

197219
// Pooled client
198-
SqlClient client = DB2Pool.client(vertx, connectOptions, poolOptions);
220+
SqlClient client = DB2Builder.client()
221+
.with(poolOptions)
222+
.connectingTo(connectOptions)
223+
.using(vertx)
224+
.build();
199225

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

203229
// Connection pool
204-
DB2Pool pool = DB2Pool.pool(vertx, connectOptions, poolOptions);
230+
Pool pool = DB2Builder.pool()
231+
.with(poolOptions)
232+
.connectingTo(connectOptions)
233+
.using(vertx)
234+
.build();
205235

206236
// Not pipelined
207237
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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.core.net.NetClientOptions;
25+
import io.vertx.db2client.impl.Db2PoolOptions;
26+
import io.vertx.db2client.spi.DB2Driver;
27+
import io.vertx.sqlclient.*;
28+
import io.vertx.sqlclient.impl.ClientBuilderBase;
29+
30+
import java.util.function.Supplier;
31+
32+
/**
33+
* Entry point for building DB2 clients.
34+
*/
35+
@VertxGen
36+
public interface DB2Builder {
37+
38+
/**
39+
* Build a pool with the specified {@code block} argument.
40+
* The {@code block} argument is usually a lambda that configures the provided builder
41+
* <p>
42+
* Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));}
43+
*
44+
* @return the pool as configured by the code {@code block}
45+
*/
46+
static Pool pool(Handler<ClientBuilder<Pool>> block) {
47+
return ClientBuilder.pool(DB2Driver.INSTANCE, block);
48+
}
49+
50+
/**
51+
* Provide a builder for DB2 pool of connections
52+
* <p>
53+
* Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()}
54+
*/
55+
static ClientBuilder<Pool> pool() {
56+
return ClientBuilder.pool(DB2Driver.INSTANCE);
57+
}
58+
59+
/**
60+
* Build a client backed by a connection pool with the specified {@code block} argument.
61+
* The {@code block} argument is usually a lambda that configures the provided builder
62+
* <p>
63+
* Example usage: {@code SqlClient client = PgBuilder.client(builder -> builder.connectingTo(connectOptions));}
64+
*
65+
* @return the client as configured by the code {@code block}
66+
*/
67+
static SqlClient client(Handler<ClientBuilder<SqlClient>> handler) {
68+
ClientBuilder<SqlClient> builder = client();
69+
handler.handle(builder);
70+
return builder.build();
71+
}
72+
73+
/**
74+
* Provide a builder for DB2 client backed by a connection pool.
75+
* <p>
76+
* Example usage: {@code SqlClient client = PgBuilder.client().connectingTo(connectOptions).build()}
77+
*/
78+
static ClientBuilder<SqlClient> client() {
79+
return new ClientBuilderBase<SqlClient>(DB2Driver.INSTANCE) {
80+
@Override
81+
public ClientBuilder<SqlClient> with(PoolOptions options) {
82+
if (options != null) {
83+
options = new Db2PoolOptions(options).setPipelined(true);
84+
}
85+
return super.with(options);
86+
}
87+
88+
@Override
89+
protected SqlClient create(Vertx vertx, Supplier<Future<SqlConnectOptions>> databases, PoolOptions poolOptions, NetClientOptions transportOptions) {
90+
return driver.createPool(vertx, databases, poolOptions, transportOptions);
91+
}
92+
};
93+
}
94+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121
import java.util.Objects;
22-
import java.util.concurrent.TimeUnit;
2322
import java.util.function.Predicate;
2423

2524
import io.vertx.codegen.annotations.DataObject;
@@ -33,7 +32,7 @@
3332
import io.vertx.sqlclient.SqlConnectOptions;
3433

3534
/**
36-
* Connect options for configuring {@link DB2Connection} or {@link DB2Pool}.
35+
* Connect options for configuring {@link DB2Connection} or {@link DB2Builder}.
3736
*/
3837
@DataObject(generateConverter = true)
3938
public class DB2ConnectOptions extends SqlConnectOptions {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
import io.vertx.core.Future;
2222
import io.vertx.core.Handler;
2323
import io.vertx.core.Vertx;
24+
import io.vertx.core.net.NetClientOptions;
2425
import io.vertx.db2client.impl.Db2PoolOptions;
2526
import io.vertx.db2client.spi.DB2Driver;
2627
import io.vertx.sqlclient.Pool;
2728
import io.vertx.sqlclient.PoolOptions;
2829
import io.vertx.sqlclient.SqlClient;
2930
import io.vertx.sqlclient.SqlConnection;
31+
import io.vertx.sqlclient.impl.Utils;
3032

3133
import java.util.Collections;
3234
import java.util.List;
@@ -107,7 +109,7 @@ static DB2Pool pool(List<DB2ConnectOptions> databases, PoolOptions options) {
107109
* {@link Vertx} instance.
108110
*/
109111
static DB2Pool pool(Vertx vertx, List<DB2ConnectOptions> databases, PoolOptions options) {
110-
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, databases, options);
112+
return (DB2Pool) DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), options, new NetClientOptions());
111113
}
112114

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

133135
/**
@@ -196,7 +198,7 @@ static SqlClient client(List<DB2ConnectOptions> databases, PoolOptions options)
196198
* {@link Vertx} instance.
197199
*/
198200
static SqlClient client(Vertx vertx, List<DB2ConnectOptions> databases, PoolOptions options) {
199-
return DB2Driver.INSTANCE.createPool(vertx, databases, new Db2PoolOptions(options).setPipelined(true));
201+
return DB2Driver.INSTANCE.createPool(vertx, Utils.roundRobinSupplier(databases), new Db2PoolOptions(options).setPipelined(true), new NetClientOptions());
200202
}
201203

202204
@Override

0 commit comments

Comments
 (0)