Created
May 1, 2016 15:26
-
-
Save RakeshChouhan/563a0ae70d102ca8f916ca436b62ddcf to your computer and use it in GitHub Desktop.
AKKA sample to demonstrate the message communication between actors using Inbox.
This file contains 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 com.akkasample.inbox; | |
import java.util.concurrent.TimeUnit; | |
import com.akkasample.actor.InboxSampleActor; | |
import com.akkasample.bean.Greeting; | |
import akka.actor.ActorRef; | |
import akka.actor.ActorSystem; | |
import akka.actor.Inbox; | |
import akka.actor.Props; | |
import scala.concurrent.duration.FiniteDuration; | |
/** | |
* Actor call from outside the system | |
* @author Rakesh | |
* | |
*/ | |
public class ActorInboxMain { | |
/** | |
* Main program | |
* @param args | |
*/ | |
public static void main(String args[]){ | |
ActorSystem system = ActorSystem.create("SampleActorSystem"); | |
Inbox inbox = Inbox.create(system); // create Inbox using AKKA system. | |
ActorRef ref = system.actorOf(Props.create(InboxSampleActor.class), "InboxSampleActor"); | |
inbox.send(ref, new Greeting("Hello From Inbox")); // Pass message to Actor | |
System.out.println("Message Received from the Actor :: "+inbox.receive(FiniteDuration.create(5, TimeUnit.SECONDS))); // prints the reply from the actor | |
} | |
} |
This file contains 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 com.akkasample.bean; | |
import java.io.Serializable; | |
/** | |
* @author Rakesh | |
* | |
*/ | |
public class Greeting implements Serializable { | |
/** | |
* Serial Version ID | |
*/ | |
private static final long serialVersionUID = 1575217482200420484L; | |
private final String message; | |
public Greeting(String message) { | |
this.message = message; | |
} | |
/** | |
* Method to get the Message | |
* @return | |
*/ | |
public String getMessage() { | |
return message; | |
} | |
} |
This file contains 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 com.akkasample.actor; | |
import com.akkasample.bean.Greeting; | |
import akka.actor.UntypedActor; | |
/** | |
* Akka actor which recieves the message and prints it. | |
* @author Rakesh | |
* | |
*/ | |
public class InboxSampleActor extends UntypedActor { | |
/* (non-Javadoc) | |
* @see akka.actor.UntypedActor#onReceive(java.lang.Object) | |
*/ | |
public void onReceive(Object object) throws Exception { | |
if(object instanceof Greeting){ | |
String message = ((Greeting)object).getMessage(); | |
System.out.println("Got Message Inside Actor :: " + message); // message given by the main program | |
getSender().tell("Greetings from the Actor", getSelf()); // Reply to the main program (Sender) | |
}else{ | |
unhandled(object); // The AKKA system will handle this message. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment