1. Akka

         <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.11</artifactId>
            <version>2.4.16</version>
        </dependency>

2. Akka Producer

public class Producer implements IndirectActorProducer {
    final ApplicationContext applicationContext;
    final String actorBeanName;

    public Producer(ApplicationContext applicationContext,
                               String actorBeanName) {
      this.applicationContext = applicationContext;
      this.actorBeanName = actorBeanName;
    }

    @Override
    public Actor produce() {
      return (Actor) applicationContext.getBean(actorBeanName);
    }

    @Override
    public Class<? extends Actor> actorClass() {
      return (Class<? extends Actor>) applicationContext.getType(actorBeanName);
    }
}

3. Akka ActorSystem-Bean

public class MyExtension extends AbstractExtensionId<MyExtension.SpringExt> {
    /**
     * The identifier used to access the SpringExtension.
     */
    public static MyExtension SpringExtProvider = new MyExtension();
   
    /**
     * 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(Producer.class,
          applicationContext, actorBeanName);
      }
    }
}

4. (Optional) Spring ApplicationContext-Provider
– ActorSystem-Bean

public class ApplicationContextProvider implements ApplicationContextAware
{

    private static ApplicationContext ctx = null;
   
       
    public ApplicationContext getApplicationContext() {
        return ctx;
    }
   
 
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        this.ctx = ctx;
    }
}

5. Actor

@Named
@Scope(“prototype”)
public class FirstActor extends UntypedActor
{
    private final static Logger LOG = LoggerFactory.getLogger(FirstActor.class);
   
    @Inject
    private ActorTestDAO actorTestDAO;

    @Override
    public void onReceive(Object var1) throws Throwable
    {
        LOG.error(“############### This is FirstActor = ” + actorTestDAO.getRand() + “::”  + var1 + ” ####################3″);
        Thread.sleep(4000l);
        super.sender().tell(“thx from actor1”, this.self());
    }

}

6. Actor

 ActorRef firstActor = actorSystem.getSystem().actorOf(
                MyExtension.SpringExtProvider.get(actorSystem.getSystem()).props(“firstActor”), “firstActor”);
        ActorRef secondActor = actorSystem.getSystem().actorOf(
                     
       
        firstActor.tell(“firstActor”, ActorRef.noSender());

7.App Configuration

@Configuration
class AppConfiguration {

  // the application context is needed to initialize the Akka Spring Extension
  @Autowired
  private ApplicationContext applicationContext;

  /**
   * Actor system singleton for this application.
   */
  @Bean
  public ActorSystem actorSystem() {
    ActorSystem system = ActorSystem.create(“AkkaJavaSpring”);
    // initialize the application context in the Akka Spring Extension
    SpringExtProvider.get(system).initialize(applicationContext);
    return system;
  }
}

8.
 – actor

ActorRef firstActor = actorSystem.getSystem().actorOf(
               MyExtension.SpringExtProvider.get(actorSystem.getSystem()).props(“firstActor”), “firstActor”);

Future<Object> firstResult = ask( actorSystem.getSystem().actorSelection(“/user/firstActor”) , “request”, 10000000); /

String result  = (String)Await.result(firstResult, Duration.apply(2, TimeUnit.SECONDS));

 – actor

@Named
@Scope(“prototype”)
public class FirstActor extends UntypedActor
{
    private final static Logger LOG = LoggerFactory.getLogger(FirstActor.class);
   
    @Inject
    private ActorTestDAO actorTestDAO;

    @Override
    public void onReceive(Object var1) throws Throwable
    {
        LOG.error(“############### This is FirstActor = ” + actorTestDAO.getRand() + “::”  + var1 + ” ####################3″);
        Thread.sleep(4000l);
        super.sender().tell(“thx from actor1”, this.self());
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *