gpt4 book ai didi

java - Spring-Boot:如何访问多个 JMS 代理 URL

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:37 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

为了解决这个问题,您必须创建自己的配置 Bean,如下例所示:

@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中,我只引用注入(inject)的属性(没有设置默认的spring.activemq属性):

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 中(假设我只想从两个队列中消费)

@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将消息发布到您想要的代理。

关于java - Spring-Boot:如何访问多个 JMS 代理 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42640648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com