我有一个使用Spring连接到TIBCO(队列)的独立应用程序。有时,由于各种原因,服务器会关闭TIBCO连接。大多数事情正在从中恢复。但是,有时由于以下错误,JmsTemplate无法发送响应。我已经重试,但是相同的错误不断出现(请参见下面的跟踪)。

可能很重要的细节:

我正在使用DefaultMessageListenerContainer来获取请求并在该接收线程中发送响应。另外,我对DefaultMessageListenerContainer和JmsTemplate使用相同的连接工厂。

Caused by: org.springframework.jms.IllegalStateException: Session is closed; nested exception is javax.jms.IllegalStateException: Session is closed
        at org.springframework.jms.support.JmsUtils.convertJmsAccessException(JmsUtils.java:279)
        at org.springframework.jms.support.JmsAccessor.convertJmsAccessException(JmsAccessor.java:169)
        at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:487)
        at org.springframework.jms.core.JmsTemplate.send(JmsTemplate.java:559)
        at org.springframework.jms.core.JmsTemplate.convertAndSend(JmsTemplate.java:682)
        at org.springframework.jms.core.JmsTemplate.convertAndSend(JmsTemplate.java:670)
        at org.springframework.integration.jms.JmsSendingMessageHandler.send(JmsSendingMessageHandler.java:149)
        at org.springframework.integration.jms.JmsSendingMessageHandler.handleMessageInternal(JmsSendingMessageHandler.java:116)
        at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
        ... 83 more
Caused by: javax.jms.IllegalStateException: Session is closed
        at com.tibco.tibjms.TibjmsxSessionImp._createProducer(TibjmsxSessionImp.java:1067)
        at com.tibco.tibjms.TibjmsxSessionImp.createProducer(TibjmsxSessionImp.java:5080)
        at org.springframework.jms.core.JmsTemplate.doCreateProducer(JmsTemplate.java:1114)
        at org.springframework.jms.core.JmsTemplate.createProducer(JmsTemplate.java:1095)
        at org.springframework.jms.core.JmsTemplate.doSend(JmsTemplate.java:591)
        at org.springframework.jms.core.JmsTemplate$3.doInJms(JmsTemplate.java:562)
        at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:484)
        ... 89 more


使用Spring框架与TIBCO队列进行通信。这是配置。 DefaultMessageListenerContainer接收到一条消息,进行处理,并使用JmsTemplate发送回响应。接收方和发送方之间共享连接工厂(这可能是个问题吗?)。

    <bean id="connectionFactory"
            class="org.springframework.jms.connection.SingleConnectionFactory">
            <constructor-arg ref="tibcoJNDI" />
            <property name="targetConnectionFactory">
                    <bean class="org.springframework.jndi.JndiObjectFactoryBean">
                            <property name="jndiTemplate" ref="tibcoJNDI"/>
                            <property name="jndiName" value="${tibco.queueConnectionFactory}" />
                    </bean>
            </property>
            <property name="reconnectOnException" value="true"/>

    </bean>

<bean id="client.req.msg.lstnr" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="autoStartup" value="false"/>
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="destination" ref="ext_client.request.queue"/>
    <property name="sessionAcknowledgeMode" value="3"/>
    <property name="concurrentConsumers" value="6"/>
    <property name="receiveTimeout" value="60000"/>
</bean>

<jms:outbound-channel-adapter
    jms-template="ext.outbound.jms.template"
    channel="jms.to.ext.clnt.reply"/>

<bean id="ext.outbound.jms.template" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="defaultDestination" ref="ext_client.reply.queue"/>
    <property name="timeToLive" value="10800000" />
    <property name="explicitQosEnabled" value="true" />
</bean>


还有一个细节可能会有所帮助。我只是注意到第一个例外实际上是不同的。首先存在“连接已关闭”异常,然后是多个“会话已关闭”异常(重试)。

Caused by: javax.jms.JMSException: Connection is closed
    at com.tibco.tibjms.TibjmsxLink.sendRequest(TibjmsxLink.java:322)
    at com.tibco.tibjms.TibjmsxLink.sendRequest(TibjmsxLink.java:286)
    at com.tibco.tibjms.TibjmsxLink.sendRequestMsg(TibjmsxLink.java:261)
    at com.tibco.tibjms.TibjmsxSessionImp._createProducer(TibjmsxSessionImp.java:1075)
    at com.tibco.tibjms.TibjmsxSessionImp.createProducer(TibjmsxSessionImp.java:5080)
    at org.springframework.jms.core.JmsTemplate.doCreateProducer(JmsTemplate.java:1114)
    at org.springframework.jms.core.JmsTemplate.createProducer(JmsTemplate.java:1095)
    at org.springframework.jms.core.JmsTemplate.doSend(JmsTemplate.java:591)
    at org.springframework.jms.core.JmsTemplate$3.doInJms(JmsTemplate.java:562)
    at org.springframework.jms.core.JmsTemplate.execute(JmsTemplate.java:484)
    ... 89 more

最佳答案

看来Spring无法正确处理这种情况。我通过重写JmsTemplate(下面的代码)并自己处理异常(清理会话和连接)解决了此问题。我希望这有帮助。

public <T> T execute(SessionCallback<T> action, boolean startConnection) throws JmsException {
    try {
        return super.execute(action, startConnection);
    } catch (JmsException jmse) {
        logger.error("Exception while executing in JmsTemplate (will cleanup session & connection): ", jmse);
        Object resourceHolder =
                TransactionSynchronizationManager.getResource(getConnectionFactory());
        if (resourceHolder != null && resourceHolder instanceof JmsResourceHolder) {
            ((JmsResourceHolder)resourceHolder).closeAll();
        }

        throw jmse;
    }
}

09-28 01:25