代码
参考c#+redis stream实现消息队列以及ack机制文章的思路,实现
SubscribeAttribute.cs
using System;
namespace DotnetQueue.Attributes
{
/// <summary>
/// 订阅特性
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class SubscribeAttribute : Attribute
{
/// <summary>
/// 订阅的名称
/// </summary>
public string Name {
get; }
/// <summary>
/// 构造函数注入
/// </summary>
/// <param name="name"></param>
public SubscribeAttribute(string name)
{
Name = name;
}
/// <summary>
/// 构造函数注入
/// </summary>
/// <param name="name"></param>
public SubscribeAttribute(string name, string groupName, string consumerName)
{
Name = name;
GroupName = groupName;
ConsumerName = consumerName;
}
/// <summary>
/// 群组名称
/// </summary>
public string GroupName {
get; private set; } = "group01";
/// <summary>
/// 消费者名称
/// </summary>
public string ConsumerName {
get; private set; } = "consumer01";
}
}
新建SubscribeMethod.cs
using System;
using System.Reflection;
namespace DotnetQueue
{
/// <summary>
/// 订阅的方法
/// </summary>
public class SubscribeMethod
{
/// <summary>
/// 类的类型
/// </summary>
public Type ClassType {
get; set; }
/// <summary>
/// 方法
/// </summary>
public MethodInfo MethodInfo {
get; set; }
}
}
新建DotnetQueueCore.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using FreeRedis;
using DotnetQueue.Attributes;
namespace DotnetQueue
{
/// <summary>
/// 消息队列核心
/// </summary>
public class DotnetQueueCore
{
private readonly CancellationToken cancellationToken;
private readonly IRedisClient redisClient;
/// <summary>
/// 构造函数注入
/// &l