I've Oracle Advanced Queue implemented & I'm writing a listener program. Below is my sample:
package com.myprog;
import java.io.File;
import java.io.FileInputStream;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.Session;
import javax.jms.TextMessage;
import oracle.jms.AQjmsFactory;
import oracle.jms.AQjmsSession;
import org.apache.log4j.Logger;
public class abc implements MessageListener, ExceptionListener {
private static String queueUserName = "admin";
private static String queueName = "my_queue";
// Initialize the logger
private static Logger log = Logger.getLogger(abc.class);
public static void main(String[] args) {
final String METHOD_NAME = "main()";
abc a = new abc();
Queue queue;
try {
QueueConnection QCon = getConnection();
Session session = QCon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QCon.start();
queue = ((AQjmsSession) session).getQueue(queueUserName, queueName);
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(a);
QCon.setExceptionListener(a);
consumer.close();
session.close();
QCon.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
public static QueueConnection getConnection() {
String hostname = "myhost";
String oracle_sid = "mysid";
int portno = 1521;
String userName = "myapp";
String password = "pwd";
String driver = "thin";
QueueConnectionFactory QFac = null;
QueueConnection QCon = null;
try {
// get connection factory , not going through JNDI here
QFac = AQjmsFactory.getQueueConnectionFactory(hostname, oracle_sid, portno,driver);
// create connection
QCon = QFac.createQueueConnection(userName, password);
} catch (Exception e) {
e.printStackTrace();
}
return QCon;
}
@Override
public void onException(JMSException e) {
log.error(e);
}
@Override
public void onMessage(Message message) {
TextMessage msg = (TextMessage) message;
try {
String m = msg.getText();
System.out.println("m="+m);
log.info("MESSAGE RECEIVED " + m);
} catch (JMSException e) {
log.error(e);
}
}
}
Please note that this program is a standalone program which will keep running & listening to messages in oracle queue.
Unfortunately, when I create a jar of this class file & run it, it just runs & then exits & consumes only 1 message in the queue. Why the listener not keep on running & listening to queue?
I thought it'll keep listening & retrieve all the messages in the queue & then will remain in listen mode forever, but its not behaving that way.
Appreciate if some one can tell me what's going wrong.
Thanks
Here's an example of how another JMS example loops to process multiple messages.
You may be able to get away with just wrapping this code in the while loop. It depends on how JMS makes you handle the connection and session objects and if they automatically close, but you can try to wrap just this.