Maxim Kirilov
Maxim Kirilov

Reputation: 2739

Configure Akka router from configuration file in Java

Just started to learn about akka capabilities. I am trying to run a program that uses akka router that based on consistent hashing.

My environment is: Java 7 update 67, akka-actor_2.10 version 2.3.7.

My configuration file:

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);
        }
    }
}

The startup fails with the following exception:

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

I wrote my code following this example. Please advice.

Upvotes: 5

Views: 3944

Answers (1)

hicolour
hicolour

Reputation: 784

Minimal configuration required to initialize consistent-hashing-group router is following:

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

You were using wrong tag deployement - the correct one is deployment

Upvotes: 1

Related Questions