I am new to akka and springboot, while searching mostly examples inject ActorSystem with springextention, I tried like below and it's working. Just want to know if there is any difference or I might face any issues in future.
Get ActorSystem like below:
@Bean
public ActorSystem getActorSystem() {
// Create the actor system.
final Config settings = ConfigFactory.load();
return ActorSystem.create("my", settings);
}
then where required just use like below:
ActorSystem system = context.getBean(ActorSystem.class);
ActorRef consumer = system.actorOf(Props.create(Consumer.class));
It is perfectly fine if you are not using any beans in actors.
Otherwise, when you use context inside actors and you call context.getBean(SomeBean.class)
in any actor, you may face lifecycle issues.
SomeBean
may not be yet loaded and initialized. Spring does not know that your ActorSystem
needs SomeBean
to be initialized first.
You may make your ActorSystem
@DependOn
all beans used by all actors, but this is fragile and hard to maintain.
Personally, I do not find Akka and (Spring+Java) to be good match. There is less friction when you use Akka with Scala language and ecosystem (libraries).