2015-10-06 45 views
1

我想在使用Java的Akka中执行'Hello World'程序。下面是我的java源文件,后面跟着pom.xml文件,以及由此产生的错误消息。Akka HelloAkkaJava.java示例不工作

我的文件夹结构如下:

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

上面的例子是使用不同的代码。

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

问候,

回答

0

我想你想将你提供的类名作为exec.mainClass参数mvn compile例如

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