[英]Spring Boot and ActiveMQ: Ignores broker-url and uses default localhost:61616
[英]Spring-Boot: how to access multiple JMS broker-url's
在 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.activemq.broker-url= # URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`
spring.activemq.in-memory=true # Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified.
spring.activemq.password= # Login password of the broker.
spring.activemq.user= # Login user of the broker.
使spring.activemq.in-memory=false
。 如果它是真的,那么它會從內存中讀取,你會得到另一個。 希望它會有所幫助。
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
您無法使用默認的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
我只是引用了注入的屬性(沒有設置默認的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
將消息發布到您想要的代理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.