简体   繁体   中英

ActiveMQ NMS consumer not receiving message at certain scenarios in C#

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.

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();

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM