Akka并发处理数据:
背景:涉及同时多个文件并发处理计算为了提高效率采用Akka。
介绍:
akka:是一个开发库和运行环境,用于构建高并发、分布式、可容错、事件驱动的基于JVM的应用,
akka是基于actor模型的实现,actor模型也就是响应式模型,它和我们常用的基于方法堵塞式的调用不同,而是基于消息的异步调用。
Actor模型:由三个重要部分组成,它们是状态(state),行为(Behavior)和邮箱(Mailbox);
一:创键一个actor.
package com.akkaTest.www;
import akka.actor.UntypedActor;
/**
* @program: akksTest
* @description:
* @author: jiaokc
* @create: 2020-08-20 18:00
**/
public class TestActor extends UntypedActor {
@Override
public void onReceive(Object o) throws Exception {
System.out.println(o.toString());
getSender().tell("good", getSelf());
//dosomethins
}
}
二:发送消息测试;
package com.akkaTest.www;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.routing.RoundRobinPool;
/**
* @program: akksTest
* @description:
* @author: jiaokc
* @create: 2020-08-20 18:04
**/
public class AkkaTest {
public static void main(String[] args) {
ActorSystem actorSystem=ActorSystem.create("myActor");
ActorRef actorRef= actorSystem.actorOf(new RoundRobinPool(10000).props(Props.create(TestActor.class)),
"testActor");
actorRef.tell("123",ActorRef.noSender());
}
}