问题描述
我在本地建立了2个具有嵌入式活动mq实例的应用程序(服务器).
I've up 2 applications(servers) with embedded active mq instances locally.
现在,我需要为此服务器创建一个客户端.
Now I need to create a client for this servers.
我已经阅读了以下答案: https://stackoverflow.com/a/43401330/2674303
I've read the asnswer: https://stackoverflow.com/a/43401330/2674303
并尝试重复此操作:
我注册了2个连接工厂:
I registered 2 connection factories:
@Bean
@Primary
public ConnectionFactory bitFinexExchangeJmsConnectionFactory() {
return new ActiveMQConnectionFactory("tcp://localhost:61616");
}
@Bean
public ConnectionFactory hitbtcExchangeJmsConnectionFactory() {
return new ActiveMQConnectionFactory("tcp://localhost:61617");
}
注册了2个jms模板:
registered 2 jms templates:
@Bean
@Primary
public JmsTemplate bitfinexJmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(bitFinexExchangeJmsConnectionFactory());
jmsTemplate.setDefaultDestinationName("robotCommand_bitfinex");
return jmsTemplate;
}
@Bean
public JmsTemplate hitBtcJmsTemplate() {
JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(hitbtcExchangeJmsConnectionFactory());
jmsTemplate.setDefaultDestinationName("robotCommand_hitbtc");
return jmsTemplate;
}
并在我的spring boot应用程序中编写了以下主要方法:
and wrote following main method in my spring boot application:
ConfigurableApplicationContext context = SpringApplication.run(RobotApplication.class, args);
JmsTemplate bitfinexJmsTemplate = context.getBean(JmsTemplate.class, "bitfinexJmsTemplate");
bitfinexJmsTemplate.convertAndSend("robotCommand", "message to bitfinex");
JmsTemplate hitBtcJmsTemplate = context.getBean(JmsTemplate.class, "hitBtcJmsTemplate");
hitBtcJmsTemplate.convertAndSend("robotCommand", "message to hitbtcc");
在客户端中,我看到只有message to bitfinex
被交付.
In client I see that only message to bitfinex
was delivered.
我开始调查问题,发现hitBtcJmsTemplate
使用bitFinexExchangeJmsConnectionFactory
.我试图更改主要方法代码:
I started to investigate issue and found out that hitBtcJmsTemplate
uses bitFinexExchangeJmsConnectionFactory
. I tried to change my main method code:
ConfigurableApplicationContext context = SpringApplication.run(RobotApplication.class, args);
JmsTemplate bitfinexJmsTemplate = context.getBean(JmsTemplate.class, "bitfinexJmsTemplate");
bitfinexJmsTemplate.convertAndSend("robotCommand", "message to bitfinex");
JmsTemplate hitBtcJmsTemplate = context.getBean(JmsTemplate.class, "hitBtcJmsTemplate");
hitBtcJmsTemplate.setConnectionFactory((ConnectionFactory) context.getBean("hitbtcExchangeJmsConnectionFactory")); // <---- additional line
hitBtcJmsTemplate.convertAndSend("robotCommand", "message to hitbtcc");
两个服务器都收到了消息.
and both servers got the message.
因此,很显然我的配置错误.请帮助纠正它.
Thus it is clear that my configuration is wrong. Please help to correct it.
推荐答案
您使用了错误的getBean方法!
you use the wrong getBean method !!
<T> T getBean(java.lang.Class<T> requiredType,
java.lang.Object... args)
更改为
JmsTemplate bitfinexJmsTemplate = context.getBean("bitfinexJmsTemplate", JmsTemplate.class);
JmsTemplate hitBtcJmsTemplate = context.getBean("hitBtcJmsTemplate", JmsTemplate.class);
这篇关于如何从同一应用程序向多个JMS(活动MQ)代理发送消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!