akka框架:(三)不可变对象

转载:https://blog.csdn.net/liubenlong007/article/details/54093794

我们都知道在编写java线程的时候,要传递不可变对象,这里akka也是如此
下面这个例子就是传递不可变对象:

package akka.unmodifiable;

import java.util.Collections;
import java.util.List;

/**
 * Created by liubenlong on 2017/1/5.
 *
 * actor中传递的对象要是不可变对象。当然是为了安全了
 */
public final class Message {

    private final int age;
    private final List<String> list;

    public Message(int age, List<String> list){
        this.age = age;
        /**
         * 把普通list包装为不可变对象
         */
        this.list = Collections.unmodifiableList(list);
    }

    public int getAge() {
        return age;
    }

    public List<String> getList() {
        return list;
    }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
package akka.unmodifiable;

import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSONObject;

public class Greeter extends UntypedActor {

  @Override
  public void onReceive(Object msg) throws InterruptedException {

    try {
      System.out.println("Greeter收到的数据为:" + JSONObject.toJSONString(msg));
      getSender().tell("Greeter工作完成。", getSelf());//给发送至发送信息.
    }catch (Exception e){
      e.printStackTrace();
    }

  }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
package akka.unmodifiable;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSONObject;

import java.util.Arrays;

public class HelloWorld extends UntypedActor {

  ActorRef greeter;

  @Override
  public void preStart() {
    // create the greeter actor
    greeter = getContext().actorOf(Props.create(Greeter.class), "greeter");
    System.out.println("Greeter actor path:" + greeter.path());
    // tell it to perform the greeting
    greeter.tell(new Message(2, Arrays.asList("2", "dsf")), getSelf());
  }

  @Override
  public void onReceive(Object msg) {
    try {
      System.out.println("HelloWorld收到的数据为:" + JSONObject.toJSONString(msg));
    }catch (Exception e){
      e.printStackTrace();
    }
  }
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
package akka.unmodifiable;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import com.typesafe.config.ConfigFactory;

public class Main {
//  public static void main(String[] args) {
//    akka.Main.main(new String[] { HelloWorld.class.getName() });
//  }

  public static void main(String[] args) {
    //创建ActorSystem。一般来说,一个系统只需要一个ActorSystem。
    //参数1:系统名称。参数2:配置文件
    ActorSystem system = ActorSystem.create("Hello", ConfigFactory.load("akka.config"));
    ActorRef a = system.actorOf(Props.create(HelloWorld.class), "helloWorld");
    System.out.println(a.path());
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值