我是akka的新手,有点卡住了。任何帮助,不胜感激。
在akka 2.1.4文档http://doc.akka.io/docs/akka/2.1.4/java/routing.html中,提供了示例自定义路由器代码,其中自定义路由逻辑将根据实例路由消息。
return new CustomRoute() {
@Override
public Iterable<Destination> destinationsFor(ActorRef sender, Object msg) {
switch ((Message) msg) {
case DemocratVote:
case DemocratCountResult:
return Arrays.asList(
new Destination[] { new Destination(sender, democratActor) });
case RepublicanVote:
case RepublicanCountResult:
return Arrays.asList(
new Destination[] { new Destination(sender, republicanActor) });
default:
throw new IllegalArgumentException("Unknown message: " + msg);
}
}
};
我正在尝试使用akka 2.3.11路由器API的http://doc.akka.io/docs/akka/2.3.11/java/routing.html实现类似的逻辑,但是无法理解以下代码段。
@Override
public Routee select(Object message, IndexedSeq<Routee> routees) {
List<Routee> targets = new ArrayList<Routee>();
for (int i = 0; i < nbrCopies; i++) {
targets.add(roundRobin.select(message, routees));
}
return new SeveralRoutees(targets);
}
无法获得路线和演员的关系。我如何将路由与消息实例相关联?
最佳答案
您无需HAVE TO(请参见发送方法注释)从提供的列表中选择目标路由。您可以有以下内容:
Routee democratRoutee = new ActorRefRoutee(democratActorRef);
Routee republicanRoutee = new ActorRefRoutee(republicanActorRef);
@Override
public Routee select(Object message, IndexedSeq<Routee> routees) {
switch ((Message) msg) {
case DemocratVote: return new NoRoutee();
case DemocratCountResult:
democratRoutee;
case RepublicanVote: return new NoRoutee();
case RepublicanCountResult:
return republicanRoutee;
default:
throw new IllegalArgumentException("Unknown message: " + msg);
}
}
//注意:我不知道Java中NoRoutee实例getter的确切语法,因此可能有所不同。
关于java - 如何使用Akka 2.3.11在Java中创建自定义路由器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31878832/