Skip to content

Instantly share code, notes, and snippets.

View teivah's full-sized avatar
Building my newsletter: The Coder Cafe

Teiva Harsanyi teivah

Building my newsletter: The Coder Cafe
View GitHub Profile
vertx.executeBlocking(future -> {
//Blocking part
future.complete();
} , res -> {
if (res.succeeded()) {
//Success
} else {
//Failure
}
});
vertx.executeBlocking(future -> {
byte[] bytes = parseLargeFile(filename);
// Complete the future with the generated objects in the
// blocking method
future.complete(bytes);
}
res -> {
if (res.succeeded()) {
byte[] bytes = (byte[]) res.result();
response.putHeader("Content-Type", "application/octet-stream");
response.setChunked(true);
response.write(Buffer.buffer(bytes));
response.setStatusCode(200);
response.end();
} else {
response.setStatusCode(500);
// Read file method
private Future<String> readFile() {
Future<String> future = Future.future();
// Retrieve a FileSystem object from vertx instance and call the
// non-blocking readFile method
vertx.fileSystem().readFile("src/main/resources/example03/read.txt", handler -> {
if (handler.succeeded()) {
System.out.println("Read content: " + handler.result());
future.complete("read success");
// Orchestration based on setHandler
private void method1() {
// Create first step
Future<String> future1 = readFile();
// Set handler on future1
future1.setHandler(res1 -> {
if (res1.succeeded()) {
Future<String> future2 = writeFile(res1.result());
Future<String> future1 = readFile();
future.setHandler(res -> {
if (res.succeeded()) {
// If the future succeeded
Object o = res.result();
// Do something...
} else {
// If the future failed
}
}
future.setHandler(res -> {
if (res.succeeded()) {
// If the future succeeded
Object o = res.result();
// Do something...
} else {
// If the future failed
}
}
// Orchestration based on compose
private void method2() {
// Create first step
Future<String> future1 = readFile();
// Define future1 composition
future1.compose(s1 -> {
Future<String> future2 = writeFile(future1.result());
// Define future2 composition
future.compose(res -> {
// If the future succeeded
Object o = res.result;
//Do something
}, Future.future().setHandler(handler -> {
// If the future failed
}));