create akka actor Props - Java Message

Java examples for Message:AKKA

Description

create akka actor Props

Demo Code



import akka.actor.UntypedActor;
import akka.actor.ActorSystem;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.event.LoggingAdapter;
import akka.event.Logging;
import akka.japi.Creator;

public class PropsActorDemo {
    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("sys");
        ActorRef actorRef = system.actorOf(PropsActor.createProps(), "props_actor");
        /*from   w  w w .j  a  v a2  s . c  o  m*/
        for (int i = 0; i < 10; i++) {
          actorRef.tell("Hello Akka", ActorRef.noSender());
        }
    }
}

class PropsActor extends UntypedActor {
    private LoggingAdapter log = Logging.getLogger(this.getContext().system(), this);

    @Override
    public void onReceive(Object message) throws Exception {
        if (message instanceof String) {
            log.info(message.toString());
        } else {
            unhandled(message);
        }
    }


    public static Props createProps() {
        return Props.create(new Creator<PropsActor>() {
            public PropsActor create() throws Exception {
                return new PropsActor();
            }
        });
    }
}

Related Tutorials