class Adapter {
void getResult(String payload, Handler<String> callback) {
eventBus.send(
"address",
new JsonObject().putString("payload", payload),
new Handler<Message<JsonObject>>() {
public void handle(Message<JsonObject> msg) {
callback.handle(msg.body.getString("result"));
}
}
);
}
}
String result;
doSomethingBefore();
result = adapterInstance.getResult("payload data here");
doThis(result);
doThat(result);
doTheOther(result);
doSomethingBefore();
adapterInstance.getResult("payload data here", new Handler<String>() {
public void handle(String result) {
doThis(result);
doThat(result);
doTheOther(result);
}
});But I believe the answer to your question is: If Adapter#getResult() is called from an event loop thread, you *can't* implementwaitForResult without blocking the event loop, which isn't allowed. Instead, your options are:
final Queue q=new BlockingQueue();
vertx.eventBus().send("addr", new JsonObject(…),
handler {
q.put(data);
}
);
result=q.take();
@blablor maybe it will be more good:final Queue q=new BlockingQueue();
vertx.eventBus().send("addr", new JsonObject(…),
handler {
q.put(data);
}
);
result=q.take();
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To post to this group, send an email to ve...@googlegroups.com.
To unsubscribe from this group, send email to vertx+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/vertx?hl=en-GB.
On Sep 17, 2012, at 11:00 PM, Joy <a.pec...@gmail.com> wrote:
@blablor maybe it will be more good:
final�Queue�q=new�BlockingQueue();
vertx.eventBus().send("addr",�new�JsonObject(�),�
handler�{
q.put(data);
}
);
result=q.take();
Much better, Joy. �I was just trying to whack something together from memory, but that's much more elegant. �Still not "vertxian", but probably workable. :-)
That just blocks on q.take().
// Parallel execution
Future<String> fs=call_async_service("service1",data);
Future<String> fs2=call_async_service("service2",data);
// serialise next service call on futures
Future<Integer> res=call_async_service("service3",fs.get(),fs2.get());
do something with res.get()
class FutureImpl<T> implements Future<T>{
...
@Override
public T get() throws InterruptedException, ExecutionException {
return (T) suspend();
}
...
}
Future<String> fs=call_async_service("service1",data);
Future<String> fs2=call_async_service("service2",data);
not good example :(, better this:
Future<String> fs=eventbus.send("service1",data);
Future<String> fs2=eventbus.send("service2",data);
// serialise next service call on futures
Future<Integer> res=eventbus.send("service3",fs.get(),fs2.get());
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To view this discussion on the web, visit https://groups.google.com/d/msg/vertx/-/YkYVbMuVqkQJ.
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/6ee7678e-1ca4-4c74-9c19-ae8c76886ef2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
e.g in rather loose pseudo code ...
do(a)
.thenInParallel(b, c)
.then(d)
Where a, b, c, d are all async operations, with the results of one stage being passed to the next.
The point here being that the execution flow is a graph and not classically linear.
This type of composition is more common than one would at first imagine. It even forms the basis of some libraries (e.g. Finagle).
To implement it over vert.x's constructs (Handler, AsyncResult) some libraries that perhaps could help (in addition to those mentioned on the thread):
RXJava (but then everything is a stream..)
JDefered
JDK's CompletableFuture
regards
Fuzz
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/c4f2d155-764e-464f-ae39-04f8422ea5da%40googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/7f5c3636-082e-4682-90f4-3eb8b176a90a%40googlegroups.com.
Vertx vertx = Vertx.vertx();
Handler<Long> third = l -> {
System.out.println("Last operation");
};
AtomicInteger counter = new AtomicInteger(0);
Handler<Long> second = l -> {
System.out.println("In parallel operations");
if (counter.incrementAndGet() == 2) {
vertx.setTimer(1, third);
System.out.println("Parallel operations complete");
}
};
Handler<Long> first = l -> {
vertx.setTimer(1, second);
vertx.setTimer(1, second);
System.out.println("First operation complete");
};
vertx.setTimer(1, first);
Sketched up this API / implementation to show the intention ...