Web applications JMS MessageListener从ServletContextListener启动,一段时间后停止
我正在创建一个在JBOSS 5.1.0ga AS/Sun OS上运行的RichFaces Web应用程序。该应用程序还处理JMS请求(从运行在不同服务器中的Weblogic队列读取请求,并将响应写回运行在该服务器中的另一个Weblogic队列)。我通过扩展MessageListener类使用异步消息处理,并在ServletContextListener中启动了侦听。一切正常,但一段时间后(至少5到6小时后),侦听器停止侦听队列,如果我重新启动服务器,侦听器也会读取旧消息。 我不明白为什么中间会停下来。在Web应用程序中启动侦听器还有其他有效的方法吗 ServletContextListenerWeb applications JMS MessageListener从ServletContextListener启动,一段时间后停止,web-applications,jboss,richfaces,jms,Web Applications,Jboss,Richfaces,Jms,我正在创建一个在JBOSS 5.1.0ga AS/Sun OS上运行的RichFaces Web应用程序。该应用程序还处理JMS请求(从运行在不同服务器中的Weblogic队列读取请求,并将响应写回运行在该服务器中的另一个Weblogic队列)。我通过扩展MessageListener类使用异步消息处理,并在ServletContextListener中启动了侦听。一切正常,但一段时间后(至少5到6小时后),侦听器停止侦听队列,如果我重新启动服务器,侦听器也会读取旧消息。 我不明白为什么中间会停
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) {
}
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());
}
}
}
JMS连接器
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) {
}
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());
}
}
MessageListener
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) {
}
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());
}
}
}我对您的问题没有答案,但对代码有一点意见。为了理智起见,请在捕获JMS异常时打印链接的异常!JMS异常是相当通用的,并且存在链接异常,以便传输供应商提供传输问题视图的低级细节。如果存在链接异常,则它将是诊断问题的关键。我通常告诉我的客户,打印链接异常的失败应该是阻止部署到生产的原因。好的,完成了。希望现在有人能给出一个真实的答案。谢谢Rob的评论,我会在这里打印链接的异常。为什么你不使用MDB?对我来说,你的解决方案听起来像是一个不必要的黑客……在
startListening
方法中没有引用任何项,因此它们可能是垃圾收集的候选项connFactory
和context
是唯一的成员变量,也许这就是原因?你曾经解决过这个问题吗?你可以试着把JMSConnector
做成一个单例,看看效果是否更好。