码迷,mamicode.com
首页 > 其他好文 > 详细

akka开发(一)HelloWorld

时间:2016-09-12 18:56:48      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

package com.hfi.helloakka;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;

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);
      }
    }
package com.hfi.helloakka;

import akka.actor.UntypedActor;

public class Greeter extends UntypedActor {
      
      public static enum Msg {
        GREET, DONE;
      }
      
      @Override
      public void onReceive(Object msg) {
        if (msg == Msg.GREET) {
          System.out.println("Hello World!");
          getSender().tell(Msg.DONE, getSelf());
        } else unhandled(msg);
      }
      
    }

pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.hfi</groupId>
  <artifactId>helloakka</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>helloakka Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.10.2</version>
    </dependency>
    <dependency>
          <groupId>com.typesafe.akka</groupId>
          <artifactId>akka-actor_2.10</artifactId>
          <version>2.2.5</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>helloakka</finalName>
  </build>
</project>

 

可能遇到的问题:

java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce$class

用的akka包跟依赖的scala包版本不一致导致(貌似scala不向下兼容它的旧版本),笔者一开始用的2.11.7报错,换成pom中的2.10.2后(到akka-2.2.5.zip中看的版本号)程序就跑起来了。  参考了:http://www.jianshu.com/p/59733597d448

akka开发(一)HelloWorld

标签:

原文地址:http://www.cnblogs.com/lyhero11/p/5865685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!