簡體   English   中英

MassTransit:未創建隊列以進行主題交換

[英]MassTransit: Queue is not created for topic exchange

我想使用topic交換將股價數據發送到rabbitmq。 我的想法是使用以下路由鍵進行主題交換:

<message-type>.<ticker>

我能夠使用本機RabbitMQ cient做到這一點,但是我不知道如何在MassTransit的幫助下做到這一點。

// setup topologies
rabbitCfg.Send<ComMessage>(x =>
{
   x.UseRoutingKeyFormatter(context => 
        $"quote.{context.Message.Ticker}");
});

rabbitCfg.Message<ComMessage>(x => x.SetEntityName("Quotes"));
rabbitCfg.Publish<ComMessage>(x =>
{
   x.ExchangeType = ExchangeType.Topic;
});


// setup reciever
rabbitCfg.ReceiveEndpoint(host, "MSFT", e =>
{
   e.Bind("Quotes", c =>
      {
         c.RoutingKey = "quote.MSFT";
         c.ExchangeType = ExchangeType.Topic;
      });

   e.Consumer<PriceConsumer>();
});

發信息:

await _bus.Publish(new ComMessage
{
   Ticker = "MSFT",
   Price = "10"
});

但是,它不起作用。 隊列未創建,但交換接收消息:

在此處輸入圖片說明

哪里有問題?

我認為您忘記了一條重要的路線。 作為參考,我提供了使用主題交流的工作單元測試的源代碼。

在接收端點中,您需要禁用自動交換綁定。

cfg.ReceiveEndpoint(host, "MSFT", x =>
{
    x.BindMessageExchanges = false;
    ...
}

一個工作示例如下所示:

using System;
using System.Threading.Tasks;
using GreenPipes.Util;
using NUnit.Framework;
using RabbitMQ.Client;
using RoutingKeyTopic;


namespace RoutingKeyTopic
{
    public class Message
    {
        public Message(decimal price, string symbol)
        {
            Price = price;
            Symbol = symbol;
        }

        public string Symbol { get; set; }

        public decimal Price { get; set; }
    }
}


[TestFixture]
public class Using_a_routing_key_and_topic_exchange :
    RabbitMqTestFixture
{
    [Test]
    public async Task Should_support_routing_by_key_and_exchange_name()
    {
        var fooHandle = await Subscribe("MSFT");
        try
        {
            var barHandle = await Subscribe("UBER");
            try
            {
                await Bus.Publish(new Message(100.0m, "MSFT"));
                await Bus.Publish(new Message(3.50m, "UBER"));

                await Consumer.Microsoft;
                await Consumer.Uber;
            }
            finally
            {
                await barHandle.StopAsync(TestCancellationToken);
            }
        }
        finally
        {
            await fooHandle.StopAsync(TestCancellationToken);
        }
    }

    async Task<HostReceiveEndpointHandle> Subscribe(string key)
    {
        var queueName = $"Stock-{key}";
        var handle = Host.ConnectReceiveEndpoint(queueName, x =>
        {
            x.BindMessageExchanges = false;
            x.Consumer<Consumer>();

            x.Bind<Message>(e =>
            {
                e.RoutingKey = GetRoutingKey(key);
                e.ExchangeType = ExchangeType.Topic;
            });
        });

        await handle.Ready;

        return handle;
    }

    protected override void ConfigureRabbitMqBusHost(IRabbitMqBusFactoryConfigurator configurator, IRabbitMqHost host)
    {
        base.ConfigureRabbitMqBusHost(configurator, host);

        configurator.Message<Message>(x => x.SetEntityName(ExchangeName));
        configurator.Publish<Message>(x => x.ExchangeType = ExchangeType.Topic);

        configurator.Send<Message>(x => x.UseRoutingKeyFormatter(context => GetRoutingKey(context.Message.Symbol)));
    }

    string ExchangeName { get; } = "Quotes";

    string GetRoutingKey(string routingKey)
    {
        return $"quote.{routingKey}";
    }


    class Consumer :
        IConsumer<Message>
    {
        static readonly TaskCompletionSource<Message> _microsoft = new TaskCompletionSource<Message>();
        static readonly TaskCompletionSource<Message> _uber = new TaskCompletionSource<Message>();
        public static Task<Message> Microsoft => _microsoft.Task;
        public static Task<Message> Uber => _uber.Task;

        public Task Consume(ConsumeContext<Message> context)
        {
            Console.WriteLine($"Received {context.Message.Symbol} for {context.RoutingKey()}");

            if (context.Message.Symbol == "MSFT")
                _microsoft.TrySetResult(context.Message);

            if (context.Message.Symbol == "UBER")
                _uber.TrySetResult(context.Message);

            return TaskUtil.Completed;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM