經過Typesafe Activator建立akka java sample

 經過Typesafe Activator建立akka java samplejava

What is Typesafe Activator?react

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. app

首先安裝Typesafe Activator,而後啓動它,以下所示,找到該模板,建立項目,ide


根據這個例子,爲了簡單,作了一些修改。ui

實現HelloWorld的actor,這個actor的做用就是發出問候消息,而且接收MSG.DONE的消息,處理之,以下,this

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的消息,以下,spa

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會處理之,code

以下,orm

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);
        }
    }

}


下面就是啓動類,以下,
get

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=======================

相關文章
相關標籤/搜索