MessageListener不在Oracle Queue中侦听消息

时间:2012-01-08 03:09:30

标签: java java-ee jms advanced-queuing

我实施了Oracle Advanced Queue&我正在写一个听众程序。以下是我的样本:

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条消息。为什么听众不能继续跑步&听队列?

我认为它会继续倾听&检索队列中的所有消息&然后将永远保持在聆听模式,但它不那么表现。

感谢有人可以告诉我出了什么问题。

由于

2 个答案:

答案 0 :(得分:2)

这都是因为你在启动后立即关闭了你的连接/会话。你需要这个过程来启动一个在JVM中永远运行的守护程序线程。 JMS不负责保持JVM运行。你需要创建一个只是睡觉的线程来完成它。

答案 1 :(得分:0)

以下是另一个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();
}