Created
July 27, 2013 10:07
-
-
Save ankurpshah/6094456 to your computer and use it in GitHub Desktop.
Akka Actor based concurrency example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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