我正在创建一个在JBOSS 5.1.0ga AS / Sun OS上运行的RichFaces Web应用程序。该应用程序还处理JMS请求(从在不同服务器中运行的Weblogic队列读取请求并将响应写回到在该服务器中运行的另一个Weblogic队列)。我通过扩展MessageListener类并在ServletContextListener中启动监听来使用异步消息处理。一切正常但在一段时间后(至少在5到6小时后),监听器停止监听队列,如果我重新启动服务器,则监听器也会读取旧消息。 我无法理解为什么它会在中间停下来。有没有其他有效的方法在Web应用程序中启动监听器?
的ServletContextListener
public class WebApplicationListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent contextEvent) {
System.out.println("Starting the JMS Listener");
new JMSConnector().startListening();
}
public void contextDestroyed(ServletContextEvent contextEvent) {
}
}
JMS连接器
public class JMSConnector{
static Properties properties = new Properties();
static Context context;
static QueueConnectionFactory connFactory;
static {
Properties props = new Properties();
InputStream inputStream = new JMSConnector().getClass()
.getClassLoader()
.getResourceAsStream("jmsconnection.properties");
// ClassLoader.getSystemResource("/HibernateConfigFile.properties").openStream()
try {
properties.load(inputStream);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
properties.getProperty("context_factory_class"));
// props.setProperty("java.naming.factory.url.pkgs",
// properties.getProperty("naming_url_pkgs"));
props.setProperty(Context.PROVIDER_URL,
properties.getProperty("provider_url"));
try {
context = new InitialContext(props);
connFactory = (QueueConnectionFactory) context.lookup(properties
.getProperty("connection_factory_name"));
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sendMessage(String responseMessage) throws Exception {
QueueConnection conn = connFactory.createQueueConnection();
// This session is not transacted, and it uses automatic message
// acknowledgement
QueueSession session = conn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue q = (Queue) context.lookup(properties
.getProperty("responsequeue"));
// Sender
QueueSender sender = session.createSender(q);
// Text message
TextMessage msg = session.createTextMessage();
msg.setText(responseMessage);
System.out.println("Sending the message: " + msg.getText());
sender.send(msg);
session.close();
conn.close();
}
public void startListening() {
try {
System.out.println("In Start Listening");
QueueConnection conn = connFactory.createQueueConnection();
// This session is not transacted, and it uses automatic message
// acknowledgement
QueueSession session = conn.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
Queue q = (Queue) context.lookup(properties
.getProperty("requestqueue"));
QueueReceiver receiver = session.createReceiver(q);
RequestListener requestListener = new RequestListener();
receiver.setMessageListener(requestListener);
conn.start();
} catch (Exception e) {
e.printStackTrace();
}
}
消息监听
public class RequestListener implements MessageListener {
/**
* Casts the message to a TextMessage and displays its text.
*
* @param message
* the incoming message
*/
public void onMessage(Message message) {
System.out.println("Message Received");
TextMessage msg = null;
try {
if (message instanceof TextMessage) {
msg = (TextMessage) message;
String requestXml = msg.getText();
//Calling Processing Methods
new JMSConnector().sendMessage(responseXml);
} else {
System.out.println("Message of wrong type: "
+ message.getClass().getName());
}
} catch (JMSException e) {
System.out.println("JMSException in onMessage(): " + e.toString());
} catch (Throwable t) {
System.out.println("Exception in onMessage():" + t.getMessage());
}
}
}