4
votes

I am using a JMS client which receives JMS messages from a remote server. I am listening to the JMS message in the onMessage() method of the client.

The issue I am facing is that, messages are getting accumulated on the Server side even while I am consuming the messages on Client side at regular interval.

I send a rollback() or commit() depending upon the processing I am doing on the client's end.

I have a query that while I am processing the JMS message and I have not sent a commit() to the queue, can I get another message from the server.

Please note that for the processing, I am doing a wait for the processing of the message received in a synchronized block. Will I get a new message while I am still processing and I have not sent a commit to the queue?

2

2 Answers

3
votes

MessageListener#onMessage() is executed as part of session thread, so you will not receive next message until return from onMessage().

In general, you should not process messages in onMessage(), if it is expected to take more time. Enqueue message into separate data structure and have some other thread process it.

You do not have to call commit() after every message, you can continue to receive message.

No. of message received without calling commit/rollback depends on client buffer size - usually configurable at queue/global.

Please note that for the processing, I am doing a wait for the processing of the message received in a synchronized block. Will I get a new message while I am still processing and I have not sent a commit to the queue?

If you are processing in onMessage() you will not receive new message, so no need to have synchronized block inside onMessage().

2
votes

If you have created a single session and created a message consumer and set your message listener instance then as per the java docs only one instance of your message listener will get message at any given point of time as the session itself takes care of serializing across multiple listeners if registered for the same session.

"The session used to create the message consumer serializes the execution of all message listeners registered with the session. At any time, only one of the session’s message listeners is running."

JMS MessageListeners

"Each session must insure that it passes messages serially to the listener. This means that a listener assigned to one or more consumers of the same session can assume that the onMessage method is not called with the next message until the session has completed the last call. "

JMS MessageListener

In short for your query:-

I have a query that while I am processing the JMS message and I have not sent a commit() to the queue, can I get another message from the server.

Answer is no if you have a single session created even if you have created multiple instances of listeners.

However if you create multiple sessions and create multiple instances of your message listener and register them then they may receive multiple messages at same time per session

Also for your problem where you are consuming messages but still messages are getting accumulated , you need to create multiple sessions for concurrency which will improve your message processing rate and hence reduce the build up of messages in server