我正在开发Spring Boot应用程序,它必须使用不同的端口甚至ip地址连接到多个WebSphere JMS连接。我需要接收和发送消息到不同的队列。
我从这个来源举了一个连接的例子-
https://github.com/lzp4ever/IBM_WebSphere_MQ_弹簧_启动_JMS
但是当我添加第二次connectionFactory Spring Boot失败时,它就是不知道该使用哪一次。
我的问题是,我应该如何配置我的配置文件来侦听多个队列?将SpringBoot应用程序连接到几个不同的JMS服务器上是个好主意吗?
发布于 2020-09-23 23:55:57
我只是复制并粘贴相同的Bean(就像上面的git链接),然后添加Bean(name)来分隔它们。它不工作,然后我将新JmsListenerContainerFactory bean添加到我的每个配置文件中。
我的一个配置文件是:
@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;
}
然后,我将我的发送者代码从以下代码更改为:
@Autowired
private JmsOperations jmsOperations;
到这个
@Autowired
@Qualifier("jmsOperations2")
private JmsOperations jmsOperations;
另外,我将我的接收器更改为:
@JmsListener(destination = "${project.queues.uzb.recieve}", containerFactory = "myFactory2")
public void receiveMessage(JMSTextMessage data) {
}
在我看来它起作用了!
但我的其中一个CachingConnectionFactory必须标记为@Primary。如果我从我的配置文件中删除了@Primaty,那么我会得到这个错误:
启动ApplicationContext时出错。要显示条件报告,请在启用“debug”的情况下重新运行应用程序。2018-03-28 12:28:37 -
应用程序启动失败
描述:
找不到'org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer‘类型的bean,com.config.UzbConnection中方法myFactory的参数%1。
操作:
考虑在您的配置中定义一个'org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer‘类型的bean。
发布于 2021-02-25 04:45:29
就我的两分钱。如果您在多个JMS连接方面遇到问题,因为您的项目混合使用了Spring-boot和JMS以及Spring xml配置来建立连接工厂,那么您可以在应用程序中禁用spring-boot-jms的自动启动,如下所示:
@SpringBootApplication(exclude = {JmsAutoConfiguration.class})
通过这种方式,您可以将两者混合在一起。
https://stackoverflow.com/questions/49520412
复制相似问题