Rabbitmq 为什么MassTransit只在一个线程上使用消息
我在一个常规的控制台应用程序中有以下简单的代码,并且希望(某些)命令能够并行使用。我认为Rabbitmq 为什么MassTransit只在一个线程上使用消息,rabbitmq,masstransit,Rabbitmq,Masstransit,我在一个常规的控制台应用程序中有以下简单的代码,并且希望(某些)命令能够并行使用。我认为useConcurrencyLit将设置并发线程的数量。我看到的是RabbitMQ确实有10条未确认的消息,但是使用者连续使用它们,每个console.writeline之间有一秒钟的暂停。我肯定错过了一些明显的东西,但我不明白 public static class EventHandler { public static void Run() { var personQue
useConcurrencyLit
将设置并发线程的数量。我看到的是RabbitMQ确实有10条未确认的消息,但是使用者连续使用它们,每个console.writeline之间有一秒钟的暂停。我肯定错过了一些明显的东西,但我不明白
public static class EventHandler
{
public static void Run()
{
var personQueueName = "RabbitMqPoc.Person";
var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
{
cfg.UseConcurrencyLimit(10);
var host = cfg.Host(new Uri("rabbitmq://localhost"), h =>
{
h.Username("guest");
h.Password("guest");
});
cfg.ReceiveEndpoint(host, personQueueName, e =>
{
e.UseConcurrencyLimit(10);
e.PrefetchCount = 10;
e.Consumer<PersonConsumer>();
});
});
var personSendEndpoint = busControl.GetSendEndpoint(new Uri($"rabbitmq://localhost/{personQueueName}")).Result;
busControl.Start();
foreach (var x in Enumerable.Range(1, 20))
{
personSendEndpoint.Send(new PersonUpdated() { Name = "Mina Ives", Key = Guid.NewGuid() });
}
Console.ReadLine();
busControl.Stop();
}
}
internal class PersonConsumer : IConsumer<IPersonUpdated>
{
public async Task Consume(ConsumeContext<IPersonUpdated> context)
{
Thread.Sleep(1000);
Console.WriteLine($"Updated {context.Message.Name}");
}
}
公共静态类EventHandler
{
公共静态无效运行()
{
var personQueueName=“RabbitMqPoc.Person”;
var busControl=Bus.Factory.CreateUsingRabbitMq(cfg=>
{
cfg.UseConcurrencyLimit(10);
var host=cfg.host(新Uri(“rabbitmq://localhost,h=>
{
h、 用户名(“客人”);
h、 密码(“客人”);
});
ReceiveEndpoint(主机,personQueueName,e=>
{
e、 使用并发限制(10);
e、 预取计数=10;
e、 消费者();
});
});
var personSendEndpoint=busControl.GetSendEndpoint(新Uri($)rabbitmq://localhost/{personQueueName}”))。结果;
busControl.Start();
foreach(可枚举范围(1,20)中的var x)
{
Send(newpersonUpdate(){Name=“Mina-Ives”,Key=Guid.NewGuid()});
}
Console.ReadLine();
busControl.Stop();
}
}
内部类PersonConsumer:IConsumer
{
公共异步任务使用(使用上下文)
{
睡眠(1000);
WriteLine($“Updated{context.Message.Name}”);
}
}
更改线程睡眠(1000)代码>等待任务。延迟(1000)代码>解决您看到的问题
由于某种原因,睡眠会对TPL造成严重破坏