簡體   English   中英

Akka HelloAkkaJava.java示例不起作用

[英]Akka HelloAkkaJava.java Example Not Working

我正在嘗試使用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參數來mvn compile例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM