Created
August 3, 2022 04:06
-
-
Save Stwissel/e079b2d688535f7db7eb2fec7245373a to your computer and use it in GitHub Desktop.
Sidestart vert.x in its own thread and call an async operation in a sync block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.notessensei.legacy.sync_async; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.atomic.AtomicBoolean; | |
import io.vertx.core.AbstractVerticle; | |
import io.vertx.core.Future; | |
import io.vertx.core.Promise; | |
import io.vertx.core.Vertx; | |
import io.vertx.core.eventbus.Message; | |
/** | |
* Sample how to connect a sync call to an async call | |
* using the Vert.x web client as example | |
* | |
* @author stw | |
*/ | |
public class Starter extends AbstractVerticle { | |
/** | |
* @param args | |
* @throws InterruptedException | |
*/ | |
public static void main(final String[] args) throws InterruptedException { | |
System.out.println("About to start"); | |
final Starter starter = new Starter(); | |
starter.warmup(); | |
System.out.println("Done!"); | |
} | |
Vertx vertx; | |
String verticleId; | |
Thread vertxThread; | |
AtomicBoolean vertxDeployed = new AtomicBoolean(false); | |
Future<String> callTheOtherSide(final String message) { | |
System.out.println("Start callTheOtherSide Promise"); | |
final Promise<String> promise = Promise.promise(); | |
this.vertx.eventBus().request("webDemo", message) | |
.onFailure(promise::fail) | |
.onSuccess(msg -> { | |
System.out.println("Start callTheOtherSide Success"); | |
promise.complete(String.valueOf(msg.body())); | |
System.out.println("End callTheOtherSide Success"); | |
}); | |
System.out.println("End callTheOtherSide Promise"); | |
return promise.future(); | |
} | |
void handleMessage(final Message<String> message) { | |
System.out.println("Start handleMessage"); | |
message.reply("You said: " + message.body()); | |
System.out.println("End handleMessage"); | |
} | |
@Override | |
public void start(final Promise<Void> startPromise) throws Exception { | |
System.out.println("Verticle start - start"); | |
this.getVertx().eventBus().consumer("webDemo", this::handleMessage); | |
startPromise.complete(); | |
System.out.println("Verticle start - done"); | |
} | |
@Override | |
public void stop(final Promise<Void> stopPromise) throws Exception { | |
System.out.println("Verticle stop - start"); | |
stopPromise.complete(); | |
System.out.println("Verticle stop - done"); | |
} | |
/** | |
* This is the synchronous function that needs to call | |
* out to something async | |
* | |
* @param verticleId | |
* @throws TimeoutException | |
* @throws ExecutionException | |
* @throws InterruptedException | |
*/ | |
void theRealAction(final String messagePayload) { | |
System.out.println("Start theRealAction"); | |
try { | |
System.out.println("Before calling the other side"); | |
final String result = this.callTheOtherSide(messagePayload) | |
.toCompletionStage() | |
.toCompletableFuture().get(); | |
System.out.println("After calling the other side, reply: " + result); | |
} catch (final InterruptedException e) { | |
e.printStackTrace(); | |
} catch (final ExecutionException e) { | |
e.printStackTrace(); | |
} | |
this.vertx.undeploy(this.verticleId) | |
.onComplete(handler -> { | |
if (handler.failed()) { | |
System.err.print(handler.cause()); | |
} | |
System.out.println("About to close Vert.x"); | |
this.vertx.close() | |
.onFailure(System.err::println) | |
.onSuccess(v -> System.out.println("Vert.x gone")); | |
}); | |
System.out.println("End theRealAction"); | |
} | |
void warmup() throws InterruptedException { | |
System.out.println("Start warmup"); | |
final Runnable vertxRunner = () -> { | |
System.out.println("Start vertxRunner"); | |
this.vertx = Vertx.vertx(); | |
this.vertx.deployVerticle(this) | |
.onSuccess(id -> { | |
System.out.println("Start success vertxRunner"); | |
this.verticleId = id; | |
synchronized (this.vertxDeployed) { | |
this.vertxDeployed.set(true); | |
this.vertxDeployed.notify(); | |
} | |
System.out.println("End success vertxRunner"); | |
}) | |
.onFailure(System.err::println); | |
System.out.println("End vertxRunner"); | |
}; | |
this.vertxThread = new Thread(vertxRunner); | |
this.vertxThread.start(); | |
System.out.println("After vertxRunner"); | |
while (!this.vertxDeployed.get()) { | |
System.out.println("Start Inside while block"); | |
synchronized (this.vertxDeployed) { | |
this.vertxDeployed.wait(); | |
} | |
System.out.println("End Inside while block"); | |
} | |
System.out.println("After while Block"); | |
this.theRealAction("Hello world"); | |
System.out.println("End warmup"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment