如何处理远程jms服务器的Connection对象 [英] How to handle a Connection object to remote jms server

查看:237
本文介绍了如何处理远程jms服务器的Connection对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用的应用程序包含一个jboss @Service mbean,它封装了一个javax.jms.Connection对象。

We are using an application that contains a jboss @Service mbean which encapsulates a javax.jms.Connection object.

在启动mbean期间,初始化远程InitialContext,从该上下文中查找ConnectionFactory,并从该工厂创建连接:

During startup of the mbean the connection is created by initializing a remote InitialContext, looking up the ConnectionFactory from that context, and creating a connection from that factory:

@Service
public class JMSPublisher extends etcc.... {
   private Connection connection;
   protected void startService() {
      Context ctx = getRemoteInitialContext();
      ConnectionFactory connectionFactory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
      connection = connectionFactory.createConnection();
   }
}

我的问题是:保持这种连接?在实践中,我们看到当我们尝试在未定义的时间之后创建一个会话时,连接会抛出一个JMSException。

My question is: how long can we be supposed to maintain that connection ? In practise we see that the connection throws a JMSException when we try to create a session on it after an undefined amount of time.

Connection的文档告诉我们,表示套接字,因此由于不活动导致的超时可能是正常的。

The documentation of Connection tells us that an object represents a socket, so timeouts due to inactivity could be normal. But how can we deal with it without creating new connections for each and every message ?

推荐答案

你最好的办法是使用 JMSPublisher 实施 javax.jms.Exception 监听器。实施 connect() 方法安全地获取连接:

Your best bet is to have JMSPublisher implement javax.jms.Exception listener. Implement a connect() method which safely acquires a connection on:


  1. startService

  2. onException

以下几点:


  • 对于代码压缩,只需通过资源注入获取JMS连接工厂。连接工厂引用将在调用 startService 之前解析,并且也将作为隐式 depends ,使JMS连接工厂成为您的服务的依赖项。

  • For code compression, simply acquire the JMS connection factory via resource injection. The connection factory reference will be resolved before startService is called and will also act as an implicit depends, making the JMS connection factory a dependency for your service.

JMSPublisher 扩展 org.jboss.system.ServiceMBeanSupport ,并实现扩展 JMSPublisherMBean //docs.jboss.org/jbossas/javadoc/3.2.7/system/org/jboss/system/ServiceMBean.htmlrel =nofollow> org.jboss.system.ServiceMBean

Have JMSPublisher extend org.jboss.system.ServiceMBeanSupport and implement a token MBean interface (JMSPublisherMBean) that extends org.jboss.system.ServiceMBean. This will ensure that the dependencies are honoured on service start (and stop).

资源注入的JMS连接工厂

@Resource(mappedName="ConnectionFactory") 
private javax.jms.ConnectionFactory connectionFactory;
private volatile javax.jms.Connection connection;

修改startService()

public void startService() {
   connect();
}

连接异常处理程序

public void onException(JMSException je) {
   connect();
}

* 安全连接初始化程序(添加conn.start

*Safe Connection Initializer (adding conn.start()) *

private void synchronized connect() {
    log.info("Initializing Connection....");
    try {
        if(connection!=null) {
           try { connection.stop(); } catch (Exception e) {}
           try { connection.close(); } catch (Exception e) {}
        }
        connection = connectionFactory.createConnection();
        connection.setExceptionListener(this);
        connection.start();
    } catch (Exception e) {
        log.error("Failed to intialize JMS connection", e);
    }
}

这不会自动处理分配的其他JMS资源通过丢失的连接,但如果其他组件正在使用此组件持有的连接,您可以从 JMSPublisher 发布JMX通知,指示连接已丢失并在通知收据上清除/重新获取。

This will not automatically take care of other JMS resources allocated through the lost connection, but if other components are using the connection held by this component, you can publish JMX Notifications from JMSPublisher indicating that the connection has been lost and clean up/re-acquire on the notification receipt.

这篇关于如何处理远程jms服务器的Connection对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆