[英]Spring Integration: How to use SingleConnectionFactory with ActiveMQ?
Spring 啟動 2.3.1.RELEASE。
使用spring.jms.cache.enabled=true
(默認), Spring 創建一個CachingConnectionFactory
:
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "true",
matchIfMissing = true)
static class CachingConnectionFactoryConfiguration {
這很糟糕,因為它不應該與DefaultMessageListenerContainer
一起使用。 我認為這就是為什么我的一些信息會“丟失”直到它們突然重新出現的原因。
使用spring.jms.cache.enabled=false
, Spring 創建一個ActiveMQConnectionFactory
:
@ConditionalOnProperty(prefix = "spring.jms.cache", name = "enabled", havingValue = "false")
ActiveMQConnectionFactory jmsConnectionFactory(ActiveMQProperties properties,
ObjectProvider<ActiveMQConnectionFactoryCustomizer> factoryCustomizers) {
return createJmsConnectionFactory(properties, factoryCustomizers);
}
private static ActiveMQConnectionFactory createJmsConnectionFactory(ActiveMQProperties properties,
這很糟糕,因為每次輪詢時,它都會創建一個到代理的新連接——用數百個連接淹沒我的代理。
所以我雖然我的問題的解決方案是使用SingleConnectionFactory
。 在AbstractPollingMessageListenerContainer.MessageListenerContainerResourceFactory
我看到:
public Connection createConnection() throws JMSException {
if (AbstractPollingMessageListenerContainer.this.sharedConnectionEnabled()) {
Connection sharedCon = AbstractPollingMessageListenerContainer.this.getSharedConnection();
return new SingleConnectionFactory(sharedCon).createConnection();
}
所以我想我會:
Jms.channel(connectionFactory)
.cacheLevel(DefaultMessageListenerContainer.CACHE_CONNECTION)
但事實證明,這個方法永遠不會被調用,只有JmsAccessor.createConnection()
會創建一個ActiveMQConnectionFactory
。 我的緩存級別沒有影響。
那么如何正確使用SingleConnectionFactory
呢?
如果您有可變並發,緩存工廠只是 DMLC 的問題。
只需將SingleConnctionFactory
定義為@Bean
並使用Jms.channel(mySingleCF())...
。
編輯
@SpringBootApplication
public class So63120705Application {
public static void main(String[] args) {
SpringApplication.run(So63120705Application.class, args).close(); // JVM should exit
}
@Bean
public ApplicationRunner runner(ConnectionFactory jmsConnectionFactory, IntegrationFlowContext context,
JmsTemplate template) {
return args -> {
SingleConnectionFactory connectionFactory = new SingleConnectionFactory(jmsConnectionFactory);
IntegrationFlow flow = f -> f.channel(Jms.channel(connectionFactory)
.destination("foo"))
.handle(System.out::println);
context.registration(flow)
.id("jms")
.addBean(connectionFactory)
.register();
template.convertAndSend("foo", "test");
Thread.sleep(5_000);
};
}
}
spring.jms.cache.enabled=false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.