Reputation: 754
guys!
While using Vert.x, I found I cannot get the result out of EventBus while communicating with other Verticle.
class Result<T> {
public T value;
public String message;
// ...
}
private Result<Integer> someMethod() {
try {
var json = new JsonObject();
vertx.eventBus().<JsonObject>send(Service.EVENT_BUS_ADDRESS, json, ar -> {
if (ar.succeeded()) {
var result = new Result<List>(ar.result().body());
if (result.isSuccessful()) {
Result.succeed(result);
} else {
Result.fail(result.message);
}
} else {
Result.fail("Remote server error");
}
});
} catch (Exception e) {
e.printStackTrace();
return Result.fail(e.getMessage());
}
return // how can I return the result in EventBus???
}
So how can I get the value out of the Async
block and return it?
Upvotes: 0
Views: 1193
Reputation: 11132
You shouldn't return the result but notify a handler instead
The following code assumes your Result.succeeed
or Result.fail
method return
an instance of Result
private void someMethod(Handler<Result> resultHandler) {
...
vertx.eventBus().<JsonObject>send(Service.EVENT_BUS_ADDRESS, json, ar -> {
if (ar.succeeded()) {
var result = new Result<List>(ar.result().body());
if (result.isSuccessful()) {
resultHandler.handle(Result.succeed(result));
} else {
resultHandler.handle(Result.fail(result.message));
}
} else {
resultHandler.handle(Result.fail("Remote server error"));
}
...
}
Upvotes: 2