如何在IntelliJ IDEA中运行akka actors

9

根据Akka官方文档:

这个主方法将创建运行actor所需的基础设施,启动给定的主actor,并安排整个应用程序在主actor终止后关闭。因此,您可以使用类似以下命令运行上面的代码:

java -classpath akka.Main example.two.HelloWorld

那么,我如何从IntelliJ IDEA启动它?我没有找到合适的窗口。

AKKA的依赖项已经在项目中了:

 <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.10</artifactId>
            <version>2.2-M3</version>
        </dependency>
    </dependencies>

代码本身(如您所见,没有 main(...)):
public class HelloWorld extends UntypedActor {

    @Override
    public void preStart() {
        // create the greeter actor
        final ActorRef greeter =
                getContext().actorOf(Props.create(Greeter.class), "greeter");
        // tell it to perform the greeting
        greeter.tell(Greeter.Msg.GREET, getSelf());
    }

    @Override
    public void onReceive(Object msg) {
        if (msg == Greeter.Msg.DONE) {
            // when the greeter is done, stop this actor and with it the application
            getContext().stop(getSelf());
        } else unhandled(msg);
    }
}
2个回答

12

看起来文档和二进制发行版不同步。

就像您可以在Github上看到的一样,Main.scala仅在16天前添加。

要解决这个问题,您可以将依赖版本更改为SNAPSHOT。将快照存储库添加到pom中,并将版本更改为2.2-SNAPSHOT

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.stackoverflow</groupId>
    <artifactId>akka-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>${project.artifactId}-${project.version}</name>

    <repositories>
        <repository>
            <id>akka-snapshots</id>
            <name>Akka Snapshots</name>
            <url>http://repo.akka.io/snapshots/</url>
            <layout>default</layout>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.typesafe.akka</groupId>
            <artifactId>akka-actor_2.10</artifactId>
            <version>2.2-SNAPSHOT</version>
        </dependency>

    </dependencies>

</project>

现在akka.Main可用。为了启动应用程序,您将需要指向正确的主类并将您自己的 actor 类添加为参数。

首先选择创建一个新的运行配置:

enter image description here

然后添加一个新的应用程序:

enter image description here

给应用程序取个名字(例如 Actor),并填写主类为 akka.Main,将您的 HelloWorld 类添加为 Program Arguments(记得包含完整的包名):

enter image description here

现在,您可以准备运行程序了,只需按工具栏中的播放按钮:

enter image description here

嘿!


这是好的。但涉及SNAPSHOT问题,请参考:http://stackoverflow.com/questions/16569726/akka-actors-for-java-maven-dependency-issue - ses
@ses 嗯,既然你正在使用SNAPSHOT版本的示例,那我就不太看出问题。如果你想让它在稳定版本中工作,那么你就需要等待。至少我回答了你的问题“那么,我该怎样从IntelliJ IDEA启动它?” - maba
目前我很高兴能够拥有:https://github.com/akka/akka/blob/master/akka-actor/src/main/scala/akka/Main.scala这个类是用Java编写的。 - ses
嗨@maba,我看到在你的屏幕截图中,IntelliJ标记Props.create(Greeter.class)为错误,但是通过sbt命令行编译和运行显然是可以的。我这里也碰到了完全相同的问题...你有任何关于如何在IntelliJ中解决该问题的想法吗?这真的很让人恼火。 :-/ - andraus
有没有想法如何在 JIdea 中调试此应用程序。尝试了调试器,但它不会在断点处停止执行。 - SashikaXP

0
//You can use the following code to start your actor:

public class Main {

  public static void main(String[] args) {
      akka.Main.main(new String[] { HelloWorld.class.getName() });
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接