How to do Configuration in Akka

Reading Time: 3 minutes

This blog is for the Scala programmer with a little bit of Akka background, but who is utterly baffled by the seemingly magical Akka Essentials. This blog is all about the configuration in Akka like how we can create a configuration in different ways.

Configuration

Configuration is all about the piece of text which is in the form of key value pair. You can start using Akka without defining any configuration, since sensible default values are provided. Later on you might need to amend the settings to change the default behavior or adapt for specific runtime environments. 

Where Configuration is read from?

All configuration for Akka is held within instances of ActorSystem, or put differently, as viewed from the outside, ActorSystem is the only consumer of configuration information. While constructing an actor system, you can either pass in a Config object or not, where the second case is equivalent to passing ConfigFactory.load(). This means roughly the default is to pass all application.conf , application.json and application.properties found at the root of the classpath.

Methods for passing configuration ?

In Actorsystem there are many ways to pass the configuration we can discuss all of them step by step-

1. Inline Configuration

This is the simplest configuration . In this we simply creating a configuration as a string and parse with config factory.

Let’s take an example in this I create a configString as a configuration and parse with ConfigFactory and then pass this config to the ActorSystem with parameter ConfigFactory.load()

val configString =
    """
      | akka {
      |   loglevel = "ERROR"
      | }
    """.stripMargin

  val config = ConfigFactory.parseString(configString)
  val system = ActorSystem("ConfigurationDemo", ConfigFactory.load(config))

2. Config in a File

In config in a file we create a another file for configuration as application.conf. Here you see the directory of application.conf file whether we can use in our program later.

Directory
Application Directory

Under application.conf we can create configuration here is an example

akka {
 loglevel = DEBUG
}

Now we create a another actor as “DefaultConfigFileDemo” . Now we can pass abd by default akka look at path of config file.

val defaultConfigFileSystem = ActorSystem("DefaultConfigFileDemo")
  val defaultConfigActor = defaultConfigFileSystem.actorOf(Props[SimpleLoggingActor])
  defaultConfigActor ! "Remember me"

3. Config in same file

If you might need more configuration and more actor system whether you can do in a same file you can. In this we can create a special configuration for their actor system i.e. we can add more special configuration in a same file.

like in a above example “application.conf” I can add more configuration here below

akka {
  loglevel = DEBUG
}

mySpecialConfig {
  akka {
    loglevel = INFO
  }
}

Now we can pass these special configuration to our ActorSystem

val specialConfig = ConfigFactory.load().getConfig("mySpecialConfig")
  val specialConfigSystem = ActorSystem("SpecialConfigDemo", specialConfig)
  val specialConfigActor = specialConfigSystem.actorOf(Props[SimpleLoggingActor])
  specialConfigActor ! "Remember me, I am special"

4. Config in another file

For creating a another configuration in another file here is an directory as a refernce.

COnfiguration

Now we can write a configuration in this secretConfig file as

akka {
  loglevel = DEBUG
}

This a code how we use this another file in our actors separateConfig get the directory of another configuration and load in a Config Factory

val separateConfig = ConfigFactory.load("secretFolder/secretConfiguration.conf")
  println(s"separate config log level: ${separateConfig.getString("akka.loglevel")}")

Final Code

Let’s have a look to the final code

Code1
Final Code
Code
Final Code

References

https://doc.akka.io/docs/akka/current/general/configuration.html

I hope this blog is helpfull for any akka learner , for more blogs on akka stay tune…

Thanks for scrolling and keep learning see you next time.

Written by 

Meenakshi Goyal is a Software Consultant and started her career in an environment and organization where her skills are challenged each day, resulting in ample learning and growth opportunities. Proficient in Scala, Akka, Akka HTTP , JAVA. Passionate about implementing and launching new projects. Ability to translate business requirements into technical solutions. Her hobbies are traveling and dancing.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading