七万号·数据平台实战手记

Back

Actix - actor in rust#

Actor 创建并发送和接收消息#

发送,接收处理#

生命周期函数#

可Response的Message#


///
/// 为了可以返回Responses 我们为Responses实现MessageResponse
impl<A,M> MessageResponse<A,M> for Responses
where A:Actor,
        M:Message<Result = Responses> {
    fn handle(self, ctx: &mut <A as Actor>::Context, tx: Option<actix::dev::OneshotSender<<M as Message>::Result>>) {
        if let Some(tx) = tx {
            tx.send(self);
        }
    }
}
rust

两个Actor互相发的结构#

示例互啄术#

Arbiter#
    let sys = System::new();

    let exec = async {
        TheActor.start();
    };

    // 使用Arbiter管理Actor
    let arbiter = Arbiter::new();

    Arbiter::spawn(&arbiter, exec);


    System::current().stop();

    sys.run();
rust
SyncArbiter#

use actix::prelude::*;

struct MySyncActor;

impl Actor for MySyncActor {
    type Context = SyncContext<Self>;
}
// 线程数2则可以有同时两个Actor在处理
let addr = SyncArbiter::start(2, || MySyncActor);
rust

Akka - actor in jvm#

Apache Pekko

创建#
传递消息#
ActorSystem system = ActorSystem.create("linux");
// 创建
ActorRef p1 = system.actorOf(DemoRev.props());
ActorRef s1 = system.actorOf(DemoSend.props());
// s1 -> p1
p1.tell("hello",s1);
system.terminate();
java

Inbox 消息#


ActorSystem system = ActorSystem.create("linux");
ActorRef p1 = system.actorOf(DemoRev.props());

final Inbox inbox = Inbox.create(system);
// inbox也是一个actor
inbox.send(p1,"hello");

System.out.println(inbox.receive(Duration.ofSeconds(1)));
system.terminate();
java

周期性消息#

生命周期#

Receive#

ask , pipie#

关于actor那些不得不说的故事
https://realcpf.tech/blog/the-actor
Author 刘佳成
Published at 2024年2月29日