Friday, March 30, 2012

Code snippet for JMS queue reader

There is quick snippet how to read from ActiveMQ queue with Groovy:


import org.apache.activemq.pool.PooledConnectionFactory
import javax.jms.Connection
import javax.jms.Session
import org.apache.activemq.command.ActiveMQQueue
import javax.jms.MessageConsumer
import org.springframework.jms.listener.adapter.MessageListenerAdapter
import javax.jms.Message

  PooledConnectionFactory pooledConnectionFactory
  ActiveMQQueue testJmsQueue

  pooledConnectionFactory.start()
  Connection conn = pooledConnectionFactory.createConnection()
  Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE)
  MessageConsumer consumer = session.createConsumer(testJmsQueue)
  consumer.setMessageListener(new MessageListenerAdapter() {
    @Override
    void onMessage(Message message) {
      println message.getContentMap()
    }
  })
  conn.start()


No comments:

Post a Comment