Reputation: 13
ActiveMQ NMS consumer (C#) can't able to receive old messages: My C# program will be in a while loop operating on message received.
I'm establishing a NMS consumer connection each time when I need a message and operate on the message received.
The problem is whenever I start the program the messages posted after my programs 1st connection attempt, I can get them downloaded/consumed.
However, if no messages are flowing in and I have some old messages sitting before I establish 1st connection, those messages are not getting consumed. I used proper connection.start()
. and I'm using consumer.receive(0) 0 - waittime.
Upvotes: 1
Views: 1498
Reputation: 795
NNS consumer example contains the following line:
// Consume a message
ITextMessage message = consumer.Receive() as ITextMessage;
When running this code it may look as if consumer is returning null
(but it doesn't).
Problem here is that consumer.Receive()
returns an IMessage
which is not always of type ITextMessage
. In my case it was returning ActiveMQBytesMessage
which is completely different type, and converting it to ITextMessage
in as ITextMessage
returns null.
The following code would be more appropriate as an example:
// Consume a message
IMessage message = consumer.Receive();
Upvotes: 1
Reputation: 18356
Any time you call a consumer receive with no timeout you are running the risk of not getting a message and must be prepared for that. The consumer doesn't query the broker for a message on a call to receive so if there hasn't been a message dispatched or it's still in flight then the receive will return null message.
Creating a new connection on each attempt to get a message is really an anti-pattern and you should consider using a long lived connection / consumer to avoid this situation more, although you can't completely mitigate this as you are doing a receive(0).
Upvotes: 0