user_mda
user_mda

Reputation: 19438

How is configuration read in AKKA

I am creating custom configurations for my akka application written in java as follows

application.conf

akka {
  actor {
    debug {
      # enable DEBUG logging of actor lifecycle changes
      lifecycle = on
    }
  }
}

I am not sure how to load this configuration. It is unclear from the docs if this needs to be explicitly loaded at actrSystem creation or if it needs to be in the class path while running the jar. In both cases, is there an example I can look at

This is a maven project and the configuration file is under src/main/resources

I see that the applucation.conf exists under target/classes. Is that it?

Upvotes: 3

Views: 5080

Answers (1)

gaston
gaston

Reputation: 498

With ConfigFactory you can load the application.conf file from src/main/resources, not necessarily need an ActorSystem to use it

According to the akka doc http://doc.akka.io/docs/akka/2.4/general/configuration.html

  • Akka uses the Typesafe Config Library, which might also be a good choice for the configuration of your own application or library built with or without Akka. This library is implemented in Java with no external dependencies; you should have a look at its documentation (in particular about ConfigFactory), which is only summarized in the following.

A simple test

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

public class ConfigFactoryTest {

    public static void main(String[] args) {
        Config conf = ConfigFactory.load("application.conf");
        System.out.println(conf.getString("akka.actor.debug.lifecycle")); //should be 'on'
     }
}

Also can use withFallback method http://doc.akka.io/docs/akka/2.4/general/configuration.html#Reading_configuration_from_a_custom_location

Assume you have another properties file called fallback.conf

akka {
 actor {
    fallback = "fallback"
       }
}

Config fallback = ConfigFactory.load("fallback.conf")
Config conf = ConfigFactory.load("application.conf").withFallback(fallback);
        System.out.println(conf.getString("akka.actor.fallback")); //fallback

Here you can find examples :

https://github.com/typesafehub/config/tree/master/examples/java

Upvotes: 4

Related Questions