如何在 Java 应用程序中使用 Akka Actors?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5245449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use Akka Actors in a Java application?
提问by Jonas
I would like to use Akka actorsin Java.
我想在 Java 中使用Akka 演员。
I downloaded the akka-1.0.zip
and added akka-actor-1.0.jar
to my "Build Path" in Eclipse.
我在 Eclipse 中下载akka-1.0.zip
并添加akka-actor-1.0.jar
到我的“构建路径”中。
Then I wrote this Actor class:
然后我写了这个 Actor 类:
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:
但是我在 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:
更新:我添加scala-library.jar
到我的构建路径,上面的错误消失了。但是当我编译和运行应用程序时出现错误:
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.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");
}
}
采纳答案by Peter Knego
In your akka-1.0.zip
file there is scala-library.jar
. Try adding it to the build path.
在您的akka-1.0.zip
文件中有scala-library.jar
. 尝试将其添加到构建路径。
Also, there is a lib_managed
directory inside the zip, which contains further library files. Possibly aslo some of them will be needed.
此外,lib_managed
zip 中有一个目录,其中包含更多库文件。可能还需要其中一些。
To avoid this kind of situations you should try maven. There is a Akka repository: http://scalablesolutions.se/akka/repository/se/scalablesolutions/akka/
为了避免这种情况,您应该尝试 maven。有一个 Akka 存储库:http: //scalablesolutions.se/akka/repository/se/scalablesolutions/akka/
回答by Jesper
回答by Dzhaughn
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?
我在安装了 Scala IDE 的 Eclipse 中的 Java 项目中使用 Akka 时看到了这个问题。在没有安装 Scala IDE 的情况下使用 Eclipse 实例时,问题就会消失。Eclipse 可能对周围的两个 Scala 库感到困惑?
回答by Thomas
I don't know Akka actors but it seems you need some scala support (libs on the build path) as well.
我不知道 Akka 演员,但似乎您还需要一些 Scala 支持(构建路径上的库)。