Skip to content

Commit 9cbea11

Browse files
committed
Introduce a client builder to replace static pool methods.
1 parent 2a15ed6 commit 9cbea11

20 files changed

Lines changed: 397 additions & 115 deletions

vertx-pg-client/src/main/java/examples/PgClientExamples.java

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
import io.vertx.core.json.JsonObject;
2323
import io.vertx.core.net.PemTrustOptions;
2424
import io.vertx.docgen.Source;
25-
import io.vertx.pgclient.PgConnectOptions;
26-
import io.vertx.pgclient.PgConnection;
27-
import io.vertx.pgclient.PgPool;
28-
import io.vertx.pgclient.SslMode;
25+
import io.vertx.pgclient.*;
2926
import io.vertx.pgclient.pubsub.PgSubscriber;
3027
import io.vertx.sqlclient.*;
3128
import io.vertx.sqlclient.data.Numeric;
@@ -59,7 +56,7 @@ public void gettingStarted() {
5956
.setMaxSize(5);
6057

6158
// Create the client pool
62-
SqlClient client = PgPool.client(connectOptions, poolOptions);
59+
SqlClient client = PgBuilder.client().config(poolOptions).connectingTo(connectOptions).build();
6360

6461
// A simple query
6562
client
@@ -81,7 +78,7 @@ public void gettingStarted() {
8178
public void configureFromEnv(Vertx vertx) {
8279

8380
// Create the pool from the environment variables
84-
PgPool pool = PgPool.pool();
81+
Pool pool = PgBuilder.pool().build();
8582

8683
// Create the connection from the environment variables
8784
PgConnection.connect(vertx)
@@ -104,7 +101,7 @@ public void configureFromDataObject(Vertx vertx) {
104101
PoolOptions poolOptions = new PoolOptions().setMaxSize(5);
105102

106103
// Create the pool from the data object
107-
PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions);
104+
Pool pool = PgBuilder.pool().connectingTo(connectOptions).config(poolOptions).using(vertx).build();
108105

109106
pool.getConnection()
110107
.onComplete(ar -> {
@@ -128,7 +125,7 @@ public void configureFromUri(Vertx vertx) {
128125
String connectionUri = "postgresql://dbuser:secretpassword@database.server.com:5432/mydb";
129126

130127
// Create the pool from the connection URI
131-
PgPool pool = PgPool.pool(connectionUri);
128+
Pool pool = PgBuilder.pool().connectingTo(PgConnectOptions.fromUri(connectionUri)).build();
132129

133130
// Create the connection from the connection URI
134131
PgConnection
@@ -153,7 +150,7 @@ public void connecting01() {
153150
.setMaxSize(5);
154151

155152
// Create the pooled client
156-
SqlClient client = PgPool.client(connectOptions, poolOptions);
153+
SqlClient client = PgBuilder.client().config(poolOptions).connectingTo(connectOptions).build();
157154
}
158155

159156
public void connecting02(Vertx vertx) {
@@ -171,10 +168,10 @@ public void connecting02(Vertx vertx) {
171168
.setMaxSize(5);
172169

173170
// Create the pooled client
174-
SqlClient client = PgPool.client(vertx, connectOptions, poolOptions);
171+
SqlClient client = PgBuilder.client().config(poolOptions).connectingTo(connectOptions).using(vertx).build();
175172
}
176173

177-
public void connecting03(PgPool client) {
174+
public void connecting03(Pool client) {
178175

179176
// Close the pooled client and all the associated resources
180177
client.close();
@@ -195,7 +192,12 @@ public void connecting04(Vertx vertx) {
195192
.setMaxSize(5);
196193

197194
// Create the pooled client
198-
PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions);
195+
Pool pool = PgBuilder
196+
.pool()
197+
.config(poolOptions)
198+
.connectingTo(connectOptions)
199+
.using(vertx)
200+
.build();
199201

200202
// Get a connection from the pool
201203
pool.getConnection().compose(conn -> {
@@ -259,19 +261,29 @@ public void connecting05(Vertx vertx) {
259261
}
260262

261263
public void clientPipelining(Vertx vertx, PgConnectOptions connectOptions, PoolOptions poolOptions) {
262-
PgPool pool = PgPool.pool(vertx, connectOptions.setPipeliningLimit(16), poolOptions);
264+
Pool pool = PgBuilder
265+
.pool()
266+
.connectingTo(connectOptions.setPipeliningLimit(16))
267+
.config(poolOptions)
268+
.using(vertx)
269+
.build();
263270
}
264271

265272
public void poolVersusPooledClient(Vertx vertx, String sql, PgConnectOptions connectOptions, PoolOptions poolOptions) {
266273

267274
// Pooled client
268-
SqlClient client = PgPool.client(vertx, connectOptions, poolOptions);
275+
SqlClient client = PgBuilder.client().config(poolOptions).connectingTo(connectOptions).using(vertx).build();
269276

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

273280
// Connection pool
274-
PgPool pool = PgPool.pool(vertx, connectOptions, poolOptions);
281+
Pool pool = PgBuilder
282+
.pool()
283+
.connectingTo(connectOptions)
284+
.config(poolOptions)
285+
.using(vertx)
286+
.build();
275287

276288
// Not pipelined
277289
Future<RowSet<Row>> res2 = pool.query(sql).execute();
@@ -291,11 +303,20 @@ public void unixDomainSockets(Vertx vertx) {
291303
.setMaxSize(5);
292304

293305
// Create the pooled client
294-
PgPool client = PgPool.pool(connectOptions, poolOptions);
306+
Pool client = PgBuilder
307+
.pool()
308+
.connectingTo(connectOptions)
309+
.config(poolOptions)
310+
.build();
295311

296312
// Create the pooled client with a vertx instance
297313
// Make sure the vertx instance has enabled native transports
298-
PgPool client2 = PgPool.pool(vertx, connectOptions, poolOptions);
314+
Pool client2 = PgBuilder
315+
.pool()
316+
.connectingTo(connectOptions)
317+
.config(poolOptions)
318+
.using(vertx)
319+
.build();
299320
}
300321

301322
public void reconnectAttempts(PgConnectOptions options) {

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
import io.vertx.core.Vertx;
2323
import io.vertx.core.tracing.TracingPolicy;
2424
import io.vertx.docgen.Source;
25+
import io.vertx.pgclient.PgBuilder;
2526
import io.vertx.pgclient.PgConnectOptions;
26-
import io.vertx.pgclient.PgPool;
27-
import io.vertx.pgclient.spi.PgDriver;
2827
import io.vertx.sqlclient.*;
29-
import io.vertx.sqlclient.spi.ConnectionFactory;
3028

3129
import java.util.ArrayList;
3230
import java.util.Arrays;
@@ -365,10 +363,10 @@ public void tracing01(PgConnectOptions options) {
365363
}
366364

367365
public void poolConfig01(PgConnectOptions server1, PgConnectOptions server2, PgConnectOptions server3, PoolOptions options) {
368-
PgPool pool = PgPool.pool(Arrays.asList(server1, server2, server3), options);
366+
Pool pool = PgBuilder.pool().connectingTo(Arrays.asList(server1, server2, server3)).config(options).build();
369367
}
370368

371-
public void poolConfig02(PgPool pool, String sql) {
369+
public void poolConfig02(Pool pool, String sql) {
372370
pool.connectHandler(conn -> {
373371
conn.query(sql).execute().onSuccess(res -> {
374372
// Release the connection to the pool, ready to be used by the application
@@ -378,7 +376,7 @@ public void poolConfig02(PgPool pool, String sql) {
378376
}
379377

380378
public void poolSharing1(Vertx vertx, PgConnectOptions database, int maxSize) {
381-
PgPool pool = PgPool.pool(database, new PoolOptions().setMaxSize(maxSize));
379+
Pool pool = PgBuilder.pool().config(new PoolOptions().setMaxSize(maxSize)).connectingTo(database).build();
382380
vertx.deployVerticle(() -> new AbstractVerticle() {
383381
@Override
384382
public void start() throws Exception {
@@ -389,36 +387,44 @@ public void start() throws Exception {
389387

390388
public void poolSharing2(Vertx vertx, PgConnectOptions database, int maxSize) {
391389
vertx.deployVerticle(() -> new AbstractVerticle() {
392-
PgPool pool;
390+
Pool pool;
393391
@Override
394392
public void start() {
395393
// Get or create a shared pool
396394
// this actually creates a lease to the pool
397395
// when the verticle is undeployed, the lease will be released automaticaly
398-
pool = PgPool.pool(database, new PoolOptions()
399-
.setMaxSize(maxSize)
400-
.setShared(true)
401-
.setName("my-pool"));
396+
pool = PgBuilder.pool().
397+
config(new PoolOptions()
398+
.setMaxSize(maxSize)
399+
.setShared(true)
400+
.setName("my-pool")).connectingTo(database)
401+
.build();
402402
}
403403
}, new DeploymentOptions().setInstances(4));
404404
}
405405

406406
public static void poolSharing3(Vertx vertx, PgConnectOptions database, int maxSize) {
407-
PgPool pool = PgPool.pool(database, new PoolOptions()
408-
.setMaxSize(maxSize)
409-
.setShared(true)
410-
.setName("my-pool")
411-
.setEventLoopSize(4));
407+
Pool pool = PgBuilder.pool()
408+
.config(new PoolOptions()
409+
.setMaxSize(maxSize)
410+
.setShared(true)
411+
.setName("my-pool")
412+
.setEventLoopSize(4)).connectingTo(database)
413+
.build();
412414
}
413415

414416
public void dynamicPoolConfig(Vertx vertx, PoolOptions poolOptions) {
415-
PgPool pool = PgPool.pool(vertx, () -> {
416-
Future<PgConnectOptions> connectOptions = retrieveOptions();
417-
return connectOptions;
418-
}, poolOptions);
417+
Pool pool = PgBuilder.pool()
418+
.config(poolOptions)
419+
.connectingTo(() -> {
420+
Future<SqlConnectOptions> connectOptions = retrieveOptions();
421+
return connectOptions;
422+
})
423+
.using(vertx)
424+
.build();
419425
}
420426

421-
private Future<PgConnectOptions> retrieveOptions() {
427+
private Future<SqlConnectOptions> retrieveOptions() {
422428
return null;
423429
}
424430
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.pgclient;
19+
20+
import io.vertx.codegen.annotations.VertxGen;
21+
import io.vertx.core.Handler;
22+
import io.vertx.pgclient.impl.PgPoolOptions;
23+
import io.vertx.pgclient.spi.PgDriver;
24+
import io.vertx.sqlclient.*;
25+
import io.vertx.sqlclient.impl.ClientBuilderBase;
26+
27+
/**
28+
* Entry point for building PostgreSQL clients.
29+
*/
30+
@VertxGen
31+
public interface PgBuilder {
32+
33+
/**
34+
* Build a pool with the specified {@code block} argument.
35+
* The {@code block} argument is usually a lambda that configures the provided builder
36+
* <p>
37+
* Example usage: {@code Pool pool = PgBuilder.pool(builder -> builder.connectingTo(connectOptions));}
38+
*
39+
* @return the pool as configured by the code {@code block}
40+
*/
41+
static Pool pool(Handler<ClientBuilder<Pool>> block) {
42+
ClientBuilder<Pool> builder = pool();
43+
block.handle(builder);
44+
return builder.build();
45+
}
46+
47+
/**
48+
* Provide a builder for PostgreSQL pool of connections
49+
* <p>
50+
* Example usage: {@code Pool pool = PgBuilder.pool().connectingTo(connectOptions).build()}
51+
*/
52+
static ClientBuilder<Pool> pool() {
53+
return new ClientBuilderBase<Pool>(PgDriver.INSTANCE) {
54+
@Override
55+
public Pool build() {
56+
PoolOptions options = poolOptions;
57+
if (options == null) {
58+
options = new PoolOptions();
59+
}
60+
return driver.createPool(vertx, database, options);
61+
}
62+
};
63+
}
64+
65+
/**
66+
* Build a client backed by a connection pool with the specified {@code block} argument.
67+
* The {@code block} argument is usually a lambda that configures the provided builder
68+
* <p>
69+
* Example usage: {@code SqlClient client = PgBuilder.client(builder -> builder.connectingTo(connectOptions));}
70+
*
71+
* @return the client as configured by the code {@code block}
72+
*/
73+
static SqlClient client(Handler<ClientBuilder<SqlClient>> handler) {
74+
ClientBuilder<SqlClient> builder = client();
75+
handler.handle(builder);
76+
return builder.build();
77+
}
78+
79+
/**
80+
* Provide a builder for PostgreSQL client backed by a connection pool.
81+
* <p>
82+
* Example usage: {@code SqlClient client = PgBuilder.client().connectingTo(connectOptions).build()}
83+
*/
84+
static ClientBuilder<SqlClient> client() {
85+
return new ClientBuilderBase<SqlClient>(PgDriver.INSTANCE) {
86+
@Override
87+
public Pool build() {
88+
PoolOptions options = poolOptions;
89+
if (options != null) {
90+
options = new PgPoolOptions(poolOptions).setPipelined(true);
91+
} else {
92+
options = new PoolOptions();
93+
}
94+
return driver.createPool(vertx, database, options);
95+
}
96+
};
97+
}
98+
}

vertx-pg-client/src/main/java/io/vertx/pgclient/PgPool.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
*
4242
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
4343
*/
44+
@Deprecated
4445
@VertxGen
4546
public interface PgPool extends Pool {
4647

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/PgPoolOptions.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public PgPoolOptions(PoolOptions other) {
2424
super(other);
2525
}
2626

27+
public PgPoolOptions() {
28+
}
29+
2730
private boolean pipelined;
2831

2932
public boolean isPipelined() {

vertx-pg-client/src/test/java/io/vertx/pgclient/CloseConnectionTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.vertx.ext.unit.Async;
88
import io.vertx.ext.unit.TestContext;
99
import io.vertx.pgclient.spi.PgDriver;
10+
import io.vertx.sqlclient.Pool;
1011
import io.vertx.sqlclient.PoolOptions;
1112
import io.vertx.sqlclient.ProxyServer;
1213
import io.vertx.sqlclient.spi.ConnectionFactory;
@@ -41,7 +42,10 @@ public void testCloseConnection(TestContext ctx) {
4142
@Test
4243
public void testClosePooledConnection(TestContext ctx) {
4344
testCloseConnection(ctx, () -> {
44-
PgPool pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(1));
45+
Pool pool = PgBuilder.pool(builder -> builder
46+
.config(new PoolOptions().setMaxSize(1))
47+
.connectingTo(options)
48+
.using(vertx));
4549
pool.getConnection().onComplete(ctx.asyncAssertSuccess(conn -> {
4650
conn.close(ctx.asyncAssertSuccess(v -> {
4751
pool.close(ctx.asyncAssertSuccess());
@@ -53,7 +57,10 @@ public void testClosePooledConnection(TestContext ctx) {
5357
@Test
5458
public void testCloseNetSocket(TestContext ctx) {
5559
testCloseConnection(ctx, () -> {
56-
PgPool pool = PgPool.pool(vertx, options, new PoolOptions().setMaxSize(1));
60+
Pool pool = PgBuilder.pool(builder -> builder
61+
.config(new PoolOptions().setMaxSize(1))
62+
.connectingTo(options)
63+
.using(vertx));
5764
ConnectionFactory factory = PgDriver.INSTANCE.createConnectionFactory(vertx, options);
5865
pool.connectionProvider(factory::connect);
5966
pool.getConnection().onComplete(ctx.asyncAssertSuccess(conn -> {

0 commit comments

Comments
 (0)