MessageListener不监听Oracle队列中的消息
我已经实现了Oracle高级队列和我正在编写一个监听器程序。 以下是我的示例:
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);
}
}
}
请注意,这个程序是一个独立的程序,它将继续在oracle队列中运行和收听消息。
不幸的是,当我创建一个这个类文件的jar并运行它时,它只运行&然后退出并仅消耗队列中的1条消息。 为什么听众不能继续跑步和倾听队列?
我认为它会继续收听并检索队列中的所有消息,然后将永远保持听音模式,但它不会以这种方式表现。
欣赏是否有人能告诉我发生了什么问题。
谢谢
以下是另一个JMS示例循环处理多条消息的示例。
Performs a JNDI lookup of the ConnectionFactory and Destination.
Creates a Connection and a Session.
Creates a MessageConsumer:
consumer = session.createConsumer(dest);
Starts the connection, causing message delivery to begin:
connection.start();
Receives the messages sent to the destination until the end-of-message-stream control message is received:
while (true) {
Message m = consumer.receive(1);
if (m != null) {
if (m instanceof TextMessage) {
message = (TextMessage) m;
System.out.println("Reading message: " +
message.getText());
} else {
break;
}
}
}
Because the control message is not a TextMessage, the receiving program terminates the while loop and stops receiving messages after the control message arrives.
Closes the connection in a finally block, automatically closing the session and MessageConsumer.
你可能只能在while循环中包装这段代码。 这取决于JMS如何让您处理连接和会话对象,以及它们是否自动关闭,但您可以尝试包装这一点。
while(true) {
QCon.start();
queue = ((AQjmsSession) session).getQueue(queueUserName, queueName);
MessageConsumer consumer = session.createConsumer(queue);
consumer.setMessageListener(a);
QCon.setExceptionListener(a);
consumer.close();
}
这都是因为你在启动后立即关闭连接/会话。 YOu需要进程启动一个在JVM中永远运行的守护进程线程。 JMS不负责保持JVM运行。 你需要创建一个刚刚睡过的线程来完成这个任务。
链接地址: http://www.djcxy.com/p/76419.html上一篇: MessageListener not listening to messages in Oracle Queue
下一篇: Xcode 9: Linking a static framework against another static framework