我正在与ActiveMQ服务通信的.NET应用程序中,尝试实现最基本的功能:
MaxRedeliveries
。我很确定这是ActiveMQ的开箱即用行为,但这对我来说根本行不通。根据{{3}}的代码,我得出了以下示例:
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Spring.Messaging.Nms.Core;
using Spring.Messaging.Nms.Listener;
using System;
namespace MqListener
{
class Program
{
private const string URI = "tcp://localhost:61616";
private const string DESTINATION = "test.queue";
static void Main(string[] args)
{
Publish();
Subscribe();
}
private static void Publish()
{
var connectionFactory = new ConnectionFactory(URI);
var template = new NmsTemplate(connectionFactory);
template.ConvertAndSend(DESTINATION, "Hello from the sender.");
}
private static void Subscribe()
{
try
{
var connectionFactory = new ConnectionFactory(URI);
connectionFactory.RedeliveryPolicy.MaximumRedeliveries = 10;
using (var listenerContainer = new SimpleMessageListenerContainer())
{
listenerContainer.ConnectionFactory = connectionFactory;
listenerContainer.DestinationName = DESTINATION;
listenerContainer.MessageListener = new Listener();
listenerContainer.AfterPropertiesSet();
Console.WriteLine("Listener started.");
Console.WriteLine("Press <ENTER> to exit.");
Console.Read();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine("Press <ENTER> to exit.");
Console.Read();
}
}
}
internal class Listener : IMessageListener
{
public Listener()
{
Console.WriteLine("Listener created");
}
public void OnMessage(IMessage message)
{
var textMessage = (Apache.NMS.ActiveMQ.Commands.ActiveMQTextMessage)message;
Console.WriteLine($"{textMessage.NMSXDeliveryCount} {message.NMSRedelivered} {textMessage.Text}");
throw new Exception(); // I want to emulate the failure during the processing
}
}
}
使用此代码,消息已成功发布,然后传递给侦听器,但是无论我做什么,ActiveMQ都会将其视为已成功传递。但是,有几次,我设法使它重新发送相同的消息两次(第一次使用message.NMSRedelivered = false
,第二次使用message.NMSRedelivered = true
),但是没有更多。最糟糕的是,我什至不知道哪个“调整”会产生这种效果。
所以问题是,我是否误解了ActiveMQ的整个概念,还是错过了一些非常基础的东西? 我将不胜感激!