Sync response from Actor

16 views
Skip to first unread message

Yair Ogen

unread,
Oct 5, 2014, 4:28:11 AM10/5/14
to jumi-tes...@googlegroups.com
I have an API that is synchronous - i.e. it need to return a result.

This API needs to run code that is not thread safe - i.e. must run in a single thread.

I'd like to bind once Actor to one Thread and have this actor serve all requests. Yes, I know I will create a bottleneck but this is what I need.

I couldn't find in Jumi how can I gt a sync response from an Actor. Only with callbacks.

Is there a solution to my problem?

Esko Luontola

unread,
Oct 6, 2014, 12:54:26 PM10/6/14
to jumi-tes...@googlegroups.com
There isn't yet support for synchronous responses, but there is a workaround: send the actor a CompletableFuture or similar as method parameter.

Then the caller code would be:

Future<String> f = new CompletableFuture<>();
actor.doSomething(f);
String result = f.get();

And the actor would be:

public void doSomething(CompletableFuture<String> result) {
    result.complete("the result");
}

I might later improve jumi-actors to support that with less boilerplate (so that you can return a Future from the actor method).
Reply all
Reply to author
Forward
0 new messages