How to forward message in typed actor?

18 views
Skip to first unread message

draco

unread,
Sep 26, 2011, 9:18:06 PM9/26/11
to Akka User List
I've writen a test class as following:

public class Test {
interface Api {
public int doWork(String worker);
}

interface Worker {
public int work();
}

public static class Worker1Impl extends akka.actor.TypedActor
implements Worker {
@Override
public int work() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("work1 : " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return 1;
}
}

public static class Worker2Impl extends akka.actor.TypedActor
implements Worker {
@Override
public int work() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("work2 : " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return 2;
}
}

public static class ApiImpl extends akka.actor.TypedActor
implements Api {
Worker work1 = (Worker) TypedActor.newInstance(Worker.class,
Worker1Impl.class, 20000);
Worker work2 = (Worker) TypedActor.newInstance(Worker.class,
Worker2Impl.class, 20000);

@Override
public int doWork(String worker) {
if(worker.equals("worker1")){
return work1.work();
}else{
return work2.work();
}
}
}

public static void main(String[] args) {
Api api = (Api) TypedActor.newInstance(Api.class,
ApiImpl.class, 20000);
api.doWork("worker1");
api.doWork("worker2");
}
}
The result is:

work1 : 0
work1 : 1
work1 : 2
work1 : 3
work1 : 4
work2 : 0
work2 : 1
work2 : 2
work2 : 3
work2 : 4

This is reasonable because actors do theirs jobs in mailbox one by
one. So until worker1 returns the api actor won't send the request to
worker2.
But which I need is that the api actor works like a dispatcher/
replicator so it can forward the request to workers and handle next
message.
I've noticed untyped actor has a "forward" feature , is this possible
for typed actor?



draco

unread,
Sep 26, 2011, 9:34:40 PM9/26/11
to Akka User List
Actually in my current project, the api actor is a remote actor.

√iktor Ҡlang

unread,
Sep 27, 2011, 3:24:41 AM9/27/11
to akka...@googlegroups.com
Hi,


How would that be implemented?

Cheers,

 



--
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To post to this group, send email to akka...@googlegroups.com.
To unsubscribe from this group, send email to akka-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/akka-user?hl=en.




--
Viktor Klang

Akka Tech Lead
Typesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang

draco

unread,
Sep 27, 2011, 3:39:35 AM9/27/11
to Akka User List
We are using remote typed actor export our api to clients. But the api
implement actor become a bottle neck
because it process request one by one.
> Typesafe <http://www.typesafe.com/> - Enterprise-Grade Scala from the
> Experts
>
> Twitter: @viktorklang

√iktor Ҡlang

unread,
Sep 27, 2011, 4:26:53 AM9/27/11
to akka...@googlegroups.com
On Tue, Sep 27, 2011 at 9:39 AM, draco <allen...@gmail.com> wrote:
We are using remote typed actor export our api to clients. But the api
implement actor become a bottle neck
because it process request one by one.

Use either void-returning methods or Future-returning methods, then you can call subsequent typed actors and return their futures.

Cheers,

 



--
Typesafe - Enterprise-Grade Scala from the Experts

Twitter: @viktorklang

Reply all
Reply to author
Forward
0 new messages