如何在 Java 应用程序中使用 Akka Actors?
我想在Java中使用 Akka actor 。
我下载了 akka-1.0.zip
并将 akka-actor-1.0.jar
添加到 Eclipse 中的“构建路径”中。
然后我写了这个 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);
}
}
但是我在 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.
我需要向我的“构建路径”添加更多文件还是我做错了什么?我觉得文档没有那么有帮助。
更新:我将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$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
这是我使用 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");
}
}
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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的
akka-1.0.zip
文件中,有scala-library.jar
。尝试将其添加到构建路径中。此外,zip 中有一个
lib_management
目录,其中包含更多库文件。可能还需要其中一些。为了避免这种情况,你应该尝试使用 Maven。有一个 Akka 存储库: http://scalablesolutions.se/akka/repository/se/可扩展解决方案/akka/
In your
akka-1.0.zip
file there isscala-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/
您可以在此处找到完整的工作示例;它是一个 Maven 项目,因此它会自动为您获取依赖项。
You can find a complete, working example here; it's a Maven project, so it will get the dependencies for you automatically.
我在安装了 Scala IDE 的 Eclipse 中的 Java 项目中使用 Akka 时看到了这个问题。当使用未安装 Scala IDE 的 Eclipse 实例时,该问题就会消失。 Eclipse 可能对周围的两个 Scala 库感到困惑?
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?
我不知道 Akka 演员,但似乎你也需要一些 scala 支持(构建路径上的库)。
I don't know Akka actors but it seems you need some scala support (libs on the build path) as well.