2025-04-15 11:15:22 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus.Kafka.Attributes
|
|
|
|
|
|
{
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method)]
|
|
|
|
|
|
public class KafkaSubscribeAttribute : Attribute
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 订阅的主题
|
|
|
|
|
|
/// </summary>
|
2025-04-16 09:54:21 +08:00
|
|
|
|
public string Topic { get; set; }
|
2025-04-15 11:15:22 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 分区
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int Partition { get; set; } = -1;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 消费者组
|
|
|
|
|
|
/// </summary>
|
2025-04-15 15:49:22 +08:00
|
|
|
|
public string GroupId { get; set; }
|
2025-04-15 11:15:22 +08:00
|
|
|
|
|
2025-04-16 09:54:21 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 任务数(默认是多少个分区多少个任务)
|
2025-04-16 18:26:25 +08:00
|
|
|
|
/// 如设置订阅指定Partition则任务数始终为1
|
2025-04-16 09:54:21 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int TaskCount { get; set; } = -1;
|
2025-04-15 11:15:22 +08:00
|
|
|
|
|
2025-04-16 18:26:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 批量处理数量
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public int BatchSize { get; set; } = 100;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否启用批量处理
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool EnableBatch { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 批次超时时间
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public TimeSpan? BatchTimeout { get; set; }=null;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 订阅主题
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="batchTimeout">batchTimeout格式:("00:05:00")</param>
|
|
|
|
|
|
public KafkaSubscribeAttribute(string topic, string groupId = "default", bool enableBatch = false, int batchSize = 100, string? batchTimeout = null)
|
2025-04-15 11:15:22 +08:00
|
|
|
|
{
|
2025-04-16 09:54:21 +08:00
|
|
|
|
this.Topic = topic;
|
2025-04-15 11:15:22 +08:00
|
|
|
|
this.GroupId = groupId;
|
2025-04-16 18:26:25 +08:00
|
|
|
|
this.EnableBatch = enableBatch;
|
|
|
|
|
|
this.BatchSize = batchSize;
|
|
|
|
|
|
this.BatchTimeout = batchTimeout != null? TimeSpan.Parse(batchTimeout): null;
|
2025-04-15 11:15:22 +08:00
|
|
|
|
}
|
2025-04-15 15:49:22 +08:00
|
|
|
|
|
2025-04-16 18:26:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 订阅主题
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="batchTimeout">batchTimeout格式:("00:05:00")</param>
|
|
|
|
|
|
public KafkaSubscribeAttribute(string topic, int partition, string groupId = "default", bool enableBatch = false, int batchSize = 100, string? batchTimeout = null)
|
2025-04-15 11:15:22 +08:00
|
|
|
|
{
|
2025-04-16 18:26:25 +08:00
|
|
|
|
this.Topic = topic;
|
2025-04-15 11:15:22 +08:00
|
|
|
|
this.Partition = partition;
|
|
|
|
|
|
this.GroupId = groupId;
|
2025-04-16 18:26:25 +08:00
|
|
|
|
this.TaskCount = 1;
|
|
|
|
|
|
this.EnableBatch = enableBatch;
|
|
|
|
|
|
this.BatchSize = batchSize;
|
|
|
|
|
|
this.BatchTimeout = batchTimeout != null ? TimeSpan.Parse(batchTimeout) : null;
|
2025-04-15 11:15:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|