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");
}
}
Akka is a toolkit and runtime for building highly concurrent, distributed, and fault-tolerant event-driven applications on the JVM. Akka can be used with both Java and Scala. This guide introduces Akka Actors by describing the Java version of the Hello World example.
Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala. Akka Insights is intelligent monitoring and observability purpose built for Akka.
What is an Actor in Akka? An actor is essentially nothing more than an object that receives messages and takes actions to handle them. It is decoupled from the source of the message and its only responsibility is to properly recognize the type of message it has received and take action accordingly.
Actors are objects (class instances in Java sense) that may have mutable state and normally follow the standard rules: Everything is an actor. Actors communicate with each other exclusively by sending asynchronous messages. There is no shared state, public static variables, etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With