Skip to main content
edited tags
Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157
Source Link

JMS configuration -- client exclusive messages

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