Spring-Boot:如何访问多个JMS broker-url [英] Spring-Boot: how to access multiple JMS broker-url's

查看:351
本文介绍了Spring-Boot:如何访问多个JMS broker-url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring-Boot中,默认的ActiveMQ(JMS)属性是:

In Spring-Boot the default ActiveMQ (JMS) properties are:

spring.activemq.broker-url=tcp://192.168.1.210:9876
spring.activemq.user=admin
spring.activemq.password=secret

如果我想发送到多个broker-url或收听不同的broker-url,该怎么做?

if I want to send to more than one broker-url or listen different broker-urls how to do it?

推荐答案

您不能使用默认的Spring-Boot自动配置访问两个不同的代理.

You cannot access two different brokers with the default Spring-Boot auto configuration.

要解决此问题,您必须创建自己的配置Bean,如以下示例所示:

For resolving this you have to create your own configuration Bean like in the following example:

@Configuration
class JmsUtilsConfiguration {

    @Value("${activemq.broker-one.url}")
    private String brokerOneUrl;

    // Im my case, broker-two is secured -> hence username and password need to be configured
    @Value("${activemq.broker-two.url}")
    private String brokerTwoUrl;

    @Value("${activemq.broker-two.username}")
    private String brokerTwoUser;

    @Value("${activemq.broker-two.password}")
    private String brokerTwoPwd;

    @Bean
    @Primary
    public ConnectionFactory jmsConnectionFactoryOne() {
        return new ActiveMQConnectionFactory(brokerOneUrl);
    }

    @Bean
    public QueueConnectionFactory jmsConnectionFactoryTwo() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(brokerTwoUrl);
        activeMQConnectionFactory.setUserName(brokerTwoUser);
        activeMQConnectionFactory.setPassword(brokerTwoPwd);
        return activeMQConnectionFactory;
    }

    // JmsListenerContainerFactory declarations
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerFactoryOne(
            ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerFactoryTwo(
            @Qualifier("jmsConnectionFactoryTwo") ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }
    
    // JMS Template Declaration
    
    @Bean
    @Primary
    public JmsTemplate jmsTemplateOne() {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(jmsConnectionFactoryOne());
        return jmsTemplate;
    }

    @Bean
    public JmsTemplate jmsTemplateTwo() {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(jmsConnectionFactoryTwo());
        return jmsTemplate;
    }

}

在我的application.yml中,我只是参考注入的属性(,没有设置默认的spring.activemq):

In my application.yml I just refer to the injected propertie (without setting the default spring.activemq ones):

activemq:
  broker-one:
    url: tcp://localhost:61616
    local-queue: TEST.LOCAL.INBOUND
  broker-two:
    url: failover:(ssl://myremote-amq-1:61617,ssl://myremote-amq-2:61617)?jms.watchTopicAdvisories=false&timeout=5000&maxReconnectDelay=10000
    username: myuser
    password: mypass
    remote-queue: TEST.REMOTE.QUEUE

和我的侦听器Bean中(假设我只想从两个队列中使用)

and in my listener Bean (assuming I just want to consume from both queues)

@Component
public class ConsumeQueuesBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(ConsumeQueuesBean.class);

    @JmsListener(destination = "${activemq.broker-one.local-queue}", containerFactory = "jmsListenerContainerFactoryOne")
    public void onMessageReceiveB1(final Message message) throws JMSException {
        if (message instanceof TextMessage) {
            String text = ((TextMessage) message).getText();
            LOGGER.info(text);
        }
    }


    @JmsListener(destination = "${activemq.broker-two.remote-queue}", containerFactory = "jmsListenerContainerFactoryTwo")
    public void onMessageReceivedB2(final Message message) throws JMSException {
        if (message instanceof TextMessage) {
            String text = ((TextMessage) message).getText();
            LOGGER.info(text);
        }
    }
}

您还可以使用配置中定义的jmsTemplates将消息发布到所需的代理.

You can also use the jmsTemplates defined in the config for posting message to the broker you want.

这篇关于Spring-Boot:如何访问多个JMS broker-url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆