MassTransit:在消费者中创建回调队列

时间:2016-04-28 15:26:11

标签: c# masstransit

MassTransit的新手,仍然在玩一些教程项目。我将有一个可以运行大约20分钟的服务,我需要做一些事情。因为它可能需要很长时间,所以我不想遵循请求/响应模式并等待响应,从而阻止了线程。我认为我的另一个选择是创建另一个队列,仅供消费者在作业完成时发布。我看过这篇文章:MassTransit3 how to make request from consumer,但我不确定如何实现这一点。我的项目,再次来自this教程,看起来像这样:

出版商:

static void Main(string[] args)
{
    var bus = Bus.Factory.CreateUsingRabbitMq(x =>
        x.Host(new Uri("rabbitmq://localhost/"), h => {}));
    var busHandle = bus.Start();
    var text = ""'
    Console.WriteLine("Publisher");
    while(text != "quit")
    {
        Console.Write("Enter a message: ");
        text = Console.ReadLine();

        var message = new SomethingHappenedMessage()
        {
            What = text,
            When = DateTime.Now
        }
        bus.Publish(message);
    }
    busHandle.Stop();
}

订户:

static void Main(string[] args)
{
    var bus = Bus.Factory.CreateUsingRabbitMq(x => 
    {
        var host = x.Host(new Uri("rabbitmq://localhost"), h => {});
        x.ReceiveEndpoint(host, "MtPubSubExample_TestSubscriber", e =>
            e.Consumer<SomethingHappenedConsumer>());
    });
    Console.WriteLine("Subscriber");
    var busHandle = bus.Start();
    Console.ReadKey();
    busHandle.Stop();
}

消费者:

class SomethingHappenedConsumer : IConsumer<ISomethingHappened>
{
    public Task Consume(ConsumeContext<ISomethingHappened> context)
    {
        Console.Write("TXT: " + context.Message.What);
        Console.Write("  SENT: " + context.Message.When);
        Console.Write("  PROCESSED: " + DateTime.Now);
        Console.WriteLine(" (" + System.Threading.Thread.CurrentThread.ManagedThreadId + ")");
        return Task.FromResult(0);
    }
}

我如何在消费者中创建回调队列?

2 个答案:

答案 0 :(得分:1)

在您的消费者中,只需Bus.Publish(new ResponseMessage());(或您称之为响应的任何内容),并让您的发布者为该消息类型注册消费者。您的发布者似乎并未绑定到队列,因此只需编写队列名称并将其绑定到队列。

答案 1 :(得分:1)

再次感谢@Travis的帮助。只想展示我最终为所有人提供的最终代码。消息传递对于响应看起来很有趣,但它正确地回发给发布者。

出版商:

static void Main(string[] args)
{
    var bus = Bus.Factory.CreateUsingRabbitMq(x =>
    {
        var host = x.Host(new Uri("rabbitmq://localhost/"), h => { });
        x.ReceiveEndpoint(host, "MtPubSubExample_TestPublisher", e =>
            e.Consumer<ResponseConsumer>());
    });
    var busHandle = bus.Start();
    var text = "";
    Console.WriteLine("Publisher");
    while(text != "quit")
    {
        Console.Write("Enter a message: ");
        text = Console.ReadLine();

        var message = new SomethingHappenedMessage()
        {
            What = text,
            When = DateTime.Now
        };
        bus.Publish(message);
    }

    busHandle.Stop();
}

响应消费者:

class ResponseConsumer : IConsumer<IResponse>
{
    public Task Consume(ConsumeContext<IResponse> context)
    {
        Console.WriteLine("RESPONSE MESSAGE: " + context.Message.Message);
        return Task.FromResult(0);
    }
}

订户:

static void Main(string[] args)
{
    var bus = Bus.Factory.CreateUsingRabbitMq(x =>
    {
        var host = x.Host(new Uri("rabbitmq://localhost/"), h => { });
        x.ReceiveEndpoint(host, "MtPubSubExample_TestSubscriber", e =>
            e.Consumer<SomethingHappenedConsumer>());
    });
    Console.WriteLine("Subscriber");
    var busHandle = bus.Start();
    Console.ReadKey();
    busHandle.Stop();
}

订阅者消费者:

class SomethingHappenedConsumer : IConsumer<ISomethingHappened>
{
    private IBusControl bus = Bus.Factory.CreateUsingRabbitMq(x =>
        x.Host(new Uri("rabbitmq://localhost/"), h => { }));

    public Task Consume(ConsumeContext<ISomethingHappened> context)
    {
        var now = DateTime.Now;
        Console.Write("TXT: " + context.Message.What);
        Console.Write("  SENT: " + context.Message.When);
        Console.Write("  PROCESSED: " + now);
        Console.WriteLine(" (" + System.Threading.Thread.CurrentThread.ManagedThreadId + ")");

        var response = new ResponseMessage()
        {
            Message = "The request was processed at " + now
        };

        bus.Publish(response);
        return Task.FromResult(0);
    }
}