使用Spring的CachingConnectionFactory时关闭会话 [英] Closing Session when using Spring's CachingConnectionFactory

查看:290
本文介绍了使用Spring的CachingConnectionFactory时关闭会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与Spring有关的Java文档此处 CachingConnectionFactory有评论:

The java doc here related to Spring CachingConnectionFactory has comment :

注意:此ConnectionFactory要求显式关闭从其共享Connection获得的所有Session.无论如何,这是对本机JMS访问代码的通常建议.但是,对于此ConnectionFactory,必须使用它,以便实际上允许Session重用.

NOTE: This ConnectionFactory requires explicit closing of all Sessions obtained from its shared Connection. This is the usual recommendation for native JMS access code anyway. However, with this ConnectionFactory, its use is mandatory in order to actually allow for Session reuse.

我不清楚如何在我的应用程序中使用以下给定的配置来处理此问题.

I am not clear how to handle this with the below given configuration in my application.

<bean id="springApp" class="com.codereq.springcore.jms.SpringJMSListenerApp"  />

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="jmsConnectionFactory"/>
    <property name="destination" ref="destination"/>
    <property name="messageListener" ref="messageListener"/>
    <property name="sessionTransacted" value="true"/>
    <property name="concurrentConsumers" value="5" />
    <property name="maxConcurrentConsumers" value="15" />
</bean>

<bean id="messageListener" class="com.codereq.springcore.jms.MessageListenerApp" />

<bean id="jmsConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"
        p:targetConnectionFactory-ref="emsConnectionFactory"
        p:sessionCacheSize="100" 
        p:cacheConsumers="true" />

<bean id="emsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="GenericConnectionFactory"/>
    <property name="jndiTemplate" ref="jndiTemplate"/>
</bean>


<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
    <property name="environment">
        <props>
            <prop key="java.naming.factory.initial">com.tibco.tibjms.naming.TibjmsInitialContextFactory</prop>
            <prop key="java.naming.provider.url">tibjmsnaming://localhost:7222</prop>
            <prop key="java.naming.security.principal">admin</prop>
            <prop key="java.naming.security.credentials">admin</prop>
        </props>
    </property>
</bean>

<bean id="destination" class="com.tibco.tibjms.TibjmsQueue">
    <constructor-arg value="com.sample.queue" />
</bean>

监听器类是这样的:

public class MessageListenerApp implements MessageListener {

private static int c = 0;

@Override
public void onMessage(Message arg0) {

    try {
        System.out.println("Received Message..."+arg0.getStringProperty("MessageNum")+". Waiting to finish..");
        Thread.sleep(2000);
        System.out.println("Finished processing.."+arg0.getStringProperty("MessageNum")+".."+(c++));
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

我应如何遵循建议,即应明确关闭从共享连接获得的会话?

How do I follow recommendation that Sessions obtained from shared connection should be closed explicitly ?

跨SessionAwareMessageListener接口,该接口提供onMessage方法,该方法提供Session的句柄.因此,要正确实现会话关闭,是否应该实现此接口?

Came across SessionAwareMessageListener interface which provides onMessage method which gives handle to Session. So to properly implement session closing, should this interface be implemented ?

推荐答案

对侦听器容器使用缓存连接工厂通常不是一个好主意,尤其是在使用maxConcurrentConsumers> concurrentConsumers时-最终缓存中有被缓存的使用者,它们在没有侦听器的情况下获取消息,并且这些消息可能被卡住".

It is generally not a good idea to use a caching connection factory with a listener container, especially when using maxConcurrentConsumers > concurrentConsumers - you can end up with cached consumers in the cache, which get messages where there is no listener, and such messages can get "stuck".

因此,在这种情况下不要使用CCF,它实际上是供生产者使用的.

So, don't use a CCF in this case, it's really intended for use on the producer side.

由于容器管理并发,因此会话/消费者是长期存在的,不需要缓存.

Since the container manages concurrency, the sessions/consumers are long-lived and don't need to be cached.

这篇关于使用Spring的CachingConnectionFactory时关闭会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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