java的akka测试

简单的消息框架吧,这样用的话

package com;

import akka.actor.UntypedActor;

public class HelloActor extends UntypedActor {
    public static final String KEY = "HelloActor";

    @Override
    public void onReceive(Object arg0) throws Exception {
        System.out.println(" HelloActor ======>" + arg0);

        ActorMgr.getDefault().tell(WorldActor.KEY, "====>> gogo");

    }

}
package com;

import akka.actor.UntypedActor;

public class WorldActor extends UntypedActor {

    public static final String KEY = "WorldActor";

    @Override
    public void onReceive(Object arg0) throws Exception {
        System.out.println(" WorldActor ======>" + arg0);
    }

}

package com;

import java.util.concurrent.ConcurrentHashMap;

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

public class ActorMgr {

    private static final ActorMgr _instance = new ActorMgr();

    private static ConcurrentHashMap kv = new ConcurrentHashMap<>();

    public static ActorMgr getDefault(){
        return _instance;
    }

    private ActorSystem sys;
    public void start(){
        sys = ActorSystem.create("ActorMgr");
    }

    public ActorRef actorOf(String key, Class clazz){
        ActorRef value = sys.actorOf(Props.create(clazz));
        kv.put(key, value);
        return value;
    }

    public boolean tell(String key, Object msg){
        ActorRef val = kv.get(key);
        if (val == null)
            return false;
        val.tell(msg, val);
        return true;
    }

    public void shutdown(){
        sys.shutdown();
    }
}

package com;

import akka.actor.ActorRef;

public class Main {

    public static void main(String[] args) {

        ActorMgr m = ActorMgr.getDefault();
        m.start();

        ActorRef a1 = m.actorOf(HelloActor.KEY, HelloActor.class);
        ActorRef a2 = m.actorOf(WorldActor.KEY, WorldActor.class);

        a1.tell("gogo", a1);

        m.shutdown();
    }
}

你可能感兴趣的:(游戏设计,网络通讯)