Skip to content

Commit 9b17a6d

Browse files
committed
refractor to vertx 3.9.1 , changing Future to Promise for async callback handling
1 parent ee50b7c commit 9b17a6d

File tree

5 files changed

+84
-66
lines changed

5 files changed

+84
-66
lines changed

.idea/compiler.xml

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common-microservice/src/main/java/io/vertx/easyerp/microservice/common/BaseMicroserviceVerticle.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.vertx.core.AbstractVerticle;
66
import io.vertx.core.CompositeFuture;
77
import io.vertx.core.Future;
8+
import io.vertx.core.Promise;
89
import io.vertx.core.impl.ConcurrentHashSet;
910
import io.vertx.core.json.JsonObject;
1011
import io.vertx.core.logging.Logger;
@@ -120,50 +121,52 @@ protected Future<Void> publish(Record record) {
120121
}
121122
}
122123

123-
Future<Void> future = Future.future();
124+
Promise<Void> promise = Promise.promise();
124125
discovery.publish(record, ar -> {
125126
if (ar.succeeded()) {
126127
registeredRecords.add(record);
127128
logger.info("Service <" + ar.result().getName() + "> published");
128-
future.complete();
129+
promise.complete();
129130
} else {
130131
logger.info("Error::Service <" + ar.result().getName() + "> failed published");
131-
future.fail(ar.cause());
132+
promise.fail(ar.cause());
132133
}
133134
});
134135

135-
return future;
136+
return promise.future();
136137
}
137138

138139
/**
139140
* Shutdown and cleanup all resources in discovery service
140141
*
141-
* @param stopFuture
142+
* @param stopPromise
142143
* @throws Exception
143144
*/
144145
@SuppressWarnings("rawtypes")
145146
@Override
146-
public void stop(Future<Void> stopFuture) throws Exception {
147+
public void stop(Promise<Void> stopPromise) throws Exception {
147148
List<Future> futures = new ArrayList<>();
148149
registeredRecords.forEach(record -> {
149-
Future<Void> cleanUpFuture = Future.future();
150-
discovery.unpublish(record.getRegistration(), cleanUpFuture.completer());
151-
futures.add(cleanUpFuture);
150+
Promise<Void> cleanUpPromise = Promise.promise();
151+
discovery.unpublish(record.getRegistration(), cleanUpPromise);
152+
futures.add(cleanUpPromise.future());
152153
});
153154

154155
if (futures.isEmpty()) {
155156
discovery.close();
156-
stopFuture.complete();
157+
stopPromise.complete();
157158
} else {
158159
CompositeFuture.all(futures)
159-
.setHandler(ar -> {
160+
.onComplete(ar -> {
160161
discovery.close();
161162
if (ar.failed()) {
162-
stopFuture.fail(ar.cause());
163+
stopPromise.fail(ar.cause());
163164
} else {
164-
stopFuture.complete();
165+
stopPromise.complete();
165166
}
166167
});
167168
}
168169
}
170+
171+
169172
}

common-microservice/src/main/java/io/vertx/easyerp/microservice/common/RestAPIVerticle.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import io.vertx.core.AsyncResult;
44
import io.vertx.core.Future;
55
import io.vertx.core.Handler;
6+
import io.vertx.core.Promise;
7+
import io.vertx.core.http.CookieSameSite;
68
import io.vertx.core.http.HttpMethod;
79
import io.vertx.core.http.HttpServer;
810
import io.vertx.core.json.JsonObject;
911
import io.vertx.core.logging.Logger;
1012
import io.vertx.core.logging.LoggerFactory;
1113
import io.vertx.ext.web.Router;
1214
import io.vertx.ext.web.RoutingContext;
13-
import io.vertx.ext.web.handler.CookieHandler;
1415
import io.vertx.ext.web.handler.CorsHandler;
1516
import io.vertx.ext.web.handler.SessionHandler;
1617
import io.vertx.ext.web.sstore.ClusteredSessionStore;
@@ -33,11 +34,11 @@ public class RestAPIVerticle extends BaseMicroserviceVerticle {
3334
* @return async result
3435
*/
3536
protected Future<Void> createHttpServer(Router router, String host, int port) {
36-
Future<HttpServer> httpServerFuture = Future.future();
37+
Promise<HttpServer> httpServerPromise = Promise.promise();
3738
vertx.createHttpServer()
38-
.requestHandler(router::accept)
39-
.listen(port, host, httpServerFuture.completer());
40-
return httpServerFuture.map(r -> null);
39+
.requestHandler(router)
40+
.listen(port, host, httpServerPromise);
41+
return httpServerPromise.future().map(r -> null);
4142
}
4243

4344
/**
@@ -70,9 +71,11 @@ protected void enableCorsSupport(Router router) {
7071
* @param router router instance
7172
*/
7273
protected void enableLocalSession(Router router) {
73-
router.route().handler(CookieHandler.create());
74+
//router.route().handler(CookieHandler.create()); deprecated
7475
router.route().handler(SessionHandler.create(
75-
LocalSessionStore.create(vertx, "erp.user.session")));
76+
LocalSessionStore.create(vertx, "erp.user.session"))
77+
.setCookieSameSite(CookieSameSite.STRICT)
78+
.setCookieSecureFlag(true));
7679
}
7780

7881
/**
@@ -81,15 +84,17 @@ protected void enableLocalSession(Router router) {
8184
* @param router Clustered session store
8285
*/
8386
protected void enableClusteredSession(Router router) {
84-
router.route().handler(CookieHandler.create());
85-
router.route().handler(SessionHandler.create(ClusteredSessionStore.create(vertx)));
87+
//router.route().handler(CookieHandler.create()); deprecated
88+
router.route().handler(SessionHandler.create(ClusteredSessionStore.create(vertx))
89+
.setCookieSameSite(CookieSameSite.STRICT)
90+
.setCookieSecureFlag(true));
8691
}
8792

8893
/**
8994
* Used to validate user login process
9095
*
9196
* @param context routing context from rest
92-
* @param biConsumer
97+
* @param biConsumer be able to get both results together
9398
*/
9499
protected void requireLogin(RoutingContext context, BiConsumer<RoutingContext, JsonObject> biConsumer) {
95100
Optional<JsonObject> principal = Optional.ofNullable(context.request().getHeader("user-principal"))
@@ -147,9 +152,9 @@ protected <T> Handler<AsyncResult<T>> resultHandler(RoutingContext context, Func
147152
};
148153
}
149154

150-
protected <T> Handler<AsyncResult<T>> resultHandlerNonEmpty(RoutingContext context, Function<T, String> converter){
155+
protected <T> Handler<AsyncResult<T>> resultHandlerNonEmpty(RoutingContext context, Function<T, String> converter) {
151156
return ar -> {
152-
if(ar.succeeded()){
157+
if (ar.succeeded()) {
153158
T res = ar.result();
154159
if (res == null) {
155160
notFound(context);
@@ -167,9 +172,9 @@ protected <T> Handler<AsyncResult<T>> resultHandlerNonEmpty(RoutingContext conte
167172
};
168173
}
169174

170-
protected <T> Handler<AsyncResult<T>> resultHandlerNonEmpty(RoutingContext context){
175+
protected <T> Handler<AsyncResult<T>> resultHandlerNonEmpty(RoutingContext context) {
171176
return ar -> {
172-
if(ar.succeeded()){
177+
if (ar.succeeded()) {
173178
T res = ar.result();
174179
if (res == null) {
175180
notFound(context);

common-microservice/src/main/java/io/vertx/easyerp/microservice/common/service/JdbcRepositoryWrapper.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package io.vertx.easyerp.microservice.common.service;
22

3-
import io.vertx.core.AsyncResult;
4-
import io.vertx.core.Future;
5-
import io.vertx.core.Handler;
6-
import io.vertx.core.Vertx;
3+
import io.vertx.core.*;
74
import io.vertx.core.json.JsonArray;
85
import io.vertx.core.json.JsonObject;
6+
import io.vertx.core.logging.Logger;
7+
import io.vertx.core.logging.LoggerFactory;
98
import io.vertx.ext.jdbc.JDBCClient;
109
import io.vertx.ext.sql.SQLConnection;
1110

@@ -19,17 +18,17 @@
1918
*/
2019

2120
public class JdbcRepositoryWrapper {
22-
21+
private static final Logger logger = LoggerFactory.getLogger(JdbcRepositoryWrapper.class);
2322
protected final JDBCClient client;
2423

2524
public JdbcRepositoryWrapper(Vertx vertx, JsonObject config) {
26-
this.client = JDBCClient.createNonShared(vertx, config);
25+
this.client = JDBCClient.create(vertx, config);
2726
}
2827

2928
/**
30-
* @param params
31-
* @param sql
32-
* @param resultHandler
29+
* @param params JsonArray containing query parameters
30+
* @param sql query
31+
* @param resultHandler results
3332
*/
3433
protected void executeNoResult(JsonArray params, String sql, Handler<AsyncResult<Void>> resultHandler) {
3534
client.getConnection(connHandler(resultHandler, con ->
@@ -44,11 +43,11 @@ protected void executeNoResult(JsonArray params, String sql, Handler<AsyncResult
4443
}
4544

4645
/**
47-
* @param params
48-
* @param sql
49-
* @param ret
50-
* @param resultHandler
51-
* @param <R>
46+
* @param params JsonArray containing query parameters
47+
* @param sql query
48+
* @param ret result passed
49+
* @param resultHandler handler
50+
* @param <R> Unknown Object
5251
*/
5352
protected <R> void execute(JsonArray params, String sql, R ret, Handler<AsyncResult<R>> resultHandler) {
5453
client.getConnection(connHandler(resultHandler, con ->
@@ -62,30 +61,26 @@ protected <R> void execute(JsonArray params, String sql, R ret, Handler<AsyncRes
6261
})));
6362
}
6463

65-
/**
66-
* @param param
67-
* @param sql
68-
* @param <K>
69-
* @return
70-
*/
64+
7165
protected <K> Future<Optional<JsonObject>> retrieveOne(K param, String sql) {
7266
return getConnection()
7367
.compose(con -> {
74-
Future<Optional<JsonObject>> future = Future.future();
68+
Promise<Optional<JsonObject>> promise = Promise.promise();
69+
7570
con.queryWithParams(sql, new JsonArray().add(param), r -> {
7671
if (r.succeeded()) {
7772
List<JsonObject> resList = r.result().getRows();
7873
if (resList == null || resList.isEmpty()) {
79-
future.complete(Optional.empty());
74+
promise.complete(Optional.empty());
8075
} else {
81-
future.complete(Optional.of(resList.get(0)));
76+
promise.complete(Optional.of(resList.get(0)));
8277
}
8378
} else {//hello
84-
future.fail(r.cause());
79+
promise.fail(r.cause());
8580
}
8681
con.close();
8782
});
88-
return future;
83+
return promise.future();
8984
});
9085
}
9186

@@ -96,16 +91,16 @@ protected <K> Future<Optional<JsonObject>> retrieveOne(K param, String sql) {
9691
*/
9792
protected Future<List<JsonObject>> retrieveMany(JsonArray param, String sql) {
9893
return getConnection().compose(con -> {
99-
Future<List<JsonObject>> future = Future.future();
94+
Promise<List<JsonObject>> promise = Promise.promise();
10095
con.queryWithParams(sql, param, r -> {
10196
if (r.succeeded()) {
102-
future.complete(r.result().getRows());
97+
promise.complete(r.result().getRows());
10398
} else {
104-
future.fail(r.cause());
99+
promise.fail(r.cause());
105100
}
106101
con.close();
107102
});
108-
return future;
103+
return promise.future();
109104
});
110105
}
111106

@@ -115,16 +110,16 @@ protected Future<List<JsonObject>> retrieveMany(JsonArray param, String sql) {
115110
*/
116111
protected Future<List<JsonObject>> retrieveAll(String sql) {
117112
return getConnection().compose(con -> {
118-
Future<List<JsonObject>> future = Future.future();
113+
Promise<List<JsonObject>> promise = Promise.promise();
119114
con.query(sql, r -> {
120115
if (r.succeeded()) {
121-
future.complete(r.result().getRows());
116+
promise.complete(r.result().getRows());
122117
} else {
123-
future.fail(r.cause());
118+
promise.fail(r.cause());
124119
}
125120
con.close();
126121
});
127-
return future;
122+
return promise.future();
128123
});
129124
}
130125

@@ -182,10 +177,17 @@ protected <R> Handler<AsyncResult<SQLConnection>> connHandler(Handler<AsyncResul
182177
}
183178

184179
protected Future<SQLConnection> getConnection() {
185-
Future<SQLConnection> future = Future.future();
186-
187-
client.getConnection(future.completer());
188-
return future;
180+
Promise<SQLConnection> promise = Promise.promise();
181+
/*client.getConnection(ar ->{
182+
if(ar.succeeded())
183+
promise.complete(ar.result());
184+
else
185+
logger.error("Database connection failed, cause is "+ar.cause());
186+
promise.fail(ar.cause());
187+
});*/
188+
189+
client.getConnection(promise); //equivalent initial
190+
return promise.future();
189191
}
190192
}
191193

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
</modules>
3333

3434
<properties>
35-
<vertx.version>3.4.1</vertx.version>
35+
<vertx.version>3.9.1</vertx.version>
3636
<mysql.conn.version>6.0.5</mysql.conn.version>
3737
<log4j.version>2.8</log4j.version>
3838
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

0 commit comments

Comments
 (0)