you can use the ConfigFactory.load().getConfig()
and load a specific configuration to your application.
Like this is my application.conf
:
akka.actor.allow-java-serialization = on
mySpecialConfig {
akka {
loglevel = DEBUG
}
}
and my application where I can call context.log.debug()
directly because the AbstractBehavior
already extends ActorLogging.
import akka.actor.typed._
import akka.actor.typed.scaladsl.{AbstractBehavior, ActorContext, Behaviors}
import com.typesafe.config.ConfigFactory
object CounterActorTypedDemo {
def main(args: Array[String]): Unit = {
run()
}
def run(): Unit = {
import Counter._
val specialConfig = ConfigFactory.load().getConfig("mySpecialConfig")
val countActor = ActorSystem(CounterActorTyped(), "CounterSystem", specialConfig)
(1 to 5).foreach(_ => countActor ! Increment)
(1 to 3).foreach(_ => countActor ! Decrement)
countActor ! Print
Thread.sleep(5000)
countActor.terminate
}
}
object Counter {
trait CounterMsg
final case object Print extends CounterMsg
final case object Increment extends CounterMsg
final case object Decrement extends CounterMsg
}
object CounterActorTyped {
def apply(): Behavior[Counter.CounterMsg] = Behaviors.setup[Counter.CounterMsg](context => new CounterActorTyped(context))
}
class CounterActorTyped(context: ActorContext[Counter.CounterMsg]) extends AbstractBehavior[Counter.CounterMsg](context) {
context.log.info("Counter Application started")
var count = 0
import Counter._
override def onMessage(msg: CounterMsg): Behavior[CounterMsg] = msg match {
case Increment =>
context.log.debug(s"incrementing $count ...")
count += 1
Behaviors.same
case Decrement =>
context.log.debug(s"decrementing $count ...")
count -= 1
Behaviors.same
case Print =>
context.log.debug(s"current count is: $count")
Behaviors.same
}
override def onSignal: PartialFunction[Signal, Behavior[CounterMsg]] = {
case PostStop =>
context.log.info("Counter Application stopped")
this
}
}
the logs will appear on the console like this:
org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTypedDemo
10:00:35.944 [CounterSystem-akka.actor.default-dispatcher-3] INFO akka.event.slf4j.Slf4jLogger - Slf4jLogger started
10:00:35.946 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.event.EventStream - logger log1-Slf4jLogger started
10:00:35.951 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.event.EventStream - Default Loggers started
SLF4J: A number (3) of logging calls during the initialization phase have been intercepted and are
SLF4J: now being replayed. These are subject to the filtering rules of the underlying logging system.
SLF4J: See also http://www.slf4j.org/codes.html#replay
10:00:36.219 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.serialization.Serialization(akka://CounterSystem) - Replacing JavaSerializer with DisabledJavaSerializer, due to `akka.actor.allow-java-serialization = off`.
10:00:36.762 [CounterSystem-akka.actor.default-dispatcher-3] INFO org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - Counter Application started
10:00:36.763 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 0 ...
10:00:36.763 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 1 ...
10:00:36.763 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 2 ...
10:00:36.764 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 3 ...
10:00:36.764 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - incrementing 4 ...
10:00:36.764 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - decrementing 5 ...
10:00:36.765 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - decrementing 4 ...
10:00:36.765 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - decrementing 3 ...
10:00:36.765 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - current count is: 2
10:00:41.780 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Running CoordinatedShutdown with reason [ActorSystemTerminateReason]
10:00:41.780 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [before-service-unbind] with [0] tasks
10:00:41.788 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [service-unbind] with [0] tasks
10:00:41.789 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [service-requests-done] with [0] tasks
10:00:41.789 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [service-stop] with [0] tasks
10:00:41.789 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [before-cluster-shutdown] with [0] tasks
10:00:41.789 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-sharding-shutdown-region] with [0] tasks
10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-leave] with [0] tasks
10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-exiting] with [0] tasks
10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-exiting-done] with [0] tasks
10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [cluster-shutdown] with [0] tasks
10:00:41.790 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [before-actor-system-terminate] with [0] tasks
10:00:41.791 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing phase [actor-system-terminate] with [1] tasks.
10:00:41.793 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.actor.CoordinatedShutdown - Performing task [terminate-system] in CoordinatedShutdown phase [actor-system-terminate]
10:00:41.800 [CounterSystem-akka.actor.default-dispatcher-3] INFO org.github.felipegutierrez.explore.akka.typed.basics.CounterActorTyped - Counter Application stopped
10:00:41.819 [CounterSystem-akka.actor.default-dispatcher-3] DEBUG akka.event.EventStream - shutting down: StandardOutLogger
Process finished with exit code 0