如果您使用相同的端點名稱創建發佈者和使用者,我們遇到MassTransit正在丟失消息的情況。MassTransit丟失消息 - Rabbit MQ - 當發佈者和消費者端點名稱相同時,
請注意下面的代碼;如果我爲使用者或發佈者使用了不同的端點名稱(例如,發佈者的「rabbitmq:// localhost/mtlossPublised」),則該消息會統計發佈和使用的匹配;如果我使用相同的端點名稱(如示例中所示),那麼消耗的消息數少於發佈的消息數。
這是預期的行爲?或者我做錯了什麼,在下面的工作示例代碼。
using MassTransit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MTMessageLoss
{
class Program
{
static void Main(string[] args)
{
var consumerBus = ServiceBusFactory.New(b =>
{
b.UseRabbitMq();
b.UseRabbitMqRouting();
b.ReceiveFrom("rabbitmq://localhost/mtloss");
});
var publisherBus = ServiceBusFactory.New(b =>
{
b.UseRabbitMq();
b.UseRabbitMqRouting();
b.ReceiveFrom("rabbitmq://localhost/mtloss");
});
consumerBus.SubscribeConsumer(() => new MessageConsumer());
for (int i = 0; i < 10; i++)
publisherBus.Publish(new SimpleMessage() { CorrelationId = Guid.NewGuid(), Message = string.Format("This is message {0}", i) });
Console.WriteLine("Press ENTER Key to see how many you consumed");
Console.ReadLine();
Console.WriteLine("We consumed {0} simple messages. Press Enter to terminate the applicaion.", MessageConsumer.Count);
Console.ReadLine();
consumerBus.Dispose();
publisherBus.Dispose();
}
}
public interface ISimpleMessage : CorrelatedBy<Guid>
{
string Message { get; }
}
public class SimpleMessage : ISimpleMessage
{
public Guid CorrelationId { get; set; }
public string Message { get; set; }
}
public class MessageConsumer : Consumes<ISimpleMessage>.All
{
public static int Count = 0;
public void Consume(ISimpleMessage message)
{
System.Threading.Interlocked.Increment(ref Count);
}
}
}
Travis,Binary Worrier。感謝您的投入。感謝鏈接到Travis文檔的鏈接;我以前從未見過;我原以爲我已經閱讀了網站上的所有文檔。所有這些顯然沒有沉入:) – Bigtoe
@Bigtoe如果您對如何通過更明確的方式使第一次錯過的任何想法有任何想法,我們很樂意聽取您的意見。無論什麼事情都可以讓人們更容易地接觸MT,這很棒。 – Travis
鏈接現已停止 – Don