通過Typesafe Activator建立akka java sample

五柳-先生發表於2015-05-30
通過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,然後啟動它,如下所示,找到該模板,建立專案,


根據這個例子,為了簡單,做了一些修改。

實現HelloWorld的actor,這個actor的作用就是發出問候訊息,並且接收MSG.DONE的訊息,處理之,如下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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的訊息,如下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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會處理之,如下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
        }
    }
 
}


下面就是啟動類,如下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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========================================

from http://my.oschina.net/xinxingegeya/blog/364720


相關文章