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.关键点
- 消息传递:Actor A完成任务后,通过
Tell
方法发送消息给Actor B。 - 异步通信:Actor之间的通信是异步的,不会阻塞对方的执行。
- 解耦: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();
}
}
}
运行结果如下:
后续会继续更新