• akka开发(一)HelloWorld


    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

  • 相关阅读:
    一、定义枚举类,并随机取值
    常用Linux日志查询命令
    一、爬虫之基础
    Jmeter接口自动化-3-生成HTML报告
    Jmeter接口自动化-2-查看结果数只能显示有限的数据,查看全部数据
    Jmeter接口自动化-1-启动报错:Could not initialize class org.apache.jmeter.gui.util.MenuFactory
    三十一、Java基础之Collection集合
    设计模式
    缓存
    Ckeditor配置
  • 原文地址:https://www.cnblogs.com/lyhero11/p/5865685.html
Copyright © 2020-2023  润新知