带有Spring参数的Java Akka Actors

Her*_*hon 6 java spring actor akka

在我的项目中,我使用Lightbend激活器模板作为代码库.它工作得很完美,但示例中的Actor不是用参数创建的.

我需要创建一个新的Actor并在构造期间传递给它一个参数,例如:

getContext().actorOf(SpringExtProvider.get(actorSystem).props("ControllerActor",type), "controller_" + type)
Run Code Online (Sandbox Code Playgroud)

在这个用例中,控制器需要能够使用props paremeter 类型创建,该类型用于键入(显然)控制器.每个Actor都专门设计用于根据其类型处理和控制特定的对象王.

但我无法在props方法中添加新参数来传递此参数.它不起作用.

这是我的代码:

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;

/**
 * Created by Hervé Darritchon on 04/04/2016.
 * <p>
 * An Akka Extension to provide access to Spring managed Actor Beans.
 */
public class SpringExtension extends AbstractExtensionId<SpringExtension.SpringExt> {

    /**
     * The identifier used to access the SpringExtension.
     */
    public static SpringExtension SpringExtProvider = new SpringExtension();

    /**
     * Is used by Akka to instantiate the Extension identified by this
     * ExtensionId, internal use only.
     */
    @Override
    public SpringExt createExtension(ExtendedActorSystem system) {
        return new SpringExt();
    }

    /**
     * The Extension implementation.
     */
    public static class SpringExt implements Extension {

        private volatile ApplicationContext applicationContext;

        /**
         * Used to initialize the Spring application context for the extension.
         *
         * @param applicationContext
         */
        public void initialize(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }

        /**
         * Create a Props for the specified actorBeanName using the
         * SpringActorProducer class.
         *
         * @param actorBeanName The name of the actor bean to create Props for
         * @return a Props that will create the named actor bean using Spring
         */
        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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

SpringActorProducer 包com.orange.spectre.core.config;

import akka.actor.Actor;
import akka.actor.IndirectActorProducer;
import org.springframework.context.ApplicationContext;

/**
 * Created by Hervé Darritchon on 21/03/2016.
 */
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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我无法使用props参数创建一个actor,因为它基本上可以与Akka一样(文档):

    public class DemoActor extends UntypedActor {

  /**
   * Create Props for an actor of this type.
   * @param magicNumber The magic number to be passed to this actor’s constructor.
   * @return a Props for creating this actor, which can then be further configured
   *         (e.g. calling `.withDispatcher()` on it)
   */
  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) {
    // some behavior here
  }

}

  system.actorOf(DemoActor.props(42), "demo");
Run Code Online (Sandbox Code Playgroud)

如果你能帮助我,那应该很棒!

谢谢.

Kon*_*hev 2

我同意“nickebbitt”。不确定这是否可能。其中一种方法是将幻数生成器的实现注入到 Actor 中。\n此外,我想建议以下 IndirectActorProducer 实现:

\n\n
public class SpringDIActor implements IndirectActorProducer {\n\nprivate static final Logger LOG = LoggerFactory.getLogger(SpringDIActor.class);\n\nprivate Class<? extends Actor> type;\nprivate Actor actorInstance = null;\n\npublic SpringDIActor(Class<? extends Actor> type) {\n    this.type = type;\n}\n\npublic SpringDIActor(Actor actorInstance) {\n    this.actorInstance = actorInstance;\n}\n\n/**\n * This factory method must produce a fresh actor instance upon each\n * invocation. <b>It is not permitted to return the same instance more than\n * once.</b>\n */\n@Override\npublic Actor produce() {\n    Actor newActor = actorInstance;\n    actorInstance = null;\n    if (newActor == null) {\n        try {\n            newActor = type.newInstance();\n        } catch (InstantiationException e) {\n            LOG.error("Unable to create actor of type:{}", type, e);\n        } catch (IllegalAccessException e) {\n            LOG.error("Unable to create actor of type:{}", type, e);\n        }\n    }\n    ApplicationContextProvider.getApplicationContext().getAutowireCapableBeanFactory().autowireBean(newActor);\n    return newActor;\n}\n\n/**\n * This method is used by [[Props]] to determine the type of actor which will\n * be created. This means that an instance of this `IndirectActorProducer`\n * will be created in order to call this method during any call to\n * [[Props#actorClass]]; it should be noted that such calls may\n * performed during actor set-up before the actual actor\xe2\x80\x99s instantiation, and\n * that the instance created for calling `actorClass` is not necessarily reused\n * later to produce the actor.\n */\n@Override\npublic Class<? extends Actor> actorClass() {\n    return type;\n}}\n
Run Code Online (Sandbox Code Playgroud)\n\n

这允许您共同创建参与者,而无需从任何代码直接访问 SpringContext,如下所示:

\n\n
ActorSystem.create("system").actorOf(Props.create(SpringDIActor.class, DemoActor.class))\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后只需在 DemoActor 中使用 @Autowired 注释即可。

\n\n

不需要在 DemoActor 上添加额外的注释。

\n