Typesafe activator question for akka-java-spring

230 views
Skip to first unread message

MV

unread,
May 15, 2014, 2:26:46 PM5/15/14
to akka...@googlegroups.com
Hello,

I am trying to use DI with Spring and Akka actors. I found the template example at TypesafeActivator but I had question for a case that is not working for me.
The example uses AnnotationBased DI and I would like to use XMl based for the following reason:

I have two algorithm implementations for my project which project my results to the end user in different formats. The algorithms are also significantly different and I have a single JAVA interface that these 2 algorithms implement.
I don't prefer the annotation based binding because I want the tester during integration testing to change the algorithm we use in an XML file or property file because if the chnage is done in a JAVA file, it warrants a new release of software version at the Integration test site. I want the tester to be able to run the jar with different algorithms based on an external XML/ java properties file.

Qn 1) The example on the website to create the actor system and Props uses Java annotation technique. How do I convert that to a regular <bean>....</bean> kind of thing?

Qn 2) I want to create a few actors as simple UntypedActors and few Actors as ConsistentHashingRouter where the number of workers comes from a Java Properties file. Is this as simple as having another props() method? How can I translate this props() method to an XML format so that I can make it follow the way I want to in Qn (1). As of now, I do not want a mix of dependency injection techniques and would like to stick to one format.

Any help will be really appreciated because I am a novice to Spring DI with Akka actors.

Thanks in advance,
Meena Venkat

Björn Antonsson

unread,
May 16, 2014, 9:46:16 AM5/16/14
to akka...@googlegroups.com
Hi,

On 15 May 2014 at 20:26:49, MV (mee...@gmail.com) wrote:

Hello,

I am trying to use DI with Spring and Akka actors. I found the template example at TypesafeActivator but I had question for a case that is not working for me.
The example uses AnnotationBased DI and I would like to use XMl based for the following reason:

I have two algorithm implementations for my project which project my results to the end user in different formats. The algorithms are also significantly different and I have a single JAVA interface that these 2 algorithms implement.
I don't prefer the annotation based binding because I want the tester during integration testing to change the algorithm we use in an XML file or property file because if the chnage is done in a JAVA file, it warrants a new release of software version at the Integration test site. I want the tester to be able to run the jar with different algorithms based on an external XML/ java properties file.

Qn 1) The example on the website to create the actor system and Props uses Java annotation technique. How do I convert that to a regular <bean>....</bean> kind of thing?


If you don't want to convert everything, then you can mix annotations and XML by adding this to your spring context before you call refresh().

XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
xmlReader.loadBeanDefinitions("spring.xml");

If you do want to convert everything, then I guess you would just have to wire the things up by hand in the same way that they are wired by the annotations.

Qn 2) I want to create a few actors as simple UntypedActors and few Actors as ConsistentHashingRouter where the number of workers comes from a Java Properties file. Is this as simple as having another props() method? How can I translate this props() method to an XML format so that I can make it follow the way I want to in Qn (1). As of now, I do not want a mix of dependency injection techniques and would like to stick to one format.


So the props method doesn't care how you wired up your beans, it only looks up a named bean. Configuration of akka is usually done in code or in the application.conf file described in the documentation. If you would want full control of that from Spring, then you would probably have to add another props method, that looks up some named configuration bean that tells you which router add with which router configuration.

B/

Any help will be really appreciated because I am a novice to Spring DI with Akka actors.

Thanks in advance,
Meena Venkat
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
-- 
Björn Antonsson
Typesafe – Reactive Apps on the JVM
twitter: @bantonsson

MV

unread,
May 19, 2014, 2:32:44 PM5/19/14
to akka...@googlegroups.com
Hello Bjorn,

Thanks for getting back to me. I am sorry for the late response as I was away for work and did not get a chance to try your solution. I wanted to get back to you on this.
For Qn(1): Configuring using XML and annotation.
--> I have taken the approach of mixing both and in my ANNOTATION BASED context file (i.e. AppConfig.java), I now use the annotation: @ImportResource("file:./properties/ApplicationProperties.xml").
--> Then I have filed in there as follows:
     @Autowired
      private Algorithm myCurrentAlg
---> This is defined in the XML file and I am able to read the bean.

Qn(2) Using the Typesafe Activator Template for AKKA-SPRING-JAVA
This is getting increasingly difficult for some reason :-)

Problem (1): 
SpringActorProducer.java - method: actorClass() is constantly throwing NoSuchBeanDefinitionException.
Scenario: I have a really simple CounterActor class....pretty much empty to test the scenario as follows:

@Named("Counter")
@Scope("prototype")
public class CounterActor extends UntypedActor
{

    public CounterActor()
    {
        System.out.println("Inside constructor...............");
    }
    @Override
    public void onReceive(Object obj) throws Exception
    {
        // TODO Auto-generated method stub
        
    }
The code for SpringActorProducer is exactly the same as the website tutorial - no changes.
I get the exception as follows:

akka.ConfigurationException: configuration problem while creating [akka://DCA-Actor-System/user/c] with dispatcher [akka.actor.default-dispatcher] and mailbox [akka.actor.default-mailbox]
at akka.actor.LocalActorRefProvider.actorOf(ActorRefProvider.scala:723)
at akka.actor.dungeon.Children$class.makeChild(Children.scala:191)
at akka.actor.dungeon.Children$class.attachChild(Children.scala:42)
at akka.actor.ActorCell.attachChild(ActorCell.scala:338)
at akka.actor.ActorSystemImpl.actorOf(ActorSystem.scala:518)
at com.bae.dca.MyTestComponent.initializeThreads(MyTestComponent.java:189)
at com.bae.dca.MyTestComponent.main(MyTestComponent.java:132)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'Counter' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159)
at org.springframework.beans.factory.support.AbstractBeanFactory.getType(AbstractBeanFactory.java:590)
at org.springframework.context.support.AbstractApplicationContext.getType(AbstractApplicationContext.java:1020)
at com.bae.dca.SpringActorProducer.actorClass(SpringActorProducer.java:70)
at akka.actor.Props.cachedActorClass(Props.scala:203)
at akka.actor.Props.actorClass(Props.scala:327)
at akka.dispatch.Mailboxes.getMailboxType(Mailboxes.scala:124)
at akka.actor.LocalActorRefProvider.actorOf(ActorRefProvider.scala:718)
... 6 more


Any idea why this is happening? I have googled everything I can think of but to no avail. If this is resolved then I have one more question regarding the props() method for you :-)

Thanks for all your help in advance.
Meena Venkat

Björn Antonsson

unread,
May 21, 2014, 7:12:04 AM5/21/14
to akka...@googlegroups.com
Hi,

I don't know why the ActorProducer can't look up your bean. I assume that there is some way to debug what beans have been defined in the ApplicationContext.

Maybe you're scanning the wrong package names for annotated beans?

B/

MV

unread,
May 21, 2014, 1:26:35 PM5/21/14
to akka...@googlegroups.com
Hello Bjorn,

Thanks for getting back to me. I finally got it yesterday after debugging and googling in various ways :-)

So the issue was that I was getting a BeanException because the applicationContext.getType(bean_name) also tries to call the constructor of the actor. The constructor of the actor was expecting a java.util.Properties which was also being injected via Annotation. After I added the @Inject to the constructor and @Named(myProperties) to the constructor, it was able to move forward.

For example (in case someone else is looking for working solutions)

class MyComponentActor extends UntypedActor
{
    final Properties appProps;
   
 
    @Inject
    public MyComponentActor (@Named("myproperties") final Properties appProperties) 
   {
           // do stuff
           appProps = appProperties;
    }
}

This seems to work for now. I am going to try the props method for creating the routers and will post here for any observations/issues.

Thanks,
Meena
Reply all
Reply to author
Forward
0 new messages