我在本地有2个具有嵌入式活动mq实例的应用程序(服务器)。
现在,我需要为此服务器创建一个客户端。
我已经阅读过答案:https ://stackoverflow.com/a/43401330/2674303
并尝试重复此操作:
我注册了2个连接工厂:
@Bean
@Primary
public ConnectionFactory bitFinexExchangeJmsConnectionFactory() {
return new ActiveMQConnectionFactory("tcp://localhost:61616");
}
@Bean
public ConnectionFactory hitbtcExchangeJmsConnectionFactory() {
return new ActiveMQConnectionFactory("tcp://localhost:61617");
}
注册了2个jms模板:
@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应用程序中编写了以下main方法:
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
交货。
我开始调查问题,并找出了hitBtcJmsTemplate
用途bitFinexExchangeJmsConnectionFactory
。我试图更改我的主要方法代码:
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");
并且两个服务器都收到了消息。
因此很明显我的配置是错误的。请帮助纠正它。
您使用了错误的getBean方法!
<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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句