我有一个名为“ IntegrationEvent”的Azure ServiceBus主题,具有一个名为“ TestSubscription”的一个订阅。我注册了两个具有 CompetingConsumers 订阅者。
两个订户都处理许多消息。我需要更改以确保不再发生这种情况?我以为每条消息应该只由一个订户处理?
发件人:
class Program
{
static async Task Main(string[] args)
{
await SendMessageAsync();
}
private static async Task SendMessageAsync()
{
var sendClient = new TopicClient("ConnectionString");
var testBlock = new ActionBlock<string>(
async id =>
{
string jsonMessage = JsonConvert.SerializeObject(id);
byte[] body = Encoding.UTF8.GetBytes(jsonMessage);
var messageToSend = new Message(body)
{
CorrelationId = id,
};
await sendClient.SendAsync(messageToSend);
}, new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 25
});
for (int i = 0; i < 10000; i++)
{
testBlock.Post(Guid.NewGuid().ToString());
}
testBlock.Complete();
await testBlock.Completion;
}
}
我使用两个订阅者/消费者(不是订阅)来监听IntegrationEvent。
class Program
{
static SubscriptionClient subscriptionClient;
static async Task Main(string[] args)
{
var builder = new ServiceBusConnectionStringBuilder("ConnectionString");
if (string.IsNullOrWhiteSpace(builder.EntityPath))
{
builder.EntityPath = "IntegrationEvent";
}
subscriptionClient = new SubscriptionClient(builder, "TestSubscription");
await subscriptionClient.RemoveRuleAsync(RuleDescription.DefaultRuleName);
await subscriptionClient.AddRuleAsync(new RuleDescription(RuleDescription.DefaultRuleName, new TrueFilter()));
ListenForMessages();
Console.Read();
}
protected static void ListenForMessages()
{
var options = new MessageHandlerOptions(ExceptionReceivedHandler)
{
AutoComplete = false,
MaxConcurrentCalls = 10
};
subscriptionClient.RegisterMessageHandler(ReceiveMessageAsync, options);
}
private static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs arg)
{
return Task.CompletedTask;
}
private static async Task ReceiveMessageAsync(Message arg1, CancellationToken arg2)
{
string integrationEvent = Encoding.UTF8.GetString(arg1.Body);
Console.WriteLine($"{ arg1.MessageId}, { arg1.CorrelationId}, {integrationEvent}");
await subscriptionClient.CompleteAsync(arg1.SystemProperties.LockToken);
}
}
答案 0 :(得分:0)
在尝试任何操作之前,了解非常重要。 documentation与此有关。具体来说,rules会影响订阅将要收到的内容。
现在,您的代码已设置为接收所有消息(捕获所有规则)。使用SQL或“相关性”过滤器创建更具体的规则后,您将可以控制每个单独的订阅将收到的内容。我不久前写了post,其中包含一些信息。