5
\$\begingroup\$

I am SURE this is somehow easy but reading the docs isn't bringing it to light.

The problem: I am trying to setup a JMS message listener and what I need is for the first client to have exclusive message processing rights.

That is: Until a single client acknowledges the message the server will not notify any other consumers of the message.

I believe this is correct but I am looking for some verification. The whole 'transacted' part is throwing me off

As an aside: I posted it to regular SOF and got told to repost here.

  public class QueueMessengerMessageListener implements MessageListener {

    protected Logger log = LoggerFactory.getLogger(getClass());

    private Connection connection;

    @Autowired
    private Queue q;

    @Autowired
    private ConnectionFactory connectionFactory;

    @Autowired
    private DWHService dwhService;

    @PostConstruct
    private void listen() throws JMSException {
        connection = connectionFactory.createConnection();
        Session jmsSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        MessageConsumer consumer = jmsSession.createConsumer(q);
        consumer.setMessageListener(this);
        connection.start();
    }

    @PreDestroy
    public void destroy() throws JMSException {
        connection.close();
    }

    @Override
    @Transactional
    public void onMessage(Message m) {
        BytesMessage jmsMessage = (BytesMessage) m;
        MyObject userMessage = null;
        try {
            byte[] in = new byte[(int) jmsMessage.getBodyLength()];
            jmsMessage.readBytes(in);
            ObjectInputStream inStream = new ObjectInputStream(new ByteArrayInputStream(in));
            userMessage = (MyObject) inStream.readObject();
            dwhService.addDwhEntry(userMessage);
            m.acknowledge();

        }
         catch (Exception e) {
            /* I normally wouldn't catch *all* exceptions, but the JMS tutorial says Thou Shalt Not throw
             * a RuntimeException from this method. */
             log.error("Error sending message {}",  e.getMessage());
        }
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Sorry for pointing back to Stack Overflow, but I guess asking there about how Session.CLIENT_ACKNOWLEDGE and @Transactional work together will get you (better) answers. \$\endgroup\$ Commented Mar 9, 2014 at 0:09
  • \$\begingroup\$ I think Code Review is the best place for this particular question as it is currently phrased, let's hope that some expert in this area can come here and give some two cents about it. I personally haven't used Java Messaging Service (JMS) at all before. \$\endgroup\$ Commented Mar 9, 2014 at 0:12
  • \$\begingroup\$ But asking a specific and good SO question that is on-topic for SO will likely give you more knowledge about the workings of this, and might be more helpful for you. \$\endgroup\$ Commented Mar 9, 2014 at 0:13

1 Answer 1

1
\$\begingroup\$

The code looks fine although I'm not familiar with JMS, so I can't say anything about it's correctness. Just three minor notes:

  1. userMessage could be declared inside the try block:

    try {
        byte[] in = new byte[(int) jmsMessage.getBodyLength()];
        jmsMessage.readBytes(in);
        ObjectInputStream inStream = new ObjectInputStream(new ByteArrayInputStream(in));
        MyObject userMessage = (MyObject) inStream.readObject();
    ...
    

    (Effective Java, Second Edition, Item 45: Minimize the scope of local variables)

  2. protected Logger log = LoggerFactory.getLogger(getClass());
    

    The logger could be private.

    (Item 13 of Effective Java 2nd Edition: Minimize the accessibility of classes and members.)

  3. The logger also could be static. There is a good template for Eclipse for generating getLogger calls with MyType.class classes instead of getClass():

    private static final Logger log = LoggerFactory.getLogger(QueueMessengerMessageListener.class);
    
\$\endgroup\$
1
  • \$\begingroup\$ You're right on both accounts. I ripped off this code from another example I had. \$\endgroup\$ Commented Mar 10, 2014 at 22:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.