How can I use Akka Actors in a Java application?
I would like to use Akka actors in Java.
I downloaded the akka-1.0.zip
and added akka-actor-1.0.jar
to my "Build Path" in Eclipse.
Then I wrote this Actor class:
package com.example;
import akka.actor.UntypedActor;
public class MyActor extends UntypedActor {
public void onReceive(Object message) throws IllegalArgumentException {
if (message instanceof String) {
System.out.println("Received: " + message);
} else throw new IllegalArgumentException("Unknown message: " + message);
}
}
But I get errors in Eclipse:
The type scala.Option cannot be resolved.
The type scala.Some cannot be resolved.
The type scala.PartialFunction cannot be resolved.
The type scala.ScalaObject cannot be resoled.
Do I need to add any more files to my "Build Path" or what am I doing wrong? I don't find the documentation beeing that helpful.
Update: I added scala-library.jar
to my Build Path and the above erros disappeared. But I get an error when I compile and run the application:
Exception in thread "main" java.lang.NoClassDefFoundError: net/lag/configgy/ConfigMap
at akka.actor.Actors.actorOf(Actors.java:70)
at com.example.ActorTest.main(ActorTest.java:9)
Caused by: java.lang.ClassNotFoundException: net.lag.configgy.ConfigMap
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.开发者_高级运维lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Here is the main class where I use my actor:
package com.example;
import akka.actor.ActorRef;
import akka.actor.Actors;
public class ActorTest {
public static void main(String[] args) {
ActorRef myActor = Actors.actorOf(MyActor.class);
myActor.start();
System.out.println("My Actor started");
}
}
In your akka-1.0.zip
file there is scala-library.jar
. Try adding it to the build path.
Also, there is a lib_managed
directory inside the zip, which contains further library files. Possibly aslo some of them will be needed.
To avoid this kind of situations you should try maven. There is a Akka repository: http://scalablesolutions.se/akka/repository/se/scalablesolutions/akka/
You can find a complete, working example here; it's a Maven project, so it will get the dependencies for you automatically.
I see this problem using Akka in a Java project in Eclipse with the Scala IDE installed. The problem goes away when using an Eclipse instance without the Scala IDE installed. Eclipse is perhaps confused by two Scala libraries around?
I don't know Akka actors but it seems you need some scala support (libs on the build path) as well.
精彩评论