0

I am new JMS arena, I have created multiple message listeners but one listener per thread. I have following message listener, This message listner is passed a session object while it is constructed. Then when the message is received then this message listener processes message then commits the session as shown in the code. Closing sessions/connections is done in shutdown hook.

    public class MyMessageListener implements MessageListener, ExceptionListener {
        private Session _session = null;

        public MyMessageListener(Session s) {
            _session = s;
        }

        @Override
        public void onMessage(Message arg0) {
            try {

                // Retrieve the data
                JMSBytesMessage bytesMessage = (JMSBytesMessage) arg0;
                byte[] payload = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(payload);
                String payloadString = new String(payload);
                ProcessMessage(payloadString);
                _session.commit();
                MessageCounter.Increment();
            } catch (Exception e) {
                e.printStackTrace();
            } 
        }


    public abstract void ProcessMessage(String theMessage);

    }

Now my worry is that if listener receives two messages at the same time, will there be a chance the session commits both messages?

Oliv
  • 10,221
  • 3
  • 55
  • 76
K.M
  • 154
  • 3
  • 11

1 Answers1

1

Everything from Session on down is single-threaded. No worry about multi-threading. See How to temporarily disable a message listener (different problem, similar issues).

You do create a separate session for each listener, right? That's a requirement. Part of the "everything from Session on down is single-threaded".

Community
  • 1
  • 1
John M
  • 13,053
  • 3
  • 27
  • 26