Skip to content

Instantly share code, notes, and snippets.

@RakeshChouhan
Created April 30, 2016 16:53
Show Gist options
  • Save RakeshChouhan/de1e1d915590cca550712bbfd94b9f4b to your computer and use it in GitHub Desktop.
Save RakeshChouhan/de1e1d915590cca550712bbfd94b9f4b to your computer and use it in GitHub Desktop.
Main program with the AKKA.
/**
*
*/
package com.akkasample;
import com.akkasample.actor.MessageActor;
import com.akkasample.bean.Greeting;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
/**
* @author Rakesh
*
*/
public class AkkaSampleMain {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args){
// Create Actor System
ActorSystem system = ActorSystem.create("SampleActorSystem");
//Create Actor in the system
ActorRef ref = system.actorOf(Props.create(MessageActor.class),"MessageActor");
// Pass message to Actor
ref.tell(new Greeting("Hello Akka!!! "),ActorRef.noSender());
//Get Actor reference using the actor path in the system
ActorSelection existingActor = system.actorSelection("/user/MessageActor");
existingActor.tell(new Greeting("Message to existing actor"), ActorRef.noSender());
}
}
/**
*
*/
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;
}
}
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 MessageActor extends UntypedActor {
/* (non-Javadoc)
* @see akka.actor.UntypedActor#onReceive(java.lang.Object)
*/
public void onReceive(Object object) throws Exception {
if(object instanceof Greeting){
System.out.println("Got Message ::" + ((Greeting)object).getMessage());
}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