Skip to content

Instantly share code, notes, and snippets.

@ankurpshah
Created July 27, 2013 10:07
Show Gist options
  • Save ankurpshah/6094456 to your computer and use it in GitHub Desktop.
Save ankurpshah/6094456 to your computer and use it in GitHub Desktop.
Akka Actor based concurrency example
package actors;
import akka.actor.*;
import static akka.actor.Actors.*;
import java.util.*;
public class AkkaActorExample {
// server code
static class MemoryActor extends UntypedActor {
final Map<String,Date> seen = new HashMap<String,Date>();
}
public void onReceive(Object messageObject) {
String message = messageObject.toString(); // simplifying assumption
if (message.equals("DUMP")) {
getContext().replySafe(seen.toString());
} else {
Date date = new Date();
seen.put(message.toString(), date);
getContext().replySafe("'" + message + "' recorded at " + date);
}
}
public static void main(String[] args) {
ActorRef remActor = actorOf(MemoryActor.class).start();
for (String arg: args) {
// client code
Object response = remActor.sendRequestReply(arg);
System.out.println("Reply received: "+response);
}
Object response = remActor.sendRequestReply("DUMP");
System.out.println("Dump of remembered strings: "+response);
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment