In my project, I used the Lightbend activator template as the code base. It works great, but as an example, a character with parameters is not created.
I need to create a new Actor and pass it a parameter during construction, for example:
getContext().actorOf(SpringExtProvider.get(actorSystem).props("ControllerActor",type), "controller_" + type)
In this case, the controller must be created using the repository of the paremeter type , which is used to input the (obviously) controller. Each actor is specifically designed to process and manage the specific king of an object, depending on its type.
But I can’t add a new parameter in the props method to pass this parameter. He does not work.
This is my code:
SpringExtension.java
package com.orange.spectre.core.akka.configuration; import akka.actor.AbstractExtensionId; import akka.actor.ExtendedActorSystem; import akka.actor.Extension; import akka.actor.Props; import com.orange.spectre.core.config.SpringActorProducer; import org.springframework.context.ApplicationContext; public class SpringExtension extends AbstractExtensionId<SpringExtension.SpringExt> { public static SpringExtension SpringExtProvider = new SpringExtension(); @Override public SpringExt createExtension(ExtendedActorSystem system) { return new SpringExt(); } public static class SpringExt implements Extension { private volatile ApplicationContext applicationContext; public void initialize(ApplicationContext applicationContext) { this.applicationContext = applicationContext; } public Props props(String actorBeanName) { return Props.create(SpringActorProducer.class, applicationContext, actorBeanName); } public Props props(String actorBeanName, String type) { return Props.create(SpringActorProducer.class, applicationContext, actorBeanName,type); } } }
SpringActorProducer package com.orange.spectre.core.config;
import akka.actor.Actor; import akka.actor.IndirectActorProducer; import org.springframework.context.ApplicationContext; public class SpringActorProducer implements IndirectActorProducer { private final ApplicationContext applicationContext; private final String actorBeanName; private final String type; public SpringActorProducer(ApplicationContext applicationContext, String actorBeanName) { this.applicationContext = applicationContext; this.actorBeanName = actorBeanName; this.type = null; } public SpringActorProducer(ApplicationContext applicationContext, String actorBeanName, String type) { this.applicationContext = applicationContext; this.actorBeanName = actorBeanName; this.type = type; } @Override public Actor produce() { return (Actor) applicationContext.getBean(actorBeanName); } @Override public Class<? extends Actor> actorClass() { return (Class<? extends Actor>) applicationContext.getType(actorBeanName); } }
I cannot create an actor with a props parameter, since this is mainly possible with Akka ( Documentation ):
public class DemoActor extends UntypedActor { public static Props props(final int magicNumber) { return Props.create(new Creator<DemoActor>() { private static final long serialVersionUID = 1L; @Override public DemoActor create() throws Exception { return new DemoActor(magicNumber); } }); } final int magicNumber; public DemoActor(int magicNumber) { this.magicNumber = magicNumber; } @Override public void onReceive(Object msg) {
If you can help me, this should be great!
Thanks.