akka-java-springfactory

Akka Spring integration for Java with additional spring factories for actor creation with ease. This approach is based on the original approach by Typesafe found here http://typesafe.com/activator/template/akka-java-spring.

How to create an actor system

Just define actor system singleton in the spring context using ActorSystemFactoryBean.

<bean id="actorSystem" class="com.github.sabomichal.akkaspringfactory.ActorSystemFactoryBean">
    <!-- actor system name -->
    <property name="name" value="MyActorSystem"/>
    <!-- actor system configuration -->
    <property name="config">
        <bean class="com.typesafe.config.ConfigFactory" factory-method="load">
            <constructor-arg value="akkaActor.conf"/>
        </bean>
    </property>
</bean>

How to create actors

Programmatic use case

Enable Spring auto scanning of componnents,

<context:component-scan base-package="..." />

annotate actor with @Actor annotation,

import com.github.sabomichal.akkaspringfactory.Actor;

@Actor
public class MyActor extends UntypedActor {

    // additionally inject some dependencies
    @Inject
    private MyService injectedService;

    @Override
    public void onReceive(Object message) throws Exception {
        // receive method
    }
}

and finally create an ActorRef programmatically.

@Inject
private ActorSystem actorSystem;
...
ActorRef myActor = actorSystem.actorOf(SpringProps.create(actorSystem, MyActor.class));

Spring managed use case

Just create a spring managed actor reference,

<bean id="springManagedActor" class="com.github.sabomichal.akkaspringfactory.ActorFactoryBean">
    <!-- actor system reference -->
    <property name="actorSystem" ref="actorSystem"/>

    <!-- class name of actor to create -->
    <property name="actorClass" value="com.github.sabomichal.akkaspringfactory.test.GreetingActor"/>

    <!-- bean name of actor to create -->
    <!--<property name="actorBeanClass" value=""/>-->

    <!-- router configuration -->
    <!--<property name="routerConfig">
        <bean class="akka.routing.SmallestMailboxPool">
            <constructor-arg value="10"/>
        </bean>
    </property>-->

    <!-- mailbox settings -->
    <!--<property name="mailbox" value=""/>-->

    <!-- dispatcher settings -->
    <!--<property name="dispatcher" value=""/>-->
</bean>

and use it as a spring managed dependency wherever you want. And since ActorFactoryBean is producing prototype beans, a new instance of ActorRef is created with all dependencies resolved.

@Inject
private ActorRef springManagedActor;
...
springManagedActor.tell(...);

For detailed examples see the provided unit tests.

If you like it, give it a star, if you don't, please write an issue.