2017-06-01 27 views
0

我們曾經有過如何在阿卡2.0創建的演員的道具與路由器在阿卡2.5

List<String> routeePaths = actorRefs.stream() 
        .map(e -> e.path().toString()) 
        .collect(toList()); 

Props routerProps = Props.empty().withRouter(new BroadcastRouter(routeePaths)); 

但在阿卡2.5,BroadcastRouter和其他一些老路由器是不存在了。什麼是將這些路由行爲添加到actor的正確方法。

回答

1

Akka 2.5有BroadcastRoutingLogic。下面的實施例是從documentation

Router router; 
{ 
    List<Routee> routees = new ArrayList<Routee>(); 
    for (int i = 0; i < 5; i++) { 
    ActorRef r = getContext().actorOf(Props.create(Worker.class)); 
    getContext().watch(r); 
    routees.add(new ActorRefRoutee(r)); 
    } 
    router = new Router(new BroadcastRoutingLogic(), routees); 
} 

也可以使用一個BroadcastPoolBroadcastGroup

BroadcastPool在配置中定義:

ActorRef router14 = getContext().actorOf(new BroadcastPool(5).props(Props.create(Worker.class)), "router14"); 

akka.actor.deployment { 
    /parent/router13 { 
    router = broadcast-pool 
    nr-of-instances = 5 
    } 
} 

ActorRef router13 = getContext().actorOf(FromConfig.getInstance().props(Props.create(Worker.class)), "router13"); 

BroadcastPool在代碼中定義的

BroadcastGroup在配置中定義:

List<String> paths = Arrays.asList("/user/workers/w1", "/user/workers/w2", "/user/workers/w3"); 
ActorRef router16 = getContext().actorOf(new BroadcastGroup(paths).props(), "router16"); 

akka.actor.deployment { 
    /parent/router15 { 
    router = broadcast-group 
    routees.paths = ["/user/workers/w1", "/user/workers/w2", "/use/workers/w3"] 
    } 
} 

ActorRef router15 = getContext().actorOf(FromConfig.getInstance().props(), "router15"); 

BroadcastGroup在代碼中定義的