Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
    }

}
like image 384
Jonas Avatar asked Mar 09 '11 12:03

Jonas


People also ask

Can Akka be used with Java?

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.

What is Akka framework in Java?

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.

How do actors work in 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.

What are actors in Java?

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.


2 Answers

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/

like image 141
Peter Knego Avatar answered Oct 12 '22 07:10

Peter Knego


You can find a complete, working example here; it's a Maven project, so it will get the dependencies for you automatically.

like image 37
Jesper Avatar answered Oct 12 '22 08:10

Jesper