在Java中配置文件配置Akka路由器

时间:2014-12-01 08:23:28

标签: java akka

刚开始学习akka功能。我正在尝试运行一个使用基于一致哈希的akka​​路由器的程序。

我的环境是:Java 7 update 67,akka-actor_2.10 version 2.3.7。

我的配置文件:

MyRouter{
    akka.actor.deployment{
        /exampleRouter {
            router = consistent-hashing-pool
            nr-of-instances = 5
        }
    }
}  

Main.java:

public class Main {

    public static void main(String... args){

        ActorSystem system = ActorSystem.create("ExampleRouter", ConfigFactory.load().getConfig("MyRouter"));
        ActorRef router = system.actorOf(Props.create(AcceptorActor.class).withRouter(new FromConfig()), "exampleRouter");

        for (int i = 0; i < 10; i++){
            router.tell(new RevisionContent("Hi", new Object()),  router);
            router.tell(new RevisionContent("Hello", new Object()),  router);
        }
    }
}

启动失败,出现以下异常:

Exception in thread "main" akka.ConfigurationException: configuration problem while creating [akka://ExampleRouter/user/exampleRouter] with router dispatcher [akka.actor.default-dispatcher] and mailbox [akka.actor.default-mailbox] and routee dispatcher [akka.actor.default-dispatcher] and mailbox [akka.actor.default-mailbox]
    at akka.actor.LocalActorRefProvider.actorOf(ActorRefProvider.scala:752)
    at akka.actor.dungeon.Children$class.makeChild(Children.scala:207)
    at akka.actor.dungeon.Children$class.attachChild(Children.scala:42)
    at akka.actor.ActorCell.attachChild(ActorCell.scala:369)
    at akka.actor.ActorSystemImpl.actorOf(ActorSystem.scala:553)
    at study.Main.main(Main.java:16)
Caused by: akka.ConfigurationException: Configuration missing for router [akka://ExampleRouter/user/exampleRouter] in 'akka.actor.deployment' section.
    at akka.routing.FromConfig.verifyConfig(RouterConfig.scala:297)
    at akka.routing.RoutedActorRef.<init>(RoutedActorRef.scala:40)
    at akka.actor.LocalActorRefProvider.actorOf(ActorRefProvider.scala:750)
    ... 5 more

我在example之后写了我的代码。请指教。

1 个答案:

答案 0 :(得分:1)

初始化consistent-hashing-group路由器所需的最小配置如下:

MyRouter{
  akka {
    actor {
      deployment {
        /exampleRouter {
          router = consistent-hashing-group
        }
      }
    }
  }
}

您使用了错误的代码deployement - 正确的代码为deployment