Interactive with AQ Topic in Java
import static org.junit.Assert.assertNotNull;\\
import java.util.Calendar;\\
import javax.jms.Message;\\
import javax.jms.QueueConnection;\\
import javax.jms.QueueConnectionFactory;\\
import javax.jms.Topic;\\
import oracle.jms.AQjmsAgent;\\
import oracle.jms.AQjmsFactory;\\
import oracle.jms.AQjmsSession;\\
import oracle.jms.AQjmsTextMessage;\\
import oracle.jms.AQjmsTopicPublisher;\\
import oracle.jms.AQjmsTopicReceiver;\\
import org.junit.After;\\
import org.junit.Before;\\
import org.junit.Test;
public class TestOracleAQ {
private static final String AQ_TOPIC = "ESB_DEMO_QUEUE";\\
/** AQ Server Connection */\\
private static final String HOST_NAME = "VLINUX";\\
private static final String SID = "XE";\\
private static final int CONNECT_PORT = 1521;\\
private static final String CONNECT_TYPE = "thin";\\
private static final String AQ_USER = "DEMOQ";\\
private static final String AQ_PASSWD = "DEMOQ";
private QueueConnectionFactory qcf = null;\\
private AQjmsSession jmsSession = null;\\
private QueueConnection qc = null;\\
private Topic topic = null;
[cite/t:@Before]\\
public void setUp() throws Exception {\\
qcf = AQjmsFactory.getQueueConnectionFactory(HOST_NAME, SID,\\
CONNECT_PORT, CONNECT_TYPE);\\
qc = qcf.createQueueConnection(AQ_USER, AQ_PASSWD);\\
qc.start();\\
jmsSession = (AQjmsSession) qc.createQueueSession(true, 0);\\
/* for single consumer AQ */\\
// q = ((AQjmsSession) qs).getQueue("DEMOQ", "ESB_DEMO_QUEUE");\\
/* for multiple consumer AQ */\\
topic = jmsSession.getTopic(AQ_USER, AQ_TOPIC);\\
}
[cite/t:@After]\\
public void tearDown() throws Exception {\\
jmsSession.close();\\
qc.stop();\\
qcf = null;\\
}
[cite/t:@Test]\\
public void testReceive() throws Exception {\\
String subscriberId = "RECIPIENT";\\
AQjmsTopicReceiver receiver = jmsSession.createTopicReceiver(topic,\\
subscriberId, null);\\
Message msg = receiver.receive(200);\\
assertNotNull(msg);\\
/* a AQjmsTextMessage Type */\\
System.out.println(msg);\\
AQjmsTextMessage jmsMsg = (AQjmsTextMessage) msg;\\
/* User Data */\\
System.out.println(jmsMsg.getText());\\
}
[cite/t:@Test]\\
public void testProduceToSpecificSubscriber() throws Exception {\\
AQjmsTopicPublisher publisher = (AQjmsTopicPublisher) jmsSession\\
.createPublisher(topic);\\
AQjmsTextMessage jmsMsg = new AQjmsTextMessage();\\
jmsMsg.setJMSTimestamp(Calendar.getInstance().getTime().getTime());\\
jmsMsg.setText("I am a message from junit test to simon..."\\
+ Calendar.getInstance().getTime());\\
AQjmsAgent[] demoSimon = { new AQjmsAgent() };\\
String subscriberId = "DEMOAQ_SIMON";\\
demoSimon[0].setName(subscriberId);\\
publisher.publish(jmsMsg, demoSimon);\\
jmsSession.commit();\\
}
[cite/t:@Test]\\
public void testProduceToAllSubscriber() throws Exception {\\
AQjmsTopicPublisher publisher = (AQjmsTopicPublisher) jmsSession\\
.createPublisher(topic);\\
AQjmsTextMessage jmsMsg = new AQjmsTextMessage();\\
jmsMsg.setJMSTimestamp(Calendar.getInstance().getTime().getTime());\\
jmsMsg.setText("I am a message from junit test to all subscriber..."\\
+ Calendar.getInstance().getTime());\\
publisher.publish(jmsMsg);\\
jmsSession.commit();\\
}
}