如何在Java应用程序中使用Akka Actors? [英] How can I use Akka Actors in a Java application?

查看:153
本文介绍了如何在Java应用程序中使用Akka Actors?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中使用 Akka actor

I would like to use Akka actors in Java.

我下载了 akka-1.0.zip 并添加了 akka-actor-1.0.jar 到Eclipse中的Build Path。

I downloaded the akka-1.0.zip and added akka-actor-1.0.jar to my "Build Path" in Eclipse.

然后我写了这个Actor类:

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

但我在Eclipse中遇到错误:

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.

更新:我添加了 scala-library.jar 到我的Build Path和上面的错误消失了。但是当我编译并运行应用程序时出现错误:

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-1.0.zip 文件中,有 scala-library.jar 。尝试将其添加到构建路径。

In your akka-1.0.zip file there is scala-library.jar. Try adding it to the build path.

此外,zip中还有一个 lib_managed 目录,其中包含更多内容库文件。可能还需要其中一些。

Also, there is a lib_managed directory inside the zip, which contains further library files. Possibly aslo some of them will be needed.

为了避免这种情况你应该尝试maven。有一个Akka存储库: http://scalablesolutions.se/akka/repository/se/ scalablesolutions / akka /

To avoid this kind of situations you should try maven. There is a Akka repository: http://scalablesolutions.se/akka/repository/se/scalablesolutions/akka/

这篇关于如何在Java应用程序中使用Akka Actors?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆