I have a .NET service that uses the ActiveMQ client. I have implemented a MessageListener with a transacted connection to consume the messages.
Occasionally, I get messages in a different order in which they were put onto the queue.
Was it wrong to use a MessageListner? Is there a way to preserve the message order?
FYI: There is one producer putting messages on the queue and one consumer pulling messages off the queue.
More Info: The server I call is third party and I have no control over what messages it sends. I do know, based on the message ID’s that they are put on in the right order. Also, in this system there is a difference between ACK and Acknowledge. The ACK is received when my original message is put on the the ‘outbound’ queue. I then monitor an ‘inbound’ queue for responses. I usually get an ‘Acknowledge’ followed by a ‘Pass’ or a ‘Fail’. The correlation ID for these messages is the message ID from the ACK.
I had originally used a message listener and responded to an OnMessge event. I abandoned this approach after realizing that, using this method, messages are delivered asynchronously and therefore in no particular order. So, I changed my code to poll using a timer (System.Threading.Timer) to call consumer.Receive() and get one message at a time. This works the way I want.
I open the consumer once when the service starts and I continuously poll for messages.