java - Akka HelloAkkaJava.java 示例不工作

标签 java maven akka

我正在尝试使用 Java 在 Akka 中编写一个“Hello World”程序。下面是我的 java 源文件,后面是 pom.xml 文件以及生成的错误消息。

我的文件夹结构如下:

hello-akka--|
    |       |
   pom.xml  |
            |--> src --
                      |
                      |--> main --
                                 |
                                 |--> java 
                                        |
                                      HelloAkkaJava.java

HelloAkkaJava.java如下:

import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.Inbox;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class HelloAkkaJava {
    public static class Greet implements Serializable {}
    public static class WhoToGreet implements Serializable {
        public final String who;
        public WhoToGreet(String who) {
            this.who = who;
        }
    }
    public static class Greeting implements Serializable {
        public final String message;
        public Greeting(String message) {
            this.message = message;
        }
    }

    public static class Greeter extends UntypedActor {
        String greeting = "";

        public void onReceive(Object message) {
            if (message instanceof WhoToGreet)
                greeting = "hello, " + ((WhoToGreet) message).who;

            else if (message instanceof Greet)
                // Send the current greeting back to the sender
                getSender().tell(new Greeting(greeting), getSelf());

            else unhandled(message);
        }
    }

    public static void main(String[] args) {
        try {
            // Create the 'helloakka' actor system - a factory
            final ActorSystem system = ActorSystem.create("helloakka");

            // Create the 'greeter' actor - 'actorOf' is a factory method called on the 'system' object reference
            final ActorRef greeter = system.actorOf(Props.create(Greeter.class), "greeter");

            // Create the "actor-in-a-box"
            final Inbox inbox = Inbox.create(system);

            // Tell the 'greeter' to change its 'greeting' message
            greeter.tell(new WhoToGreet("akka"), ActorRef.noSender());

            // Ask the 'greeter for the latest 'greeting'
            // Reply should go to the "actor-in-a-box"
            inbox.send(greeter, new Greet());

            // Wait 5 seconds for the reply with the 'greeting' message
            final Greeting greeting1 = (Greeting) inbox.receive(Duration.create(5, TimeUnit.SECONDS));
            System.out.println("Please do not divide by zero " + greeting1.message);

            // Change the greeting and ask for it again
            greeter.tell(new WhoToGreet("typesafe"), ActorRef.noSender());
            inbox.send(greeter, new Greet());
            final Greeting greeting2 = (Greeting) inbox.receive(Duration.create(5, TimeUnit.SECONDS));
            System.out.println("ACDC Rocks also... Greeting: " + greeting2.message);

            //after zero seconds, send a Greet message every second to the greeter with a sender of the GreetPrinter
            final ActorRef greetPrinter = system.actorOf(Props.create(GreetPrinter.class));
            system.scheduler().schedule(Duration.Zero(), Duration.create(1, TimeUnit.SECONDS), greeter, new Greet(), system.dispatcher(), greetPrinter);

        System.out.println("Everything in main() ran.");
        System.exit(0); 
        } catch (TimeoutException ex) {
            System.out.println("Got a timeout waiting for reply from an actor");
            ex.printStackTrace();
        }
    }

    public static class GreetPrinter extends UntypedActor {
        public void onReceive(Object message) {
            if (message instanceof Greeting)
                System.out.println(((Greeting) message).message);
        }
    }
}

我的 pom.xml 文件来尝试运行这个恶作剧:

<?xml version="1.0" encoding="UTF-8"?>
<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>
  <artifactId>hello-akka</artifactId>
  <groupId>oh.wow.amazing</groupId>
  <name>Hello Akka</name>
  <version>1.0</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.typesafe.akka</groupId>
      <artifactId>akka-actor_2.11</artifactId>
      <version>2.4.0</version>
    </dependency>
  </dependencies>

</project>

运行时收到的错误:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.531s
[INFO] Finished at: Tue Oct 06 10:53:27 CDT 2015
[INFO] Final Memory: 15M/162M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:java (default-cli) on project hello-akka: An exception occured while executing the Java class. null: InvocationTargetException: HelloWorld -> [Help 1]
[ERROR] 

我用来获取此输出的命令:mvn exec:java -Dexec.mainClass="akka.Main"-Dexec.args="HelloWorld"

我在此处获取了有关从命令行运行什么内容的信息:http://www.typesafe.com/activator/template/akka-sample-main-java?_ga=1.217401040.754558484.1443808082

但是上面的示例使用了不同的代码。

感谢您阅读所有这些垃圾内容。

问候,

最佳答案

我认为您希望将类名作为exec.mainClass参数提供给mvncompile,例如

mvn compile exec:java -Dexec.mainClass="your.package.HelloAkkaJava"

关于java - Akka HelloAkkaJava.java 示例不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32974483/

相关文章:

Java - LWJGL 中的绑定(bind)纹理导致白屏

java - 哪种设计模式最适合这种场景

java - 关于 javax.persistence JAR 的 Maven 依赖项?

javascript - 在 Maven 生命周期中运行 JavaScript 文件

multithreading - 处理Akka actor的receive方法中的异步调用的最佳方法

java - Vertx 追加到文件

java - 这种类型的Object意味着什么?

java - Spring Boot firebase admin sdk 设置 - 无法解析符号

java - 停止(getSelf())与停止(this.getSelf())

java - Akka Actor 生命周期