Skip to content

Commit 3aaa5d6

Browse files
committed
Replace onComplete with onSuccess + onFailure
Simplify the code by replacing onComplete + if-ar.succeeded-else with onSuccess and onFailure. This avoid the additional nesting of the if-else clause. In addition to the verbose exampleFuture1 code show how it can be reduced to concise but still understandable code. Remove unused exampleFuture2 from lines 173-186. A new exampleFuture2 is created in line 145.
1 parent 3eb4248 commit 3aaa5d6

File tree

2 files changed

+96
-80
lines changed

2 files changed

+96
-80
lines changed

vertx-core/src/main/asciidoc/futures.adoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ You cannot interact directly with the result of a future, instead you need to se
1212
{@link examples.CoreExamples#exampleFuture1}
1313
----
1414

15+
Instead of using `onComplete` with `if (ar.succeeded()) { … } else { … }` you can use the `onSuccess` and `onFailure` handlers:
16+
17+
[source,$lang]
18+
----
19+
{@link examples.CoreExamples#exampleFuture2}
20+
----
21+
22+
Frequently the future variable can be avoided. Often the lambda parameter type is not needed by compiler and code readers, allowing to replace `(FileProps fileProps)` with `fileProps`. In a lambda code block with a single statement the semicolon and the curly braces can be removed. This yields a more concise code:
23+
24+
[source,$lang]
25+
----
26+
{@link examples.CoreExamples#exampleFuture3}
27+
----
28+
1529
[CAUTION]
1630
====
1731
Do not confuse _futures_ with _promises_.

vertx-core/src/main/java/examples/CoreExamples.java

Lines changed: 82 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
2+
* Copyright (c) 2011-2025 Contributors to the Eclipse Foundation
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -73,19 +73,19 @@ public void example7(Vertx vertx) {
7373
vertx.executeBlocking(() -> {
7474
// Call some blocking API that takes a significant amount of time to return
7575
return someAPI.blockingMethod("hello");
76-
}).onComplete(res -> {
77-
System.out.println("The result is: " + res.result());
78-
});
76+
})
77+
.onSuccess(result -> System.out.println("The result is: " + result))
78+
.onFailure(e -> e.printStackTrace());
7979
}
8080

8181
public void workerExecutor1(Vertx vertx) {
8282
WorkerExecutor executor = vertx.createSharedWorkerExecutor("my-worker-pool");
8383
executor.executeBlocking(() -> {
8484
// Call some blocking API that takes a significant amount of time to return
8585
return someAPI.blockingMethod("hello");
86-
}).onComplete(res -> {
87-
System.out.println("The result is: " + res.result());
88-
});
86+
})
87+
.onSuccess(result -> System.out.println("The result is: " + result))
88+
.onFailure(e -> e.printStackTrace());
8989
}
9090

9191
public void workerExecutor2(WorkerExecutor executor) {
@@ -127,7 +127,7 @@ public void clusteredVertxBuilder(VertxOptions options, ClusterManager clusterMa
127127
.buildClustered();
128128
}
129129

130-
public void exampleFuture1(Vertx vertx, Handler<HttpServerRequest> requestHandler) {
130+
public void exampleFuture1(Vertx vertx) {
131131
FileSystem fs = vertx.fileSystem();
132132

133133
Future<FileProps> future = fs.props("/my_file.txt");
@@ -142,6 +142,28 @@ public void exampleFuture1(Vertx vertx, Handler<HttpServerRequest> requestHandle
142142
});
143143
}
144144

145+
public void exampleFuture2(Vertx vertx) {
146+
FileSystem fs = vertx.fileSystem();
147+
148+
Future<FileProps> future = fs.props("/my_file.txt");
149+
150+
future
151+
.onSuccess((FileProps fileProps) -> {
152+
System.out.println("File size = " + fileProps.size());
153+
})
154+
.onFailure((Throwable e) -> {
155+
System.out.println("Failure: " + e.getMessage());
156+
});
157+
}
158+
159+
public void exampleFuture3(Vertx vertx) {
160+
FileSystem fs = vertx.fileSystem();
161+
162+
fs.props("/my_file.txt")
163+
.onSuccess(fileProps -> System.out.println("File size = " + fileProps.size()))
164+
.onFailure(e -> System.out.println("Failure: " + e.getMessage()));
165+
}
166+
145167
public void promiseCallbackOrder(Future<Void> future) {
146168
future.onComplete(ar -> {
147169
// Do something
@@ -170,61 +192,58 @@ public void exampleFutureComposition1(Vertx vertx) {
170192
});
171193
}
172194

173-
public void exampleFuture2(Vertx vertx, Handler<HttpServerRequest> requestHandler) {
174-
FileSystem fs = vertx.fileSystem();
195+
public void exampleFutureComposition2(Vertx vertx) {
175196

176-
Future<FileProps> future = fs.props("/my_file.txt");
197+
FileSystem fs = vertx.fileSystem();
177198

178-
future.onComplete((AsyncResult<FileProps> ar) -> {
179-
if (ar.succeeded()) {
180-
FileProps props = ar.result();
181-
System.out.println("File size = " + props.size());
182-
} else {
183-
System.out.println("Failure: " + ar.cause().getMessage());
184-
}
185-
});
199+
Future<Void> future = fs
200+
.createFile("/foo")
201+
// When the file is created (fut1), execute this:
202+
.compose(v -> fs.writeFile("/foo", Buffer.buffer()))
203+
// When the file is written (fut2), execute this:
204+
.compose(v -> fs.move("/foo", "/bar"));
186205
}
187206

188207
public void exampleFutureAll1(HttpServer httpServer, NetServer netServer) {
189208
Future<HttpServer> httpServerFuture = httpServer.listen();
190209

191210
Future<NetServer> netServerFuture = netServer.listen();
192211

193-
Future.all(httpServerFuture, netServerFuture).onComplete(ar -> {
194-
if (ar.succeeded()) {
212+
Future.all(httpServerFuture, netServerFuture)
213+
.onSuccess(result -> {
195214
// All servers started
196-
} else {
215+
})
216+
.onFailure(e -> {
197217
// At least one server failed
198-
}
199-
});
218+
});
200219
}
201220

202221
public void exampleFutureAll2(Future<?> future1, Future<?> future2, Future<?> future3) {
203222
Future.all(Arrays.asList(future1, future2, future3));
204223
}
205224

206225
public void exampleFutureAny1(Future<String> future1, Future<String> future2) {
207-
Future.any(future1, future2).onComplete(ar -> {
208-
if (ar.succeeded()) {
226+
Future.any(future1, future2)
227+
.onSuccess(result -> {
209228
// At least one is succeeded
210-
} else {
229+
})
230+
.onFailure(e -> {
211231
// All failed
212-
}
213-
});
232+
});
214233
}
215234

216235
public void exampleFutureAny2(Future<?> f1, Future<?> f2, Future<?> f3) {
217236
Future.any(Arrays.asList(f1, f2, f3));
218237
}
219238

220239
public void exampleFutureJoin1(Future<?> future1, Future<?> future2, Future<?> future3) {
221-
Future.join(future1, future2, future3).onComplete(ar -> {
222-
if (ar.succeeded()) {
240+
Future.join(future1, future2, future3)
241+
.onSuccess(result -> {
223242
// All succeeded
224-
} else {
243+
})
244+
.onFailure(e -> {
225245
// All completed and at least one failed
226-
}
227-
});
246+
});
228247
}
229248

230249
public void exampleFutureJoin2(Future<?> future1, Future<?> future2, Future<?> future3) {
@@ -286,13 +305,9 @@ public void start(Promise<Void> startPromise) throws Exception {
286305
Future<String> future = bindService();
287306

288307
// Requires to write
289-
future.onComplete(ar -> {
290-
if (ar.succeeded()) {
291-
startPromise.complete();
292-
} else {
293-
startPromise.fail(ar.cause());
294-
}
295-
});
308+
future
309+
.onSuccess(x -> startPromise.complete())
310+
.onFailure(startPromise::fail);
296311

297312
// Or
298313
future
@@ -337,25 +352,15 @@ public void example9(Vertx vertx) {
337352
public void example10(Vertx vertx) {
338353
vertx
339354
.deployVerticle(new MyOrderProcessorVerticle())
340-
.onComplete(res -> {
341-
if (res.succeeded()) {
342-
System.out.println("Deployment id is: " + res.result());
343-
} else {
344-
System.out.println("Deployment failed!");
345-
}
346-
});
355+
.onSuccess(deploymentID -> System.out.println("Deployment id is: " + deploymentID))
356+
.onFailure(e -> System.out.println("Deployment failed: " + e.getMessage()));
347357
}
348358

349359
public void example11(Vertx vertx, String deploymentID) {
350360
vertx
351361
.undeploy(deploymentID)
352-
.onComplete(res -> {
353-
if (res.succeeded()) {
354-
System.out.println("Undeployed ok");
355-
} else {
356-
System.out.println("Undeploy failed!");
357-
}
358-
});
362+
.onSuccess(x -> System.out.println("Undeployed ok"))
363+
.onFailure(e -> System.out.println("Undeploy failed: " + e.getMessage()));
359364
}
360365

361366
public void example12(Vertx vertx) {
@@ -527,12 +532,11 @@ public void tcpServerWithDomainSockets(Vertx vertx) {
527532
// Handle application
528533
})
529534
.listen(address)
530-
.onComplete(ar -> {
531-
if (ar.succeeded()) {
532-
// Bound to socket
533-
} else {
534-
// Handle failure
535-
}
535+
.onSuccess(theNetServer -> {
536+
// Bound to socket
537+
})
538+
.onFailure(e -> {
539+
// Handle failure
536540
});
537541
}
538542

@@ -547,12 +551,11 @@ public void httpServerWithDomainSockets(Vertx vertx) {
547551
// Handle application
548552
})
549553
.listen(address)
550-
.onComplete(ar -> {
551-
if (ar.succeeded()) {
552-
// Bound to socket
553-
} else {
554-
// Handle failure
555-
}
554+
.onSuccess(theHttpServer -> {
555+
// Bound to socket
556+
})
557+
.onFailure(e -> {
558+
// Handle failure
556559
});
557560
}
558561

@@ -565,12 +568,11 @@ public void tcpClientWithDomainSockets(Vertx vertx) {
565568
// Connect to the server
566569
netClient
567570
.connect(addr)
568-
.onComplete(ar -> {
569-
if (ar.succeeded()) {
570-
// Connected
571-
} else {
572-
// Handle failure
573-
}
571+
.onSuccess(netSocket -> {
572+
// Connected
573+
})
574+
.onFailure(e -> {
575+
// Handle failure
574576
});
575577
}
576578

@@ -586,13 +588,13 @@ public void httpClientWithDomainSockets(Vertx vertx) {
586588
.setHost("localhost")
587589
.setPort(8080)
588590
.setURI("/"))
589-
.compose(request -> request.send().compose(HttpClientResponse::body))
590-
.onComplete(ar -> {
591-
if (ar.succeeded()) {
592-
// Process response
593-
} else {
594-
// Handle failure
595-
}
591+
.compose(request -> request.send())
592+
.compose(HttpClientResponse::body)
593+
.onSuccess(buffer -> {
594+
// Process response
595+
})
596+
.onFailure(e -> {
597+
// Handle failure
596598
});
597599
}
598600
}

0 commit comments

Comments
 (0)