当前位置: 首页 > article >正文

3.1 actor基本框架(c#的Akka.Actor模式)

1.最简单的一个框架

代码如下(代码容易理解):

using System;
using Akka.Actor;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApp11
{
    // 定义Actor
    public class MyActor:ReceiveActor
    {
        public MyActor()
        {

        // 定义接收消息的行为
        Receive<string>(message =>
            {
                // 定义接收消息的行为
                Console.WriteLine($"收到消息:{message}");
            }
                );
        }
    }
    


    class Program
    {
        static void Main(string[] args)
        {
            // 创建 Actor系统
            var actorSystem = ActorSystem.Create("MyActorSystem");

            // 创建一个Actor
            var myActor = actorSystem.ActorOf<MyActor>("MyActor");

            // 向Actor 发送消息
            myActor.Tell("hello,actor!");

            // 停止 Actor 系统
            actorSystem.Terminate().Wait();
        }
    }
}

2.加入一个数据处理的功能

代码如下:

using System;
using Akka.Actor;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApp11
{
    public class MyActor : ReceiveActor 
    { 
        public MyActor() 
        { 
            Receive<string>(message => 
            { 
                Console.WriteLine($"Received: {message}");  // 输出: Received: 1
            }); 
        }
        protected override void PreStart() => Console.WriteLine("Actor started");  //actor的重启,  输出: Actor started
        protected override void PostStop() => Console.WriteLine("Actor stopped");  //actor的停止,  输出: Actor stopped

    }


    // 添加信息获取和信息处理类
    public class cacus
    {
        public cacus() { }

        public double tt = 1; // 这里可以获取数据

        public double cas() // 
        {
            return tt;
        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            // 1.创建 Actor系统
            var actorSystem = ActorSystem.Create("MyActorSystem");

            // 2.创建一个Actor
            var myActor = actorSystem.ActorOf<MyActor>("MyActor");

            // 3.获得myActor的路径(每个myActor都有自己的路径)
            var actorPath = myActor.Path;
            Console.WriteLine($"Actor Path: {actorPath}"); // 输出: Actor Path: akka://MyActorSystem/user/MyActor

            // 4.向Actor 发送消息
            var st = new cacus();
            var ss = st.cas();
            string s1 = ss.ToString();
            myActor.Tell(s1);

            // 5.停止 Actor 系统
            actorSystem.Terminate().Wait();
        }
    }
}

运行结果如下:

3.两个actor同时运行

该内容看了博客C# Actor 如何理解和使用?-CSDN博客,作为笔记用。

以下是一个简单的例子,展示如何通过消息传递实现“A完成后再通知B执行”的逻辑。

1. 定义 Actor A 和 Actor B

  • Actor A:完成任务后发送消息给Actor B。
  • Actor B:接收到消息后执行自己的任务。

代码如下:

using System;
using Akka.Actor;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApp11
{
    
    // Actor A
    public class ActorA : ReceiveActor 
    { 
        private readonly IActorRef _actorB;
        public ActorA(IActorRef actorB) 
        { 
            _actorB = actorB; 
            Receive<string>(message => 
            { 
            Console.WriteLine("Actor A is working..."); // 模拟任务完成
            Task.Delay(1000).Wait(); // 模拟耗时操作
            Console.WriteLine("Actor A has finished its task."); // 通知 Actor B
             _actorB.Tell("A has finished, B can start now."); });
        }
    } 

    // Actor B
    public class ActorB : ReceiveActor 
    { 
        public ActorB() 
        { 
            Receive<string>(message => 
            {
            Console.WriteLine("Actor B received a message: " + message); 
            Console.WriteLine("Actor B is working..."); // 模拟任务完成
            Task.Delay(1000).Wait(); // 模拟耗时操作
            Console.WriteLine("Actor B has finished its task."); }); 
        } 
    }


    class Program
    {
        static void Main(string[] args)
        {
            
            //创建 ActorSystem
            var system = ActorSystem.Create("MySystem"); 
            
            // 创建 Actor B
            var actorB = system.ActorOf<ActorB>("actorB"); 

            // 创建 Actor A,并传入 Actor B 的引用
            var actorA = system.ActorOf(Props.Create(() => new ActorA(actorB)), "actorA"); 

            // 触发 Actor A 开始工作
            actorA.Tell("Start"); Console.ReadLine(); 
        }
    }

}

运行结果如下:

2.关键点

  1. 消息传递:Actor A完成任务后,通过Tell方法发送消息给Actor B。
  2. 异步通信:Actor之间的通信是异步的,不会阻塞对方的执行。
  3. 解耦:Actor A和Actor B是完全独立的,它们之间没有直接的依赖,仅通过消息协作。

4.更复杂的协作

内容来自博客C# Actor 如何理解和使用?-CSDN博客,作为笔记用.

如果需要多个Actor协作(例如A -> B -> C),可以通过类似的方式实现:

  • Actor A完成任务后通知Actor B。
  • Actor B完成任务后通知Actor C。

代码如下:

using System;
using Akka.Actor;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography.X509Certificates;

namespace ConsoleApp11
{
    
    // Actor A
    public class ActorA : ReceiveActor 
    { 
        private readonly IActorRef _actorB;
        public ActorA(IActorRef actorB) 
        { 
            _actorB = actorB; 
            Receive<string>(message => 
            { 
            Console.WriteLine("Actor A is working..."); // 模拟任务完成
            Task.Delay(1000).Wait(); // 模拟耗时操作
            Console.WriteLine("Actor A has finished its task."); // 通知 Actor B
             _actorB.Tell("A has finished, B can start now."); });
        }
    } 


    // ctor B,使其完成任务后通知 Actor C
    public class ActorB : ReceiveActor
    { 
        private readonly IActorRef _actorC;
        public ActorB(IActorRef actorC)
        { 
            _actorC = actorC;
            Receive<string>(message => 
            {
            Console.WriteLine("Actor B received a message: " + message); 
            Console.WriteLine("Actor B is working..."); 
            Task.Delay(1000).Wait(); // 模拟耗时操作
            Console.WriteLine("Actor B has finished its task."); // 通知Actor C 
                _actorC.Tell("B has finished, C can start now."); }); 
        }
    }

    // Actor C
    public class ActorC : ReceiveActor 
    {
        public ActorC()
        { 
            Receive<string>(message => 
            { 
            Console.WriteLine("Actor C received a message: " + message);
            Console.WriteLine("Actor C is working..."); 
            Task.Delay(1000).Wait();// 模拟耗时操作
            Console.WriteLine("Actor C has finished its task."); });
        } 
    } 



    class Program
    {
        static void Main(string[] args)
        {

            //创建 ActorSystem
            var system = ActorSystem.Create("MySystem");

            // 创建 Actor C
            var actorC = system.ActorOf<ActorC>("actorC");

            // 创建 Actor B,并传入 Actor C 的引用
            var actorB = system.ActorOf(Props.Create(() => new ActorB(actorC)), "actorB");

            // 创建 Actor A,并传入 Actor B 的引用
            var actorA = system.ActorOf(Props.Create(() => new ActorA(actorB)), "actorA");

            // 触发 Actor A 开始工作
            actorA.Tell("Start"); Console.ReadLine();
        }

    }

}

运行结果如下:

后续会继续更新


http://www.kler.cn/a/554639.html

相关文章:

  • 约束性委派攻击和非约束性委派攻击
  • Vue 3 工程化:从理论到实践 (上篇)
  • DeepSeek在企业中的有那些具体应用?
  • 易基因: ChIP-seq+DRIP-seq揭示AMPK通过调控H3K4me3沉积和R-loop形成以维持基因组稳定性和生殖细胞完整性|NAR
  • jvm中各个参数的理解
  • ROS 2机器人开发--第一个节点
  • HTTP SSE 实现
  • 【清华大学】DeepSeek从入门到精通完整版pdf下载
  • Could not initialize class io.netty.util.internal.Platfor...
  • nginx配置ssl
  • 《Spring实战》(第6版) 第3章 使用数据
  • 前端PDF转图片技术调研实战指南:从踩坑到高可用方案的深度解析
  • 基于Java爬虫获取1688商品分类信息(cat_get接口)的实现指南
  • 常用网络工具分析(ping,tcpdump等)
  • 如何平衡网络服务的成本、质量和可扩展性?
  • Nginx环境安装
  • 工业安卓主板在智慧粮仓设备中发挥着至关重要的作用
  • 黑盒测试、白盒测试、单元测试、集成测试、系统测试、验收测试的区别与联系
  • CVTE面经学习
  • AI训练中的常用指令