From 50f8d0c3a147c8acf42b94784ddf5700e3e1ea59 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Tue, 7 Mar 2023 14:12:09 +0100 Subject: [PATCH] Remove callback usage in examples --- src/main/java/docoverride/dns/Examples.java | 28 +- src/main/java/examples/CoreExamples.java | 73 +-- src/main/java/examples/DNSExamples.java | 224 ++++---- src/main/java/examples/DatagramExamples.java | 90 ++- src/main/java/examples/EventBusExamples.java | 88 +-- .../java/examples/FileSystemExamples.java | 200 ++++--- src/main/java/examples/HTTP2Examples.java | 36 +- src/main/java/examples/HTTPExamples.java | 521 ++++++++++-------- src/main/java/examples/NetExamples.java | 100 ++-- .../java/examples/SharedDataExamples.java | 166 +++--- src/main/java/examples/StreamsExamples.java | 69 +-- 11 files changed, 859 insertions(+), 736 deletions(-) diff --git a/src/main/java/docoverride/dns/Examples.java b/src/main/java/docoverride/dns/Examples.java index a51819a0f93..6f0a5253bb6 100644 --- a/src/main/java/docoverride/dns/Examples.java +++ b/src/main/java/docoverride/dns/Examples.java @@ -25,20 +25,22 @@ public class Examples { public void example16(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "10.0.0.1"); - client.lookup("nonexisting.vert.xio", ar -> { - if (ar.succeeded()) { - String record = ar.result(); - System.out.println(record); - } else { - Throwable cause = ar.cause(); - if (cause instanceof DnsException) { - DnsException exception = (DnsException) cause; - DnsResponseCode code = exception.code(); - // ... + client + .lookup("nonexisting.vert.xio") + .onComplete(ar -> { + if (ar.succeeded()) { + String record = ar.result(); + System.out.println(record); } else { - System.out.println("Failed to resolve entry" + ar.cause()); + Throwable cause = ar.cause(); + if (cause instanceof DnsException) { + DnsException exception = (DnsException) cause; + DnsResponseCode code = exception.code(); + // ... + } else { + System.out.println("Failed to resolve entry" + ar.cause()); + } } - } - }); + }); } } diff --git a/src/main/java/examples/CoreExamples.java b/src/main/java/examples/CoreExamples.java index b2e475d71d7..af3b5ea1ca4 100644 --- a/src/main/java/examples/CoreExamples.java +++ b/src/main/java/examples/CoreExamples.java @@ -71,7 +71,7 @@ public void example7(Vertx vertx) { // Call some blocking API that takes a significant amount of time to return String result = someAPI.blockingMethod("hello"); promise.complete(result); - }, res -> { + }).onComplete(res -> { System.out.println("The result is: " + res.result()); }); } @@ -82,7 +82,7 @@ public void workerExecutor1(Vertx vertx) { // Call some blocking API that takes a significant amount of time to return String result = someAPI.blockingMethod("hello"); promise.complete(result); - }, res -> { + }).onComplete(res -> { System.out.println("The result is: " + res.result()); }); } @@ -254,23 +254,27 @@ public void example9(Vertx vertx) { } public void example10(Vertx vertx) { - vertx.deployVerticle("com.mycompany.MyOrderProcessorVerticle", res -> { - if (res.succeeded()) { - System.out.println("Deployment id is: " + res.result()); - } else { - System.out.println("Deployment failed!"); - } - }); + vertx + .deployVerticle("com.mycompany.MyOrderProcessorVerticle") + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("Deployment id is: " + res.result()); + } else { + System.out.println("Deployment failed!"); + } + }); } public void example11(Vertx vertx, String deploymentID) { - vertx.undeploy(deploymentID, res -> { - if (res.succeeded()) { - System.out.println("Undeployed ok"); - } else { - System.out.println("Undeploy failed!"); - } - }); + vertx + .undeploy(deploymentID) + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("Undeployed ok"); + } else { + System.out.println("Undeploy failed!"); + } + }); } public void example12(Vertx vertx) { @@ -412,15 +416,18 @@ public void tcpServerWithDomainSockets(Vertx vertx) { } public void httpServerWithDomainSockets(Vertx vertx) { - vertx.createHttpServer().requestHandler(req -> { - // Handle application - }).listen(SocketAddress.domainSocketAddress("/var/tmp/myservice.sock"), ar -> { - if (ar.succeeded()) { - // Bound to socket - } else { - ar.cause().printStackTrace(); - } - }); + vertx.createHttpServer() + .requestHandler(req -> { + // Handle application + }) + .listen(SocketAddress.domainSocketAddress("/var/tmp/myservice.sock")) + .onComplete(ar -> { + if (ar.succeeded()) { + // Bound to socket + } else { + ar.cause().printStackTrace(); + } + }); } public void tcpClientWithDomainSockets(Vertx vertx) { @@ -430,13 +437,15 @@ public void tcpClientWithDomainSockets(Vertx vertx) { SocketAddress addr = SocketAddress.domainSocketAddress("/var/tmp/myservice.sock"); // Connect to the server - netClient.connect(addr, ar -> { - if (ar.succeeded()) { - // Connected - } else { - ar.cause().printStackTrace(); - } - }); + netClient + .connect(addr) + .onComplete(ar -> { + if (ar.succeeded()) { + // Connected + } else { + ar.cause().printStackTrace(); + } + }); } public void httpClientWithDomainSockets(Vertx vertx) { diff --git a/src/main/java/examples/DNSExamples.java b/src/main/java/examples/DNSExamples.java index f9772620e23..570cd0e6bd7 100644 --- a/src/main/java/examples/DNSExamples.java +++ b/src/main/java/examples/DNSExamples.java @@ -45,91 +45,105 @@ public void example1__(Vertx vertx) { public void example2(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.lookup("vertx.io", ar -> { - if (ar.succeeded()) { - System.out.println(ar.result()); - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + client + .lookup("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + System.out.println(ar.result()); + } else { + System.out.println("Failed to resolve entry" + ar.cause()); + } + }); } public void example3(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.lookup4("vertx.io", ar -> { - if (ar.succeeded()) { - System.out.println(ar.result()); - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + client + .lookup4("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + System.out.println(ar.result()); + } else { + System.out.println("Failed to resolve entry" + ar.cause()); + } + }); } public void example4(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.lookup6("vertx.io", ar -> { - if (ar.succeeded()) { - System.out.println(ar.result()); - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + client + .lookup6("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + System.out.println(ar.result()); + } else { + System.out.println("Failed to resolve entry" + ar.cause()); + } + }); } public void example5(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.resolveA("vertx.io", ar -> { - if (ar.succeeded()) { - List records = ar.result(); - for (String record : records) { - System.out.println(record); + client + .resolveA("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + List records = ar.result(); + for (String record : records) { + System.out.println(record); + } + } else { + System.out.println("Failed to resolve entry" + ar.cause()); } - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + }); } public void example6(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.resolveAAAA("vertx.io", ar -> { - if (ar.succeeded()) { - List records = ar.result(); - for (String record : records) { - System.out.println(record); + client + .resolveAAAA("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + List records = ar.result(); + for (String record : records) { + System.out.println(record); + } + } else { + System.out.println("Failed to resolve entry" + ar.cause()); } - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + }); } public void example7(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.resolveCNAME("vertx.io", ar -> { - if (ar.succeeded()) { - List records = ar.result(); - for (String record : records) { - System.out.println(record); + client + .resolveCNAME("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + List records = ar.result(); + for (String record : records) { + System.out.println(record); + } + } else { + System.out.println("Failed to resolve entry" + ar.cause()); } - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + }); } public void example8(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.resolveMX("vertx.io", ar -> { - if (ar.succeeded()) { - List records = ar.result(); - for (MxRecord record: records) { - System.out.println(record); + client + .resolveMX("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + List records = ar.result(); + for (MxRecord record : records) { + System.out.println(record); + } + } else { + System.out.println("Failed to resolve entry" + ar.cause()); } - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + }); } public void example9(MxRecord record) { @@ -139,44 +153,50 @@ public void example9(MxRecord record) { public void example10(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.resolveTXT("vertx.io", ar -> { - if (ar.succeeded()) { - List records = ar.result(); - for (String record: records) { - System.out.println(record); + client + .resolveTXT("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + List records = ar.result(); + for (String record : records) { + System.out.println(record); + } + } else { + System.out.println("Failed to resolve entry" + ar.cause()); } - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + }); } public void example11(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.resolveNS("vertx.io", ar -> { - if (ar.succeeded()) { - List records = ar.result(); - for (String record: records) { - System.out.println(record); + client + .resolveNS("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + List records = ar.result(); + for (String record : records) { + System.out.println(record); + } + } else { + System.out.println("Failed to resolve entry" + ar.cause()); } - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + }); } public void example12(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.resolveSRV("vertx.io", ar -> { - if (ar.succeeded()) { - List records = ar.result(); - for (SrvRecord record: records) { - System.out.println(record); + client + .resolveSRV("vertx.io") + .onComplete(ar -> { + if (ar.succeeded()) { + List records = ar.result(); + for (SrvRecord record : records) { + System.out.println(record); + } + } else { + System.out.println("Failed to resolve entry" + ar.cause()); } - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + }); } private static SrvRecord getSrvRecord() { @@ -195,25 +215,29 @@ public void example13(SrvRecord record) { public void example14(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.resolvePTR("1.0.0.10.in-addr.arpa", ar -> { - if (ar.succeeded()) { - String record = ar.result(); - System.out.println(record); - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + client + .resolvePTR("1.0.0.10.in-addr.arpa") + .onComplete(ar -> { + if (ar.succeeded()) { + String record = ar.result(); + System.out.println(record); + } else { + System.out.println("Failed to resolve entry" + ar.cause()); + } + }); } public void example15(Vertx vertx) { DnsClient client = vertx.createDnsClient(53, "9.9.9.9"); - client.reverseLookup("10.0.0.1", ar -> { - if (ar.succeeded()) { - String record = ar.result(); - System.out.println(record); - } else { - System.out.println("Failed to resolve entry" + ar.cause()); - } - }); + client + .reverseLookup("10.0.0.1") + .onComplete(ar -> { + if (ar.succeeded()) { + String record = ar.result(); + System.out.println(record); + } else { + System.out.println("Failed to resolve entry" + ar.cause()); + } + }); } } diff --git a/src/main/java/examples/DatagramExamples.java b/src/main/java/examples/DatagramExamples.java index 0a2f2a9ae02..d0ffd2fcb84 100644 --- a/src/main/java/examples/DatagramExamples.java +++ b/src/main/java/examples/DatagramExamples.java @@ -29,81 +29,67 @@ public void example2(Vertx vertx) { DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions()); Buffer buffer = Buffer.buffer("content"); // Send a Buffer - socket.send(buffer, 1234, "10.0.0.1", asyncResult -> { - System.out.println("Send succeeded? " + asyncResult.succeeded()); - }); + socket + .send(buffer, 1234, "10.0.0.1") + .onComplete(asyncResult -> System.out.println("Send succeeded? " + asyncResult.succeeded())); // Send a String - socket.send("A string used as content", 1234, "10.0.0.1", asyncResult -> { - System.out.println("Send succeeded? " + asyncResult.succeeded()); - }); + socket + .send("A string used as content", 1234, "10.0.0.1") + .onComplete(asyncResult -> System.out.println("Send succeeded? " + asyncResult.succeeded())); } public void example3(Vertx vertx) { DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions()); - socket.listen(1234, "0.0.0.0", asyncResult -> { - if (asyncResult.succeeded()) { - socket.handler(packet -> { - // Do something with the packet - }); - } else { - System.out.println("Listen failed" + asyncResult.cause()); - } - }); + socket + .handler(packet -> { + // Do something with the packet + }) + .listen(1234, "0.0.0.0") + .onComplete(asyncResult -> System.out.println("Send succeeded? " + asyncResult.succeeded())); + ; } public void example4(Vertx vertx) { DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions()); Buffer buffer = Buffer.buffer("content"); // Send a Buffer to a multicast address - socket.send(buffer, 1234, "230.0.0.1", asyncResult -> { - System.out.println("Send succeeded? " + asyncResult.succeeded()); - }); + socket + .send(buffer, 1234, "230.0.0.1") + .onComplete(asyncResult -> System.out.println("Send succeeded? " + asyncResult.succeeded())); } public void example5(Vertx vertx) { DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions()); - socket.listen(1234, "0.0.0.0", asyncResult -> { - if (asyncResult.succeeded()) { - socket.handler(packet -> { - // Do something with the packet - }); - - // join the multicast group - socket.listenMulticastGroup("230.0.0.1", asyncResult2 -> { - System.out.println("Listen succeeded? " + asyncResult2.succeeded()); - }); - } else { - System.out.println("Listen failed" + asyncResult.cause()); - } - }); + socket + .handler(packet -> { + // Do something with the packet + }) + .listen(1234, "0.0.0.0") + .compose(v -> socket.listenMulticastGroup("230.0.0.1")) // join the multicast group + .onComplete(asyncResult -> System.out.println("Listen succeeded? " + asyncResult.succeeded())); } public void example6(Vertx vertx) { DatagramSocket socket = vertx.createDatagramSocket(new DatagramSocketOptions()); - socket.listen(1234, "0.0.0.0", asyncResult -> { + socket + .handler(packet -> { + // Do something with the packet + }) + .listen(1234, "0.0.0.0") + .compose(v -> socket.listenMulticastGroup("230.0.0.1")) // join the multicast group + .onComplete(asyncResult -> { if (asyncResult.succeeded()) { - socket.handler(packet -> { - // Do something with the packet - }); - - // join the multicast group - socket.listenMulticastGroup("230.0.0.1", asyncResult2 -> { - if (asyncResult2.succeeded()) { - // will now receive packets for group + // will now receive packets for group - // do some work + // do some work - socket.unlistenMulticastGroup("230.0.0.1", asyncResult3 -> { - System.out.println("Unlisten succeeded? " + asyncResult3.succeeded()); - }); - } else { - System.out.println("Listen failed" + asyncResult2.cause()); - } + socket.unlistenMulticastGroup("230.0.0.1").onComplete(asyncResult2 -> { + System.out.println("Unlisten succeeded? " + asyncResult2.succeeded()); }); } else { System.out.println("Listen failed" + asyncResult.cause()); } - }); + }); } public void example7(Vertx vertx) { @@ -112,8 +98,8 @@ public void example7(Vertx vertx) { // Some code // This would block packets which are send from 10.0.0.2 - socket.blockMulticastGroup("230.0.0.1", "10.0.0.2", asyncResult -> { - System.out.println("block succeeded? " + asyncResult.succeeded()); - }); + socket + .blockMulticastGroup("230.0.0.1", "10.0.0.2") + .onComplete(asyncResult -> System.out.println("block succeeded? " + asyncResult.succeeded())); } } diff --git a/src/main/java/examples/EventBusExamples.java b/src/main/java/examples/EventBusExamples.java index da965f7fb32..0ce71539820 100644 --- a/src/main/java/examples/EventBusExamples.java +++ b/src/main/java/examples/EventBusExamples.java @@ -56,13 +56,15 @@ public void example3(MessageConsumer consumer) { } public void example4(MessageConsumer consumer) { - consumer.unregister(res -> { - if (res.succeeded()) { - System.out.println("The handler un-registration has reached all nodes"); - } else { - System.out.println("Un-registration failed!"); - } - }); + consumer + .unregister() + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("The handler un-registration has reached all nodes"); + } else { + System.out.println("Un-registration failed!"); + } + }); } public void example5(EventBus eventBus) { @@ -82,24 +84,28 @@ public void example8(EventBus eventBus) { } public void example9(EventBus eventBus) { - eventBus.request("news.uk.sport", "Yay! Someone kicked a ball across a patch of grass", ar -> { - if (ar.succeeded()) { - System.out.println("Received reply: " + ar.result().body()); - } - }); + eventBus + .request("news.uk.sport", "Yay! Someone kicked a ball across a patch of grass") + .onComplete(ar -> { + if (ar.succeeded()) { + System.out.println("Received reply: " + ar.result().body()); + } + }); } public void example12() { VertxOptions options = new VertxOptions(); - Vertx.clusteredVertx(options, res -> { - if (res.succeeded()) { - Vertx vertx = res.result(); - EventBus eventBus = vertx.eventBus(); - System.out.println("We now have a clustered event bus: " + eventBus); - } else { - System.out.println("Failed: " + res.cause()); - } - }); + Vertx + .clusteredVertx(options) + .onComplete(res -> { + if (res.succeeded()) { + Vertx vertx = res.result(); + EventBus eventBus = vertx.eventBus(); + System.out.println("We now have a clustered event bus: " + eventBus); + } else { + System.out.println("Failed: " + res.cause()); + } + }); } public void example13() { @@ -111,15 +117,17 @@ public void example13() { .setClientAuth(ClientAuth.REQUIRED) ); - Vertx.clusteredVertx(options, res -> { - if (res.succeeded()) { - Vertx vertx = res.result(); - EventBus eventBus = vertx.eventBus(); - System.out.println("We now have a clustered event bus: " + eventBus); - } else { - System.out.println("Failed: " + res.cause()); - } - }); + Vertx + .clusteredVertx(options) + .onComplete(res -> { + if (res.succeeded()) { + Vertx vertx = res.result(); + EventBus eventBus = vertx.eventBus(); + System.out.println("We now have a clustered event bus: " + eventBus); + } else { + System.out.println("Failed: " + res.cause()); + } + }); } public void example14() { @@ -129,15 +137,17 @@ public void example14() { .setClusterPublicPort(1234) ); - Vertx.clusteredVertx(options, res -> { - if (res.succeeded()) { - Vertx vertx = res.result(); - EventBus eventBus = vertx.eventBus(); - System.out.println("We now have a clustered event bus: " + eventBus); - } else { - System.out.println("Failed: " + res.cause()); - } - }); + Vertx + .clusteredVertx(options) + .onComplete(res -> { + if (res.succeeded()) { + Vertx vertx = res.result(); + EventBus eventBus = vertx.eventBus(); + System.out.println("We now have a clustered event bus: " + eventBus); + } else { + System.out.println("Failed: " + res.cause()); + } + }); } diff --git a/src/main/java/examples/FileSystemExamples.java b/src/main/java/examples/FileSystemExamples.java index 29d2c6af76a..cfd7b16bbee 100644 --- a/src/main/java/examples/FileSystemExamples.java +++ b/src/main/java/examples/FileSystemExamples.java @@ -11,6 +11,7 @@ package examples; +import io.vertx.core.Future; import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; import io.vertx.core.file.AsyncFile; @@ -26,13 +27,14 @@ public void example1(Vertx vertx) { FileSystem fs = vertx.fileSystem(); // Copy file from foo.txt to bar.txt - fs.copy("foo.txt", "bar.txt", res -> { - if (res.succeeded()) { - // Copied ok! - } else { - // Something went wrong - } - }); + fs.copy("foo.txt", "bar.txt") + .onComplete(res -> { + if (res.succeeded()) { + // Copied ok! + } else { + // Something went wrong + } + }); } public void example2(Vertx vertx) { @@ -44,111 +46,133 @@ public void example2(Vertx vertx) { public void example3(FileSystem fileSystem) { OpenOptions options = new OpenOptions(); - fileSystem.open("myfile.txt", options, res -> { - if (res.succeeded()) { - AsyncFile file = res.result(); - } else { - // Something went wrong! - } - }); + fileSystem + .open("myfile.txt", options) + .onComplete(res -> { + if (res.succeeded()) { + AsyncFile file = res.result(); + } else { + // Something went wrong! + } + }); } public void asyncAPIExamples(Vertx vertx) { // Read a file - vertx.fileSystem().readFile("target/classes/readme.txt", result -> { - if (result.succeeded()) { - System.out.println(result.result()); - } else { - System.err.println("Oh oh ..." + result.cause()); - } - }); + vertx.fileSystem() + .readFile("target/classes/readme.txt") + .onComplete(result -> { + if (result.succeeded()) { + System.out.println(result.result()); + } else { + System.err.println("Oh oh ..." + result.cause()); + } + }); // Copy a file - vertx.fileSystem().copy("target/classes/readme.txt", "target/classes/readme2.txt", result -> { - if (result.succeeded()) { - System.out.println("File copied"); - } else { - System.err.println("Oh oh ..." + result.cause()); - } - }); + vertx.fileSystem() + .copy("target/classes/readme.txt", "target/classes/readme2.txt") + .onComplete(result -> { + if (result.succeeded()) { + System.out.println("File copied"); + } else { + System.err.println("Oh oh ..." + result.cause()); + } + }); // Write a file - vertx.fileSystem().writeFile("target/classes/hello.txt", Buffer.buffer("Hello"), result -> { - if (result.succeeded()) { - System.out.println("File written"); - } else { - System.err.println("Oh oh ..." + result.cause()); - } - }); + vertx.fileSystem() + .writeFile("target/classes/hello.txt", Buffer.buffer("Hello")) + .onComplete(result -> { + if (result.succeeded()) { + System.out.println("File written"); + } else { + System.err.println("Oh oh ..." + result.cause()); + } + }); // Check existence and delete - vertx.fileSystem().exists("target/classes/junk.txt", result -> { - if (result.succeeded() && result.result()) { - vertx.fileSystem().delete("target/classes/junk.txt", r -> { + vertx.fileSystem() + .exists("target/classes/junk.txt") + .compose(exist -> { + if (exist) { + return vertx.fileSystem().delete("target/classes/junk.txt"); + } else { + return Future.failedFuture("File does not exist"); + } + }).onComplete(result -> { + if (result.succeeded()) { System.out.println("File deleted"); - }); - } else { - System.err.println("Oh oh ... - cannot delete the file: " + result.cause()); - } - }); + } else { + System.err.println("Oh oh ... - cannot delete the file: " + result.cause().getMessage()); + } + }); } public void asyncFileWrite(Vertx vertx) { - vertx.fileSystem().open("target/classes/hello.txt", new OpenOptions(), result -> { - if (result.succeeded()) { - AsyncFile file = result.result(); - Buffer buff = Buffer.buffer("foo"); - for (int i = 0; i < 5; i++) { - file.write(buff, buff.length() * i, ar -> { - if (ar.succeeded()) { - System.out.println("Written ok!"); - // etc - } else { - System.err.println("Failed to write: " + ar.cause()); - } - }); + vertx.fileSystem() + .open("target/classes/hello.txt", new OpenOptions()) + .onComplete(result -> { + if (result.succeeded()) { + AsyncFile file = result.result(); + Buffer buff = Buffer.buffer("foo"); + for (int i = 0; i < 5; i++) { + file + .write(buff, buff.length() * i) + .onComplete(ar -> { + if (ar.succeeded()) { + System.out.println("Written ok!"); + // etc + } else { + System.err.println("Failed to write: " + ar.cause()); + } + }); + } + } else { + System.err.println("Cannot open file " + result.cause()); } - } else { - System.err.println("Cannot open file " + result.cause()); - } - }); + }); } public void asyncFileRead(Vertx vertx) { - vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> { - if (result.succeeded()) { - AsyncFile file = result.result(); - Buffer buff = Buffer.buffer(1000); - for (int i = 0; i < 10; i++) { - file.read(buff, i * 100, i * 100, 100, ar -> { - if (ar.succeeded()) { - System.out.println("Read ok!"); - } else { - System.err.println("Failed to write: " + ar.cause()); - } - }); + vertx.fileSystem() + .open("target/classes/les_miserables.txt", new OpenOptions()) + .onComplete(result -> { + if (result.succeeded()) { + AsyncFile file = result.result(); + Buffer buff = Buffer.buffer(1000); + for (int i = 0; i < 10; i++) { + file + .read(buff, i * 100, i * 100, 100) + .onComplete(ar -> { + if (ar.succeeded()) { + System.out.println("Read ok!"); + } else { + System.err.println("Failed to write: " + ar.cause()); + } + }); + } + } else { + System.err.println("Cannot open file " + result.cause()); } - } else { - System.err.println("Cannot open file " + result.cause()); - } - }); + }); } public void asyncFilePipe(Vertx vertx) { final AsyncFile output = vertx.fileSystem().openBlocking("target/classes/plagiary.txt", new OpenOptions()); - vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> { - if (result.succeeded()) { - AsyncFile file = result.result(); - file.pipeTo(output) - .onComplete(v -> { - file.close(); - System.out.println("Copy done"); - }); - } else { - System.err.println("Cannot open file " + result.cause()); - } - }); + vertx.fileSystem() + .open("target/classes/les_miserables.txt", new OpenOptions()) + .compose(file -> file + .pipeTo(output) + .eventually(v -> file.close())) + .onComplete(result -> { + if (result.succeeded()) { + System.out.println("Copy done"); + } else { + System.err.println("Cannot copy file " + result.cause().getMessage()); + } + }); } } diff --git a/src/main/java/examples/HTTP2Examples.java b/src/main/java/examples/HTTP2Examples.java index 9b8336e52d0..fc10ec876e0 100644 --- a/src/main/java/examples/HTTP2Examples.java +++ b/src/main/java/examples/HTTP2Examples.java @@ -88,21 +88,23 @@ public void example6(HttpServerRequest request) { HttpServerResponse response = request.response(); // Push main.js to the client - response.push(HttpMethod.GET, "/main.js", ar -> { + response + .push(HttpMethod.GET, "/main.js") + .onComplete(ar -> { - if (ar.succeeded()) { + if (ar.succeeded()) { - // The server is ready to push the response - HttpServerResponse pushedResponse = ar.result(); + // The server is ready to push the response + HttpServerResponse pushedResponse = ar.result(); - // Send main.js response - pushedResponse. + // Send main.js response + pushedResponse. putHeader("content-type", "application/json"). end("alert(\"Push response hello\")"); - } else { - System.out.println("Could not push client resource " + ar.cause()); - } - }); + } else { + System.out.println("Could not push client resource " + ar.cause()); + } + }); // Send the requested resource response.sendFile(""); @@ -230,11 +232,9 @@ public void example20(HttpConnection connection) { } public void example21(HttpConnection connection) { - connection.updateSettings(new Http2Settings().setMaxConcurrentStreams(100), ar -> { - if (ar.succeeded()) { - System.out.println("The settings update has been acknowledged "); - } - }); + connection + .updateSettings(new Http2Settings().setMaxConcurrentStreams(100)) + .onSuccess(v -> System.out.println("The settings update has been acknowledged ")); } public void example22(HttpConnection connection) { @@ -248,9 +248,9 @@ public void example23(HttpConnection connection) { for (byte i = 0;i < 8;i++) { data.appendByte(i); } - connection.ping(data, pong -> { - System.out.println("Remote side replied"); - }); + connection + .ping(data) + .onSuccess(pong -> System.out.println("Remote side replied")); } public void example24(HttpConnection connection) { diff --git a/src/main/java/examples/HTTPExamples.java b/src/main/java/examples/HTTPExamples.java index 8c3c8fa7a16..c740a234b3a 100644 --- a/src/main/java/examples/HTTPExamples.java +++ b/src/main/java/examples/HTTPExamples.java @@ -72,13 +72,15 @@ public void example4(Vertx vertx) { public void example5(Vertx vertx) { HttpServer server = vertx.createHttpServer(); - server.listen(8080, "myhost.com", res -> { - if (res.succeeded()) { - System.out.println("Server is now listening!"); - } else { - System.out.println("Failed to bind!"); - } - }); + server + .listen(8080, "myhost.com") + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("Server is now listening!"); + } else { + System.out.println("Failed to bind!"); + } + }); } public void example6(Vertx vertx) { @@ -333,11 +335,13 @@ public void exampleClientLogging(Vertx vertx) { } public void example30(HttpClient client) { - client.request(HttpMethod.GET,8080, "myserver.mycompany.com", "/some-uri", ar1 -> { - if (ar1.succeeded()) { - // Connected to the server - } - }); + client + .request(HttpMethod.GET, 8080, "myserver.mycompany.com", "/some-uri") + .onComplete(ar1 -> { + if (ar1.succeeded()) { + // Connected to the server + } + }); } public void example31(Vertx vertx) { @@ -347,17 +351,21 @@ public void example31(Vertx vertx) { // Can also set default port if you want... HttpClient client = vertx.createHttpClient(options); - client.request(HttpMethod.GET, "/some-uri", ar1 -> { - if (ar1.succeeded()) { - HttpClientRequest request = ar1.result(); - request.send(ar2 -> { - if (ar2.succeeded()) { - HttpClientResponse response = ar2.result(); - System.out.println("Received response with status code " + response.statusCode()); - } - }); - } - }); + client + .request(HttpMethod.GET, "/some-uri") + .onComplete(ar1 -> { + if (ar1.succeeded()) { + HttpClientRequest request = ar1.result(); + request + .send() + .onComplete(ar2 -> { + if (ar2.succeeded()) { + HttpClientResponse response = ar2.result(); + System.out.println("Received response with status code " + response.statusCode()); + } + }); + } + }); } public void example32(Vertx vertx) { @@ -367,18 +375,22 @@ public void example32(Vertx vertx) { // Write some headers using the headers multi-map MultiMap headers = HttpHeaders.set("content-type", "application/json").set("other-header", "foo"); - client.request(HttpMethod.GET, "some-uri", ar1 -> { - if (ar1.succeeded()) { + client + .request(HttpMethod.GET, "some-uri") + .onComplete(ar1 -> { if (ar1.succeeded()) { - HttpClientRequest request = ar1.result(); - request.headers().addAll(headers); - request.send(ar2 -> { - HttpClientResponse response = ar2.result(); - System.out.println("Received response with status code " + response.statusCode()); - }); + if (ar1.succeeded()) { + HttpClientRequest request = ar1.result(); + request.headers().addAll(headers); + request + .send() + .onComplete(ar2 -> { + HttpClientResponse response = ar2.result(); + System.out.println("Received response with status code " + response.statusCode()); + }); + } } - } - }); + }); } public void example33(HttpClientRequest request) { @@ -390,52 +402,62 @@ public void example33(HttpClientRequest request) { } public void sendRequest01(HttpClient client) { - client.request(HttpMethod.GET,8080, "myserver.mycompany.com", "/some-uri", ar1 -> { - if (ar1.succeeded()) { - HttpClientRequest request = ar1.result(); - - // Send the request and process the response - request.send(ar -> { - if (ar.succeeded()) { - HttpClientResponse response = ar.result(); - System.out.println("Received response with status code " + response.statusCode()); - } else { - System.out.println("Something went wrong " + ar.cause().getMessage()); - } - }); - } - }); + client + .request(HttpMethod.GET, 8080, "myserver.mycompany.com", "/some-uri") + .onComplete(ar1 -> { + if (ar1.succeeded()) { + HttpClientRequest request = ar1.result(); + + // Send the request and process the response + request + .send() + .onComplete(ar -> { + if (ar.succeeded()) { + HttpClientResponse response = ar.result(); + System.out.println("Received response with status code " + response.statusCode()); + } else { + System.out.println("Something went wrong " + ar.cause().getMessage()); + } + }); + } + }); } public void sendRequest02(HttpClient client) { - client.request(HttpMethod.GET,8080, "myserver.mycompany.com", "/some-uri", ar1 -> { - if (ar1.succeeded()) { - HttpClientRequest request = ar1.result(); - - // Send the request and process the response - request.send("Hello World", ar -> { - if (ar.succeeded()) { - HttpClientResponse response = ar.result(); - System.out.println("Received response with status code " + response.statusCode()); - } else { - System.out.println("Something went wrong " + ar.cause().getMessage()); - } - }); - } - }); + client + .request(HttpMethod.GET, 8080, "myserver.mycompany.com", "/some-uri") + .onComplete(ar1 -> { + if (ar1.succeeded()) { + HttpClientRequest request = ar1.result(); + + // Send the request and process the response + request + .send("Hello World") + .onComplete(ar -> { + if (ar.succeeded()) { + HttpClientResponse response = ar.result(); + System.out.println("Received response with status code " + response.statusCode()); + } else { + System.out.println("Something went wrong " + ar.cause().getMessage()); + } + }); + } + }); } public void sendRequest03(HttpClientRequest request) { // Send the request and process the response - request.send(Buffer.buffer("Hello World"), ar -> { - if (ar.succeeded()) { - HttpClientResponse response = ar.result(); - System.out.println("Received response with status code " + response.statusCode()); - } else { - System.out.println("Something went wrong " + ar.cause().getMessage()); - } - }); + request + .send(Buffer.buffer("Hello World")) + .onComplete(ar -> { + if (ar.succeeded()) { + HttpClientResponse response = ar.result(); + System.out.println("Received response with status code " + response.statusCode()); + } else { + System.out.println("Something went wrong " + ar.cause().getMessage()); + } + }); } public void sendRequest04(HttpClientRequest request, ReadStream stream) { @@ -443,14 +465,15 @@ public void sendRequest04(HttpClientRequest request, ReadStream stream) // Send the request and process the response request .putHeader(HttpHeaders.CONTENT_LENGTH, "1000") - .send(stream, ar -> { - if (ar.succeeded()) { - HttpClientResponse response = ar.result(); - System.out.println("Received response with status code " + response.statusCode()); - } else { - System.out.println("Something went wrong " + ar.cause().getMessage()); - } - }); + .send(stream) + .onComplete(ar -> { + if (ar.succeeded()) { + HttpClientResponse response = ar.result(); + System.out.println("Received response with status code " + response.statusCode()); + } else { + System.out.println("Something went wrong " + ar.cause().getMessage()); + } + }); } public void example34(Vertx vertx, String body) { HttpClient client = vertx.createHttpClient(); @@ -469,22 +492,6 @@ public void example34(Vertx vertx, String body) { // Make sure the request is ended when you're done with it request.end(); }); - - // Or fluently: - - client.request(HttpMethod.POST, "some-uri") - .onSuccess(request -> { - request - .response(ar -> { - if (ar.succeeded()) { - HttpClientResponse response = ar.result(); - System.out.println("Received response with status code " + response.statusCode()); - } - }) - .putHeader("content-length", "1000") - .putHeader("content-type", "text/plain") - .end(body); - }); } public void example35(HttpClientRequest request) { @@ -562,22 +569,9 @@ public void example44(HttpClientRequest request, AsyncFile file) { public void example45(HttpClientRequest request) { // Send the request - request.send(ar2 -> { - if (ar2.succeeded()) { - - HttpClientResponse response = ar2.result(); - - // the status code - e.g. 200 or 404 - System.out.println("Status code is " + response.statusCode()); - - // the status message e.g. "OK" or "Not Found". - System.out.println("Status message is " + response.statusMessage()); - } - }); - - // Similar to above, set a completion handler and end the request request - .response(ar2 -> { + .send() + .onComplete(ar2 -> { if (ar2.succeeded()) { HttpClientResponse response = ar2.result(); @@ -588,9 +582,7 @@ public void example45(HttpClientRequest request) { // the status message e.g. "OK" or "Not Found". System.out.println("Status message is " + response.statusMessage()); } - }) - .end(); - + }); } public void example46(HttpClientResponse response) { @@ -602,61 +594,71 @@ public void example46(HttpClientResponse response) { public void example47(HttpClient client) { - client.request(HttpMethod.GET, "some-uri", ar1 -> { + client + .request(HttpMethod.GET, "some-uri") + .onComplete(ar1 -> { - if (ar1.succeeded()) { - HttpClientRequest request = ar1.result(); - request.send(ar2 -> { - HttpClientResponse response = ar2.result(); - response.handler(buffer -> { - System.out.println("Received a part of the response body: " + buffer); - }); - }); - } - }); + if (ar1.succeeded()) { + HttpClientRequest request = ar1.result(); + request + .send() + .onComplete(ar2 -> { + HttpClientResponse response = ar2.result(); + response.handler(buffer -> { + System.out.println("Received a part of the response body: " + buffer); + }); + }); + } + }); } public void example48(HttpClientRequest request) { - request.send(ar2 -> { + request + .send() + .onComplete(ar2 -> { - if (ar2.succeeded()) { + if (ar2.succeeded()) { - HttpClientResponse response = ar2.result(); + HttpClientResponse response = ar2.result(); - // Create an empty buffer - Buffer totalBuffer = Buffer.buffer(); + // Create an empty buffer + Buffer totalBuffer = Buffer.buffer(); - response.handler(buffer -> { - System.out.println("Received a part of the response body: " + buffer.length()); + response.handler(buffer -> { + System.out.println("Received a part of the response body: " + buffer.length()); - totalBuffer.appendBuffer(buffer); - }); + totalBuffer.appendBuffer(buffer); + }); - response.endHandler(v -> { - // Now all the body has been read - System.out.println("Total response body length is " + totalBuffer.length()); - }); - } - }); + response.endHandler(v -> { + // Now all the body has been read + System.out.println("Total response body length is " + totalBuffer.length()); + }); + } + }); } public void example49(HttpClientRequest request) { - request.send(ar1 -> { - - if (ar1.succeeded()) { - HttpClientResponse response = ar1.result(); - response.body(ar2 -> { + request + .send() + .onComplete(ar1 -> { - if (ar2.succeeded()) { - Buffer body = ar2.result(); - // Now all the body has been read - System.out.println("Total response body length is " + body.length()); - } - }); - } - }); + if (ar1.succeeded()) { + HttpClientResponse response = ar1.result(); + response + .body() + .onComplete(ar2 -> { + + if (ar2.succeeded()) { + Buffer body = ar2.result(); + // Now all the body has been read + System.out.println("Total response body length is " + body.length()); + } + }); + } + }); } private interface HttpClient2 { @@ -674,9 +676,11 @@ public void exampleClientComposition01(HttpClient2 client) throws Exception { get.onSuccess(response -> { // Response events might have happen already - response.body(ar -> { + response + .body() + .onComplete(ar -> { - }); + }); }); } @@ -745,20 +749,24 @@ public void exampleClientComposition04(HttpClient client, FileSystem fileSystem) } public void exampleFollowRedirect01(HttpClient client) { - client.request(HttpMethod.GET, "some-uri", ar1 -> { - if (ar1.succeeded()) { - - HttpClientRequest request = ar1.result(); - request.setFollowRedirects(true); - request.send(ar2 -> { - if (ar2.succeeded()) { + client + .request(HttpMethod.GET, "some-uri") + .onComplete(ar1 -> { + if (ar1.succeeded()) { - HttpClientResponse response = ar2.result(); - System.out.println("Received response with status code " + response.statusCode()); - } - }); - } - }); + HttpClientRequest request = ar1.result(); + request.setFollowRedirects(true); + request + .send() + .onComplete(ar2 -> { + if (ar2.succeeded()) { + + HttpClientResponse response = ar2.result(); + System.out.println("Received response with status code " + response.statusCode()); + } + }); + } + }); } public void exampleFollowRedirect02(Vertx vertx) { @@ -767,20 +775,24 @@ public void exampleFollowRedirect02(Vertx vertx) { new HttpClientOptions() .setMaxRedirects(32)); - client.request(HttpMethod.GET, "some-uri", ar1 -> { - if (ar1.succeeded()) { - - HttpClientRequest request = ar1.result(); - request.setFollowRedirects(true); - request.send(ar2 -> { - if (ar2.succeeded()) { + client + .request(HttpMethod.GET, "some-uri") + .onComplete(ar1 -> { + if (ar1.succeeded()) { - HttpClientResponse response = ar2.result(); - System.out.println("Received response with status code " + response.statusCode()); - } - }); - } - }); + HttpClientRequest request = ar1.result(); + request.setFollowRedirects(true); + request + .send() + .onComplete(ar2 -> { + if (ar2.succeeded()) { + + HttpClientResponse response = ar2.result(); + System.out.println("Received response with status code " + response.statusCode()); + } + }); + } + }); } private String resolveURI(String base, String uriRef) { @@ -880,18 +892,20 @@ public void clientTunnel(HttpClient client) { .onSuccess(request -> { // Connect to the server - request.connect(ar -> { - if (ar.succeeded()) { - HttpClientResponse response = ar.result(); - - if (response.statusCode() != 200) { - // Connect failed for some reason - } else { - // Tunnel created, raw buffers are transmitted on the wire - NetSocket socket = response.netSocket(); + request + .connect() + .onComplete(ar -> { + if (ar.succeeded()) { + HttpClientResponse response = ar.result(); + + if (response.statusCode() != 200) { + // Connect failed for some reason + } else { + // Tunnel created, raw buffers are transmitted on the wire + NetSocket socket = response.netSocket(); + } } - } - }); + }); }); } @@ -952,12 +966,14 @@ public void example53(HttpServer server) { } public void example54(HttpClient client) { - client.webSocket("/some-uri", res -> { - if (res.succeeded()) { - WebSocket ws = res.result(); - System.out.println("Connected!"); - } - }); + client + .webSocket("/some-uri") + .onComplete(res -> { + if (res.succeeded()) { + WebSocket ws = res.result(); + System.out.println("Connected!"); + } + }); } public void exampleWebSocketDisableOriginHeader(HttpClient client, String host, int port, String requestUri) { @@ -966,12 +982,14 @@ public void exampleWebSocketDisableOriginHeader(HttpClient client, String host, .setPort(port) .setURI(requestUri) .setAllowOriginHeader(false); - client.webSocket(options, res -> { - if (res.succeeded()) { - WebSocket ws = res.result(); - System.out.println("Connected!"); - } - }); + client + .webSocket(options) + .onComplete(res -> { + if (res.succeeded()) { + WebSocket ws = res.result(); + System.out.println("Connected!"); + } + }); } public void exampleWebSocketSetOriginHeader(HttpClient client, String host, int port, String requestUri, String origin) { @@ -980,12 +998,14 @@ public void exampleWebSocketSetOriginHeader(HttpClient client, String host, int .setPort(port) .setURI(requestUri) .addHeader(HttpHeaders.ORIGIN, origin); - client.webSocket(options, res -> { - if (res.succeeded()) { - WebSocket ws = res.result(); - System.out.println("Connected!"); - } - }); + client + .webSocket(options) + .onComplete(res -> { + if (res.succeeded()) { + WebSocket ws = res.result(); + System.out.println("Connected!"); + } + }); } public void example55(WebSocket webSocket) { @@ -1084,17 +1104,21 @@ public void example60(Vertx vertx) { HttpClientOptions options = new HttpClientOptions() .setProxyOptions(new ProxyOptions().setType(ProxyType.HTTP)); HttpClient client = vertx.createHttpClient(options); - client.request(HttpMethod.GET, "ftp://ftp.gnu.org/gnu/", ar -> { - if (ar.succeeded()) { - HttpClientRequest request = ar.result(); - request.send(ar2 -> { - if (ar2.succeeded()) { - HttpClientResponse response = ar2.result(); - System.out.println("Received response with status code " + response.statusCode()); - } - }); - } - }); + client + .request(HttpMethod.GET, "ftp://ftp.gnu.org/gnu/") + .onComplete(ar -> { + if (ar.succeeded()) { + HttpClientRequest request = ar.result(); + request + .send() + .onComplete(ar2 -> { + if (ar2.succeeded()) { + HttpClientResponse response = ar2.result(); + System.out.println("Received response with status code " + response.statusCode()); + } + }); + } + }); } @@ -1121,19 +1145,24 @@ public void serversharing(Vertx vertx) { public void serversharingclient(Vertx vertx) { vertx.setPeriodic(100, (l) -> { - vertx.createHttpClient().request(HttpMethod.GET, 8080, "localhost", "/", ar1 -> { - if (ar1.succeeded()) { - HttpClientRequest request = ar1.result(); - request.send(ar2 -> { - if (ar2.succeeded()) { - HttpClientResponse resp = ar2.result(); - resp.bodyHandler(body -> { - System.out.println(body.toString("ISO-8859-1")); + vertx + .createHttpClient() + .request(HttpMethod.GET, 8080, "localhost", "/") + .onComplete(ar1 -> { + if (ar1.succeeded()) { + HttpClientRequest request = ar1.result(); + request + .send() + .onComplete(ar2 -> { + if (ar2.succeeded()) { + HttpClientResponse resp = ar2.result(); + resp.bodyHandler(body -> { + System.out.println(body.toString("ISO-8859-1")); + }); + } }); - } - }); - } - }); + } + }); }); } @@ -1144,21 +1173,25 @@ public void randomServersharing(Vertx vertx) { } public void setSSLPerRequest(HttpClient client) { - client.request(new RequestOptions() + client + .request(new RequestOptions() .setHost("localhost") .setPort(8080) .setURI("/") - .setSsl(true), ar1 -> { - if (ar1.succeeded()) { - HttpClientRequest request = ar1.result(); - request.send(ar2 -> { - if (ar2.succeeded()) { - HttpClientResponse response = ar2.result(); - System.out.println("Received response with status code " + response.statusCode()); - } - }); - } - }); + .setSsl(true)) + .onComplete(ar1 -> { + if (ar1.succeeded()) { + HttpClientRequest request = ar1.result(); + request + .send() + .onComplete(ar2 -> { + if (ar2.succeeded()) { + HttpClientResponse response = ar2.result(); + System.out.println("Received response with status code " + response.statusCode()); + } + }); + } + }); } public static void setIdentityContentEncodingHeader(HttpServerRequest request) { diff --git a/src/main/java/examples/NetExamples.java b/src/main/java/examples/NetExamples.java index f856b360cbf..2061c32d664 100755 --- a/src/main/java/examples/NetExamples.java +++ b/src/main/java/examples/NetExamples.java @@ -56,25 +56,29 @@ public void example4(Vertx vertx) { public void example5(Vertx vertx) { NetServer server = vertx.createNetServer(); - server.listen(1234, "localhost", res -> { - if (res.succeeded()) { - System.out.println("Server is now listening!"); - } else { - System.out.println("Failed to bind!"); - } - }); + server + .listen(1234, "localhost") + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("Server is now listening!"); + } else { + System.out.println("Failed to bind!"); + } + }); } public void example5_1(Vertx vertx) { NetServer server = vertx.createNetServer(); - server.listen(0, "localhost", res -> { - if (res.succeeded()) { - System.out.println("Server is now listening on actual port: " + server.actualPort()); - } else { - System.out.println("Failed to bind!"); - } - }); + server + .listen(0, "localhost") + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("Server is now listening on actual port: " + server.actualPort()); + } else { + System.out.println("Failed to bind!"); + } + }); } public void example6(Vertx vertx) { @@ -113,13 +117,15 @@ public void example8(NetSocket socket) { public void example9(NetServer server) { - server.close(res -> { - if (res.succeeded()) { - System.out.println("Server is now closed"); - } else { - System.out.println("close failed"); - } - }); + server + .close() + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("Server is now closed"); + } else { + System.out.println("close failed"); + } + }); } public void example9_1(NetSocket socket) { @@ -172,14 +178,16 @@ public void example15(Vertx vertx) { NetClientOptions options = new NetClientOptions().setConnectTimeout(10000); NetClient client = vertx.createNetClient(options); - client.connect(4321, "localhost", res -> { - if (res.succeeded()) { - System.out.println("Connected!"); - NetSocket socket = res.result(); - } else { - System.out.println("Failed to connect: " + res.cause().getMessage()); - } - }); + client + .connect(4321, "localhost") + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("Connected!"); + NetSocket socket = res.result(); + } else { + System.out.println("Failed to connect: " + res.cause().getMessage()); + } + }); } public void example16(Vertx vertx) { @@ -619,13 +627,15 @@ public void example48(Vertx vertx) throws CertificateException { .setTrustOptions(certificate.trustOptions()); NetClient client = vertx.createNetClient(clientOptions); - client.connect(1234, "localhost", ar -> { - if (ar.succeeded()) { - ar.result().handler(buffer -> System.out.println(buffer)); - } else { - System.err.println("Woops: " + ar.cause().getMessage()); - } - }); + client + .connect(1234, "localhost") + .onComplete(ar -> { + if (ar.succeeded()) { + ar.result().handler(buffer -> System.out.println(buffer)); + } else { + System.err.println("Woops: " + ar.cause().getMessage()); + } + }); } public void example49() { @@ -688,13 +698,15 @@ public void useSNIInClient(Vertx vertx, JksOptions trustOptions) { ); // Connect to 'localhost' and present 'server.name' server name - client.connect(1234, "localhost", "server.name", res -> { - if (res.succeeded()) { - System.out.println("Connected!"); - NetSocket socket = res.result(); - } else { - System.out.println("Failed to connect: " + res.cause().getMessage()); - } - }); + client + .connect(1234, "localhost", "server.name") + .onComplete(res -> { + if (res.succeeded()) { + System.out.println("Connected!"); + NetSocket socket = res.result(); + } else { + System.out.println("Failed to connect: " + res.cause().getMessage()); + } + }); } } diff --git a/src/main/java/examples/SharedDataExamples.java b/src/main/java/examples/SharedDataExamples.java index d57fab33223..c67e28dac96 100644 --- a/src/main/java/examples/SharedDataExamples.java +++ b/src/main/java/examples/SharedDataExamples.java @@ -45,121 +45,139 @@ public void localMap(Vertx vertx) { public void asyncMap(Vertx vertx) { SharedData sharedData = vertx.sharedData(); - sharedData.getAsyncMap("mymap", res -> { - if (res.succeeded()) { - AsyncMap map = res.result(); - } else { - // Something went wrong! - } - }); + sharedData. + getAsyncMap("mymap") + .onComplete(res -> { + if (res.succeeded()) { + AsyncMap map = res.result(); + } else { + // Something went wrong! + } + }); } public void localAsyncMap(Vertx vertx) { SharedData sharedData = vertx.sharedData(); - sharedData.getLocalAsyncMap("mymap", res -> { - if (res.succeeded()) { - // Local-only async map - AsyncMap map = res.result(); - } else { - // Something went wrong! - } - }); + sharedData. + getLocalAsyncMap("mymap") + .onComplete(res -> { + if (res.succeeded()) { + // Local-only async map + AsyncMap map = res.result(); + } else { + // Something went wrong! + } + }); } public void example3(AsyncMap map) { - map.put("foo", "bar", resPut -> { - if (resPut.succeeded()) { - // Successfully put the value - } else { - // Something went wrong! - } - }); + map + .put("foo", "bar") + .onComplete(resPut -> { + if (resPut.succeeded()) { + // Successfully put the value + } else { + // Something went wrong! + } + }); } public void example4(AsyncMap map) { - map.get("foo", resGet -> { - if (resGet.succeeded()) { - // Successfully got the value - Object val = resGet.result(); - } else { - // Something went wrong! - } - }); + map + .get("foo") + .onComplete(resGet -> { + if (resGet.succeeded()) { + // Successfully got the value + Object val = resGet.result(); + } else { + // Something went wrong! + } + }); } public void lock(Vertx vertx) { SharedData sharedData = vertx.sharedData(); - sharedData.getLock("mylock", res -> { - if (res.succeeded()) { - // Got the lock! - Lock lock = res.result(); + sharedData + .getLock("mylock") + .onComplete(res -> { + if (res.succeeded()) { + // Got the lock! + Lock lock = res.result(); - // 5 seconds later we release the lock so someone else can get it + // 5 seconds later we release the lock so someone else can get it - vertx.setTimer(5000, tid -> lock.release()); + vertx.setTimer(5000, tid -> lock.release()); - } else { - // Something went wrong - } - }); + } else { + // Something went wrong + } + }); } public void lockWithTimeout(Vertx vertx) { SharedData sharedData = vertx.sharedData(); - sharedData.getLockWithTimeout("mylock", 10000, res -> { - if (res.succeeded()) { - // Got the lock! - Lock lock = res.result(); - - } else { - // Failed to get lock - } - }); + sharedData + .getLockWithTimeout("mylock", 10000) + .onComplete(res -> { + if (res.succeeded()) { + // Got the lock! + Lock lock = res.result(); + + } else { + // Failed to get lock + } + }); } public void localLock(Vertx vertx) { SharedData sharedData = vertx.sharedData(); - sharedData.getLocalLock("mylock", res -> { - if (res.succeeded()) { - // Local-only lock - Lock lock = res.result(); + sharedData + .getLocalLock("mylock") + .onComplete(res -> { + if (res.succeeded()) { + // Local-only lock + Lock lock = res.result(); - // 5 seconds later we release the lock so someone else can get it + // 5 seconds later we release the lock so someone else can get it - vertx.setTimer(5000, tid -> lock.release()); + vertx.setTimer(5000, tid -> lock.release()); - } else { - // Something went wrong - } - }); + } else { + // Something went wrong + } + }); } public void counter(Vertx vertx) { SharedData sharedData = vertx.sharedData(); - sharedData.getCounter("mycounter", res -> { - if (res.succeeded()) { - Counter counter = res.result(); - } else { - // Something went wrong! - } - }); + sharedData + .getCounter("mycounter") + .onComplete(res -> { + if (res.succeeded()) { + Counter counter = res.result(); + } else { + // Something went wrong! + } + }); } public void localCounter(Vertx vertx) { SharedData sharedData = vertx.sharedData(); - sharedData.getLocalCounter("mycounter", res -> { - if (res.succeeded()) { - // Local-only counter - Counter counter = res.result(); - } else { - // Something went wrong! - } - }); + sharedData + .getLocalCounter("mycounter") + .onComplete(res -> { + if (res.succeeded()) { + // Local-only counter + Counter counter = res.result(); + } else { + // Something went wrong! + } + }); } } diff --git a/src/main/java/examples/StreamsExamples.java b/src/main/java/examples/StreamsExamples.java index 3de17b92276..4911e08f565 100644 --- a/src/main/java/examples/StreamsExamples.java +++ b/src/main/java/examples/StreamsExamples.java @@ -101,13 +101,15 @@ public void pipe6(NetServer server) { server.connectHandler(sock -> { // Pipe the socket providing an handler to be notified of the result - sock.pipeTo(sock, ar -> { - if (ar.succeeded()) { - System.out.println("Pipe succeeded"); - } else { - System.out.println("Pipe failed"); - } - }); + sock + .pipeTo(sock) + .onComplete(ar -> { + if (ar.succeeded()) { + System.out.println("Pipe succeeded"); + } else { + System.out.println("Pipe failed"); + } + }); }).listen(); } @@ -118,15 +120,16 @@ public void pipe7(NetServer server, FileSystem fs) { Pipe pipe = sock.pipe(); // Open a destination file - fs.open("/path/to/file", new OpenOptions(), ar -> { - if (ar.succeeded()) { - AsyncFile file = ar.result(); - - // Pipe the socket to the file and close the file at the end - pipe.to(file); - } else { - sock.close(); - } + fs.open("/path/to/file", new OpenOptions()) + .onComplete(ar -> { + if (ar.succeeded()) { + AsyncFile file = ar.result(); + + // Pipe the socket to the file and close the file at the end + pipe.to(file); + } else { + sock.close(); + } }); }).listen(); } @@ -139,29 +142,31 @@ public void pipe8(Vertx vertx, FileSystem fs) { Pipe pipe = request.pipe(); // Open a destination file - fs.open("/path/to/file", new OpenOptions(), ar -> { - if (ar.succeeded()) { - AsyncFile file = ar.result(); - - // Pipe the socket to the file and close the file at the end - pipe.to(file); - } else { - // Close the pipe and resume the request, the body buffers will be discarded - pipe.close(); - - // Send an error response - request.response().setStatusCode(500).end(); - } - }); + fs.open("/path/to/file", new OpenOptions()) + .onComplete(ar -> { + if (ar.succeeded()) { + AsyncFile file = ar.result(); + + // Pipe the socket to the file and close the file at the end + pipe.to(file); + } else { + // Close the pipe and resume the request, the body buffers will be discarded + pipe.close(); + + // Send an error response + request.response().setStatusCode(500).end(); + } + }); }).listen(8080); } public void pipe9(AsyncFile src, AsyncFile dst) { src.pipe() .endOnSuccess(false) - .to(dst, rs -> { + .to(dst) + .onComplete(rs -> { // Append some text and close the file dst.end(Buffer.buffer("done")); - }); + }); } }