All Articles

Spring : Browsing Oracle AQ (XMLType) using Spring JMS

A tutorial showing how to browse Oracle AQ (XMLType) using Spring JMS -

<beans …> view rawbeans.xml hosted with ❤ by GitHub package com.sachinhandiekar.oracle.aq;

import java.util.Enumeration;

import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Queue; import javax.jms.QueueBrowser; import javax.jms.Session;

import oracle.jms.AQjmsAdtMessage; import oracle.jms.AQjmsQueueBrowser; import oracle.jms.AQjmsSession; import oracle.sql.ORADataFactory; import oracle.xdb.XMLType;

import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.SessionCallback;

public class OracleAQQueueBrowser {

private JmsTemplate jmsTemplate;

public void browseMessages() {

Integer count = (Integer) jmsTemplate.execute(new SessionCallback() { public Object doInJms(Session session) throws JMSException { int count = 0; Queue queue = (Queue) jmsTemplate.getDefaultDestination();

QueueBrowser browser = ((AQjmsSession) session).createBrowser(queue, null, XMLType.getORADataFactory(),
  true);

Enumeration messages = browser.getEnumeration();

while (messages.hasMoreElements()) {
 count++;
 Message message = (Message) messages.nextElement();
 displayMessage(message);
}

 return new Integer(count);

}

private void displayMessage(Message message) { try { XMLType xmlMsg = (XMLType) ((AQjmsAdtMessage) message).getAdtPayload(); System.out.println(“Message ==> ” + xmlMsg.getStringVal()); System.out.println(); } catch (Exception e) { e.printStackTrace(); } } }, true);

System.out.println(“Number of Messages : ” + count); }

public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } }