my understanding is that when specifying AcknowledgementMode.ClientAcknowledge in the producer, i should be able to stop and restart the consumer and the broker will resend all the unacknowledged messages on the topic. i can't get this to work however. (i've also tried using an onMessage listener in the consumer but i get the same behavior.) any assistance is appreciated. here's my producer:
static void Main(string[] args) {
IConnectionFactory factory = new NMSConnectionFactory("tcp://localhost:61616");
using (IConnection connection = factory.CreateConnection()) {
connection.ClientId = "producer";
using (ISession session = connection.CreateSession(AcknowledgementMode.ClientAcknowledge)) {
IDestination destination = SessionUtil.GetDestination(session, "MY_TOPIC", DestinationType.Topic);
IMessageProducer producer = session.CreateProducer(destination);
while (!Console.KeyAvailable) {
string message = "[" + DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm:ss.fff") + "]";
ITextMessage xtext_message = session.CreateTextMessage(message);
producer.Send(xtext_message, MsgDeliveryMode.Persistent, MsgPriority.Normal, new TimeSpan(1, 0, 0));
Console.WriteLine("Sent message:" + message);
Thread.Sleep(1000);
}
}
}
}
here's my consumer:
static void Main(string[] args) {
IConnectionFactory factory = new NMSConnectionFactory("tcp://localhost:61616/");
using (IConnection connection = factory.CreateConnection()) {
connection.ClientId = "consumer";
connection.Start();
using (ISession session = connection.CreateSession()) {
IMessageConsumer consumer = session.CreateConsumer(SessionUtil.GetDestination(session, "MY_TOPIC", DestinationType.Topic));
while (!Console.KeyAvailable) {
ITextMessage receivedMsg = consumer.Receive() as ITextMessage;
if (receivedMsg != null) {
Console.WriteLine("--> received: " + receivedMsg.Text);
}
}
}
}
}
Client Acknowledgement has no affect on Message producers, they send a message to the broker and wait for broker in the same manner regardless (only transactional session mode has any affect on producers). The acknowledgement mode affect the consumer of the messages which in the case of client acknowledgement needs to specifically acknowledge messages in order for them to be consumed from the Queue.
If a consumer that is using client acknowledge mode fails to acknowledge and is closed the message that were delivered to it will be dispatched to another consumer if one exists or will simply remain on the queue until another consumer arrives.