1
1
/*
2
- * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
2
+ * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation
3
3
*
4
4
* This program and the accompanying materials are made available under the
5
5
* terms of the Eclipse Public License 2.0 which is available at
@@ -73,19 +73,19 @@ public void example7(Vertx vertx) {
73
73
vertx .executeBlocking (() -> {
74
74
// Call some blocking API that takes a significant amount of time to return
75
75
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 () );
79
79
}
80
80
81
81
public void workerExecutor1 (Vertx vertx ) {
82
82
WorkerExecutor executor = vertx .createSharedWorkerExecutor ("my-worker-pool" );
83
83
executor .executeBlocking (() -> {
84
84
// Call some blocking API that takes a significant amount of time to return
85
85
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 () );
89
89
}
90
90
91
91
public void workerExecutor2 (WorkerExecutor executor ) {
@@ -127,7 +127,7 @@ public void clusteredVertxBuilder(VertxOptions options, ClusterManager clusterMa
127
127
.buildClustered ();
128
128
}
129
129
130
- public void exampleFuture1 (Vertx vertx , Handler < HttpServerRequest > requestHandler ) {
130
+ public void exampleFuture1 (Vertx vertx ) {
131
131
FileSystem fs = vertx .fileSystem ();
132
132
133
133
Future <FileProps > future = fs .props ("/my_file.txt" );
@@ -142,6 +142,28 @@ public void exampleFuture1(Vertx vertx, Handler<HttpServerRequest> requestHandle
142
142
});
143
143
}
144
144
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
+
145
167
public void promiseCallbackOrder (Future <Void > future ) {
146
168
future .onComplete (ar -> {
147
169
// Do something
@@ -170,61 +192,58 @@ public void exampleFutureComposition1(Vertx vertx) {
170
192
});
171
193
}
172
194
173
- public void exampleFuture2 (Vertx vertx , Handler <HttpServerRequest > requestHandler ) {
174
- FileSystem fs = vertx .fileSystem ();
195
+ public void exampleFutureComposition2 (Vertx vertx ) {
175
196
176
- Future < FileProps > future = fs . props ( "/my_file.txt" );
197
+ FileSystem fs = vertx . fileSystem ( );
177
198
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" ));
186
205
}
187
206
188
207
public void exampleFutureAll1 (HttpServer httpServer , NetServer netServer ) {
189
208
Future <HttpServer > httpServerFuture = httpServer .listen ();
190
209
191
210
Future <NetServer > netServerFuture = netServer .listen ();
192
211
193
- Future .all (httpServerFuture , netServerFuture ). onComplete ( ar -> {
194
- if ( ar . succeeded ()) {
212
+ Future .all (httpServerFuture , netServerFuture )
213
+ . onSuccess ( result -> {
195
214
// All servers started
196
- } else {
215
+ })
216
+ .onFailure (e -> {
197
217
// At least one server failed
198
- }
199
- });
218
+ });
200
219
}
201
220
202
221
public void exampleFutureAll2 (Future <?> future1 , Future <?> future2 , Future <?> future3 ) {
203
222
Future .all (Arrays .asList (future1 , future2 , future3 ));
204
223
}
205
224
206
225
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 -> {
209
228
// At least one is succeeded
210
- } else {
229
+ })
230
+ .onFailure (e -> {
211
231
// All failed
212
- }
213
- });
232
+ });
214
233
}
215
234
216
235
public void exampleFutureAny2 (Future <?> f1 , Future <?> f2 , Future <?> f3 ) {
217
236
Future .any (Arrays .asList (f1 , f2 , f3 ));
218
237
}
219
238
220
239
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 -> {
223
242
// All succeeded
224
- } else {
243
+ })
244
+ .onFailure (e -> {
225
245
// All completed and at least one failed
226
- }
227
- });
246
+ });
228
247
}
229
248
230
249
public void exampleFutureJoin2 (Future <?> future1 , Future <?> future2 , Future <?> future3 ) {
@@ -286,13 +305,9 @@ public void start(Promise<Void> startPromise) throws Exception {
286
305
Future <String > future = bindService ();
287
306
288
307
// 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 );
296
311
297
312
// Or
298
313
future
@@ -337,25 +352,15 @@ public void example9(Vertx vertx) {
337
352
public void example10 (Vertx vertx ) {
338
353
vertx
339
354
.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 ()));
347
357
}
348
358
349
359
public void example11 (Vertx vertx , String deploymentID ) {
350
360
vertx
351
361
.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 ()));
359
364
}
360
365
361
366
public void example12 (Vertx vertx ) {
@@ -527,12 +532,11 @@ public void tcpServerWithDomainSockets(Vertx vertx) {
527
532
// Handle application
528
533
})
529
534
.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
536
540
});
537
541
}
538
542
@@ -547,12 +551,11 @@ public void httpServerWithDomainSockets(Vertx vertx) {
547
551
// Handle application
548
552
})
549
553
.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
556
559
});
557
560
}
558
561
@@ -565,12 +568,11 @@ public void tcpClientWithDomainSockets(Vertx vertx) {
565
568
// Connect to the server
566
569
netClient
567
570
.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
574
576
});
575
577
}
576
578
@@ -586,13 +588,13 @@ public void httpClientWithDomainSockets(Vertx vertx) {
586
588
.setHost ("localhost" )
587
589
.setPort (8080 )
588
590
.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
596
598
});
597
599
}
598
600
}
0 commit comments