When is it appropriate to cacheConsumers when usin

2019-08-05 16:42发布

问题:

Spring includes a class called CachingConnectionFactory. One variable in this class is a boolean named cacheProducers. By default, this value is true. This variable can be set to false using setCacheProducers(false).

When would it be appropriate to set this to false? What are the benefits and drawbacks of setting to false?

Background: We are conducting a performance test of our application and use Spring JMS to post thousands of messages a second to JMS. Profiling our code, we see that the session.createProducer call is consuming over 60% of our total processing time and the factory is currently set to setCacheProducers(false). What are the consequences to setting this value to true?

回答1:

Setting it to false pretty much defeats the whole purpose. You are left with simply caching the connection.

You might want to set it to false if you want to manage the producer lifecycle yourself (e.e. when using execute() with a SessionCallback). But when you are using the higher level abstractions (send(), convertAndSend() etc), you would generally want it to be true.

This is for the Jmstemplate. It is generally not recommended to use a CachingConnectionFactory with the DefaultMessageListenerContainer.