Spring 引导多个 JMS 连接

Spring Boot multiple JMS connections

我正在开发 Spring 启动应用程序,它必须连接到多个具有不同端口甚至 IP 地址的 WebSphere JMS 连接。我需要接收和发送消息到不同的队列。

我从这个来源获取了连接示例 - https://github.com/lzp4ever/IBM_WebSphere_MQ_Spring_Boot_JMS

但是当我添加第二个 connectionFactory 时 Spring 引导无法启动,它只是不知道该使用哪个。

我的问题是我应该如何配置我的配置文件来监听多个队列?将 Spring 启动应用程序连接到多个不同的 JMS 服务器是个好主意吗?

解决方案

我只是第二次复制并粘贴相同的 beans(如上面的 git link)并添加 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,那么我会得到这个错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2018-03-28 12:28:37 -


应用程序启动失败


Description:

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.

Action:

Consider defining a bean of type 'org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer' in your configuration.

谢谢

只是我的 2 美分。如果您的多个 JMS 连接有问题,因为您有一个项目混合使用 Spring-boot 和 JMS 以及 Spring xml 配置来创建您的连接工厂,您可以禁用 spring-boot-jms 在你的应用程序中使用这个:

 @SpringBootApplication(exclude = {JmsAutoConfiguration.class})

这样你就可以混合使用。