Spring Boot多个JMS连接 [英] Spring Boot multiple JMS connections
问题描述
我正在开发Spring Boot应用程序,该应用程序必须连接到具有不同端口甚至IP地址的多个WebSphere JMS连接.我需要接收消息并将消息发送到不同的队列.
I'm developing Spring Boot application which must connect to several WebSphere JMS connections with different ports or even ip addresses. I need receive and send messages to different queues.
我以来自此源的连接为例- https://github.com/lzp4ever/IBM_WebSphere_MQ_Spring_Boot_JMS
I took example of connection from this source - https://github.com/lzp4ever/IBM_WebSphere_MQ_Spring_Boot_JMS
但是当我添加第二个连接时,Factory Boot无法启动,只是不知道使用哪个.
But when i add second connectionFactory Spring Boot failes to start, it just don't know which once to use.
我的问题是我应该如何配置我的配置文件以侦听多个队列?将SpringBoot应用程序连接到几个不同的JMS服务器是个好主意吗?
My question is How should i configure my config file to listen several queues? Is it good idea connecting SpringBoot app to several different JMS servers?
推荐答案
解决方案
我只是第二次复制并粘贴相同的bean(如上面的git链接),然后添加Bean(名称)来分开它们.这是行不通的,然后我向每个配置文件添加了新的JmsListenerContainerFactory bean.
Solution
i just copy and paste same beans(like at git link above) second time and add Bean(name) to separate them. It was not work and then i added new JmsListenerContainerFactory bean to each of my config file.
我的配置文件之一是:
@Bean(name = "mqQueueConnectionFactory2")
public MQQueueConnectionFactory mqQueueConnectionFactory2() {
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
mqQueueConnectionFactory.setHostName(host);
try {
mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
mqQueueConnectionFactory.setCCSID(1208);
mqQueueConnectionFactory.setChannel(channel);
mqQueueConnectionFactory.setPort(port);
mqQueueConnectionFactory.setQueueManager(queueManager);
} catch (Exception e) {
logger.error("MQQueueConnectionFactory bean exception", e);
}
return mqQueueConnectionFactory;
}
@Bean(name = "userCredentialsConnectionFactoryAdapter2")
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter2(@Qualifier("mqQueueConnectionFactory2") MQQueueConnectionFactory mqQueueConnectionFactory) {
UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter = new UserCredentialsConnectionFactoryAdapter();
userCredentialsConnectionFactoryAdapter.setUsername(username);
userCredentialsConnectionFactoryAdapter.setPassword(password);
userCredentialsConnectionFactoryAdapter.setTargetConnectionFactory(mqQueueConnectionFactory);
return userCredentialsConnectionFactoryAdapter;
}
@Bean(name = "cachingConnectionFactory2")
//@Primary
public CachingConnectionFactory cachingConnectionFactory2(@Qualifier("userCredentialsConnectionFactoryAdapter2") UserCredentialsConnectionFactoryAdapter userCredentialsConnectionFactoryAdapter) {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setTargetConnectionFactory(userCredentialsConnectionFactoryAdapter);
cachingConnectionFactory.setSessionCacheSize(500);
cachingConnectionFactory.setReconnectOnException(true);
return cachingConnectionFactory;
}
@Bean(name = "jmsTransactionManager2")
public PlatformTransactionManager jmsTransactionManager2(@Qualifier("cachingConnectionFactory2") CachingConnectionFactory cachingConnectionFactory) {
JmsTransactionManager jmsTransactionManager = new JmsTransactionManager();
jmsTransactionManager.setConnectionFactory(cachingConnectionFactory);
return jmsTransactionManager;
}
@Bean(name = "jmsOperations2")
public JmsOperations jmsOperations2(@Qualifier("cachingConnectionFactory2") CachingConnectionFactory cachingConnectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(cachingConnectionFactory);
jmsTemplate.setReceiveTimeout(receiveTimeout);
return jmsTemplate;
}
@Bean
public JmsListenerContainerFactory<?> myFactory2(@Qualifier("cachingConnectionFactory2") CachingConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
// This provides all boot's default to this factory, including the message converter
configurer.configure(factory, connectionFactory);
// You could still override some of Boot's default if necessary.
return factory;
}
然后我从中更改发件人代码:
Then i change my sender code from this:
@Autowired
private JmsOperations jmsOperations;
对此
@Autowired
@Qualifier("jmsOperations2")
private JmsOperations jmsOperations;
我也将接收器更改为:
also i change my receiver to:
@JmsListener(destination = "${project.queues.uzb.recieve}", containerFactory = "myFactory2")
public void receiveMessage(JMSTextMessage data) {
}
在我看来,它很有用!!
it seems to me it worked!!!
但是我的CachingConnectionFactory之一必须标记为@Primary.如果我从其中一个配置文件中删除@Primaty,则表示我收到此错误:
But one of my CachingConnectionFactory must be marked as @Primary. If i delete @Primaty from one of my config files then i am gettig this error:
启动ApplicationContext时出错.要显示条件报告,请在启用调试"的情况下重新运行您的应用程序.2018-03-28 12:28:37-
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-03-28 12:28:37 -
申请无法开始
说明:
在com.config.UzbConnection中,方法myFactory的参数1需要一个类型为'org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer'的bean.
Parameter 1 of method myFactory in com.config.UzbConnection required a bean of type 'org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer' that could not be found.
操作:
考虑在您的配置中定义类型为"org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer"的bean.
Consider defining a bean of type 'org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer' in your configuration.
这篇关于Spring Boot多个JMS连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!