Skip to content

Instantly share code, notes, and snippets.

@vaclav
Created November 5, 2010 05:58
Show Gist options
  • Save vaclav/663708 to your computer and use it in GitHub Desktop.
Save vaclav/663708 to your computer and use it in GitHub Desktop.
Using GPars actors in Java
import groovy.lang.Closure;
import groovyx.gpars.actor.DynamicDispatchActor;
public class StatelessActorDemo {
public static void main(String[] args) throws InterruptedException {
final MyStatelessActor actor = new MyStatelessActor();
actor.start();
actor.send("Hello");
actor.sendAndWait(10);
actor.sendAndContinue(10.0, new Closure(null) {
@Override public Object call(final Object arguments) {
System.out.println("Received a reply " + arguments);
return null;
}
});
}
}
class MyStatelessActor extends DynamicDispatchActor {
public void onMessage(final String msg) {
System.out.println("Received " + msg);
replyIfExists("Thank you");
}
public void onMessage(final Integer msg) {
System.out.println("Received a number " + msg);
replyIfExists("Thank you");
}
public void onMessage(final Object msg) {
System.out.println("Received an object " + msg);
replyIfExists("Thank you");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment