深入理解CachingConnectionFactory

http://windows9834.blog.163.com/blog/static/27345004201311834632915/

深入理解CachingConnectionFactory  

CachingConnectionFactory类扩展自SingleConnectionFactory,主要用于提供缓存JMS资源功能。具体包括MessageProducer、MessageConsumer和Session的缓存功能。

public class CachingConnectionFactory extends SingleConnectionFactory { private int sessionCacheSize = 1; private boolean cacheProducers = true; private boolean cacheConsumers = true; private volatile boolean active = true; private final Map cachedSessions = new HashMap();



Spring中发送消息的核心是JmsTemplate,然而Jmstemplate的问题是在每次调用时都要打开/关闭session和producter,效率很低,所以引申出了PooledConnectionFactory连接池,用于缓存session和producter。然而这还不是最好的。从spring2.5.3版本后,Spring又提供了CachingConnectionFactory,这才是首选的方案。然而CachingConnectionFactory有一个问题必须指出,默认情况下, CachingConnectionFactory只缓存一个session,在它的JavaDoc中,它 声明对于低并发情况下这是足够的。与之相反,PooledConnectionFactory的 默认值是500。这些设置,在很多情况下,需要亲自去测试并验证。我将其设置为100,对我来说还是很不错

 

 

==============

http://singztechmusings.wordpress.com/2011/06/21/pooledconnectionfactory-vs-cachingconnectionfactory-which-one-is-a-perfect-match-for-spring-jmstemplate/

PooledConnectionFactory vs CachingConnectionFactory: Which one is a perfect match for Spring JmsTemplate?


 

JmsTemplate, part of Core Spring JMS framework, simplifies the use of JMS since it handles the creation and release of resources when sending or synchronously receiving messages. As discussed in this post – http://singztechmusings.wordpress.com/2011/04/24/problem-with-creating-jms-messageproducer-using-spring-jmstemplate-how-to-solve/ – we need to have pooling in place to make it efficient.

We’ve got two JMS provider choices: ActiveMQ‘s PooledConnectionFactory or Spring’s own CachingConnectionFactory

They’re meant for the same purpose – to pool Connection, Session and MessageProducer instances. But we need to consider a thing or two before deciding to use one of these:

1. If you have clustered ActiveMQs, and use failover transport (Eg. failover:(tcp://firstbox:61616,tcp://secondbox:61616)?timeout=30000), it has been reported that CachingConnectionFactory is not a right choice.

The problem I’m having is that if one box goes down, we should start sending messages on the other, but it seems to still be using the old connection (every send times out). If I restart the program, it’ll connect again and everything works.
Source: http://stackoverflow.com/questions/5916638/autoreconnect-problem-with-activemq-and-cachingconnectionfactory

The problem is that cached connections to the failed ActiveMQ was still in use and that created the problem for the user. Now, the choice for this scenario is PooledConnectionFactory.

2. If you’re using ActiveMQ today, and chances are that you may switch to some other broker (JBoss MQ, WebSphere MQ) in future, do not use PooledConnectionFactory, as it tightly couples your code to ActiveMQ.

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AMQP 是基于 Spring 框架的 AMQP 消息解决方案,提供模板化的发送和接收消息的抽象层,提供基于消息驱动的 POJO。同时有 Java 和 .NET 的版本。示例代码:public static void main(final String... args) throws Exception {     ConnectionFactory cf = new CachingConnectionFactory();     // set up the queue, exchange, binding on the broker     RabbitAdmin admin = new RabbitAdmin(cf);     Queue queue = new Queue("myQueue");     admin.declareQueue(queue);     TopicExchange exchange = new TopicExchange("myExchange");     admin.declareExchange(exchange);     admin.declareBinding(         BindingBuilder.bind(queue).to(exchange).with("foo.*"));     // set up the listener and container     SimpleMessageListenerContainer container =             new SimpleMessageListenerContainer(cf);     Object listener = new Object() {         public void handleMessage(String foo) {             System.out.println(foo);         }     };     MessageListenerAdapter adapter = new MessageListenerAdapter(listener);     container.setMessageListener(adapter);     container.setQueueNames("myQueue");     container.start();     // send something     RabbitTemplate template = new RabbitTemplate(cf);     template.convertAndSend("myExchange", "foo.bar", "Hello, world!");     Thread.sleep(1000);     container.stop(); } 标签:Spring

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值