通过Typesafe Activator创建akka java sample

 通过Typesafe Activator创建akka java sample

What is Typesafe Activator?

Typesafe Activator gets you started with the Typesafe Reactive Platform, including Akka, Play Framework and Scala. It is a hub for developers that want to build reactive applications, providing an in browser environment for creating new applications. 

首先安装Typesafe Activator,然后启动它,如下所示,找到该模板,创建项目,

通过Typesafe Activator创建akka java sample_第1张图片


根据这个例子,为了简单,做了一些修改。

实现HelloWorld的actor,这个actor的作用就是发出问候消息,并且接收MSG.DONE的消息,处理之,如下,

package sample.hello;

import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.actor.ActorRef;

public class HelloWorld extends UntypedActor {

    /**
     * Is called when an Actor is started.
     */
    @Override
    public void preStart() {
        // create the greeter actor
        final ActorRef greeter = getContext().actorOf(Props.create(Greeter.class), "greeter");
        // 把Greeter.Msg.GREET这个消息发送给给名字为greeter的actor,发送者是self
        //getSelf() 是一个sender
        greeter.tell(Greeter.Msg.GREET, getSelf());
    }

    @Override
    public void onReceive(Object msg) {
        if (msg == Greeter.Msg.DONE) {
            // when the greeter is done, stop this actor and with it the application
            getContext().stop(getSelf());
        } else
            unhandled(msg);
    }
}


然后相应的Greeter的actor接收问候消息,然后打印,然后向其消息的发送者发送DONE的消息,如下,

package sample.hello;

import akka.actor.UntypedActor;

public class Greeter extends UntypedActor {

    public static enum Msg {
        GREET, DONE;
    }

    @Override
    public void onReceive(Object msg) {
        if (msg == Msg.GREET) {
            //得到Msg.GREET消息,打印
            System.out.println("Hello World!");
            // 得到当前消息的发送者,并告诉这个发送者消息应经处理完成,
            // 此时Msg.DONE的发送者是self
            getSender().tell(Msg.DONE, getSelf());
        } else
            unhandled(msg);
    }

}


这是两个actor的交互行为,在引入第三个actor,负责监视HelloWorld的actor,当其发出Terminated的消息时,这个负责监视的actor会处理之,

如下,

package sample.hello;

import akka.actor.ActorRef;
import akka.actor.Terminated;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;

public class Terminator extends UntypedActor {

    private final LoggingAdapter log = Logging.getLogger(getContext().system(), this);
    private final ActorRef ref;

    /**
     * 此时的Terminator actor要观察另一个actor ref
     *
     * @param ref
     */
    public Terminator(ActorRef ref) {
        this.ref = ref;
        //Registers this actor as a Monitor-监视 for the provided ActorRef.
        getContext().watch(ref);
    }

    @Override
    public void onReceive(Object msg) {
        if (msg instanceof Terminated) {
            log.info("||||||||{} has terminated, shutting down system", ref.path());
            getContext().system().shutdown();
        } else {
            unhandled(msg);
        }
    }

}


下面就是启动类,如下,

package sample.hello;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class Main2 {

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("Hello");
        //actorOf--Create new actor as child of this context with the given name
        //actor 的名字是helloWorld
        /**
         * 当创建helloWorld的actor时,会通过preStart创建一个greeter的actor,
         * 然后greeter.tell(Greeter.Msg.GREET, getSelf());
         */
        ActorRef a = system.actorOf(Props.create(HelloWorld.class), "helloWorld");
        //actorOf--Create new actor as child of this context with the given name
        //actor 的名字是terminator
        system.actorOf(Props.create(Terminator.class, a), "terminator");
    }
    
}

=======================END=======================

你可能感兴趣的:(通过Typesafe Activator创建akka java sample)