Spring Boot多个JMS连接

时间:2018-03-27 18:50:57

标签: java spring spring-boot jms spring-jms

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

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

但是当我添加第二个连接工具Spring Boot故障开始时,它只是不知道哪个曾经使用过。

我的问题是如何配置我的配置文件来监听多个队列?将SpringBoot应用程序连接到几个不同的JMS服务器是不是很好?

新的问题更新!

我只是第二次复制并粘贴相同的bean(如上面的git链接)并添加Bean(名称)以将它们分开。它没有用,然后我在每个配置文件中添加了新的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时出错。要显示条件报告,请使用&#39; debug&#39;重新运行您的应用程序。启用。   2018-03-28 12:28:37 -

           

申请失败

           

说明

     

com.config.UzbConnection中方法myFactory的参数1需要一个类型为&#39; org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer&#39;的bean。无法找到。

     

动作:

     

考虑定义类型&#39; org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer&#39;在你的配置中。

我的新问题是。我可以通过简单地使用@Primaty标记我的工厂来使用这个approch吗?或者更好地为每个连接创建两个DefaultJmsListenerContainerFactoryConfigure ???

谢谢。

2 个答案:

答案 0 :(得分:0)

解决方案

我只是第二次复制并粘贴相同的bean(如上面的git链接),然后添加Bean(名称)来分隔它们。它不起作用,然后我向每个配置文件添加了新的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时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。 2018-03-28 12:28:37-


申请无法开始


说明:

在com.config.UzbConnection中,方法myFactory的参数1需要一个类型为'org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer'的bean。

动作:

考虑在您的配置中定义类型为“ org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer”的bean。

谢谢

答案 1 :(得分:0)

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

 @SpringBootApplication(exclude = {JmsAutoConfiguration.class})

这样你就可以混合两者了。