我想使用 MassTransit + RabbitMq 总线来调度来自总线的消息。我编写了两个 C# 控制台应用程序,一个用于消息创建者并将消息发送到调度程序,另一个用于消息使用者。
以下代码用于在总线中进行调度,以便每秒向调度程序发送一条消息,然后调度程序以 10 秒的延迟发送给消费者。我的问题是在rabbitMq 客户端没有消息发送到消费者或消费者队列。我的错误在哪里?
注意: UseInMemoryScheduler 工作正常,但 UseMessageScheduler 不起作用。
总线消息创建器
class Program
{
public static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
Console.ReadKey();
}
static async Task MainAsync(string[] args)
{
var busControl = Bus.Factory.CreateUsingRabbitMq(rabbit =>
{
var host = rabbit.Host(new Uri("rabbitmq://localhost:5672"), settings =>
{
settings.Username("guest");
settings.Password("guest");
});
//rabbit.UseInMemoryScheduler(); // This works
rabbit.UseMessageScheduler(new Uri("rabbitmq://localhost/quartz"));// This doesn't work,
});
busControl.Start();
var sendEndpoint = await busControl.GetSendEndpoint(new Uri("rabbitmq://localhost/quartz"));
for (int i = 0; i < 1000000; i++)
{
await sendEndpoint.ScheduleSend(new Uri("rabbitmq://localhost/publisher"),
DateTime.Now.AddSeconds(10),
new MessageCreated()
{
Text = $"message {i}"
});
Thread.Sleep(1000);
}
Console.ReadKey();
busControl.Stop();
}
}
消息消费者。
class Program
{
static void Main(string[] args)
{
var busControl = Bus.Factory.CreateUsingRabbitMq(rabbit =>
{
var host = rabbit.Host(new Uri("rabbitmq://localhost:5672"), settings =>
{
settings.Password("guest");
settings.Username("guest");
});
rabbit.ReceiveEndpoint(host, "publisher", conf =>
{
conf.Consumer<Consumer>();
});
});
busControl.Start();
Console.ReadKey();
busControl.Stop();
}
}
杨__羊羊
Cats萌萌
相关分类