Java 如何从同一应用程序向多个jms(活动mq)代理发送消息?
我有两个应用程序(服务器)在本地嵌入了活动mq实例 现在我需要为这个服务器创建一个客户端 我读过asnswer: 再重复一遍: 我注册了两个连接工厂:Java 如何从同一应用程序向多个jms(活动mq)代理发送消息?,java,spring-boot,jms,activemq,spring-jms,Java,Spring Boot,Jms,Activemq,Spring Jms,我有两个应用程序(服务器)在本地嵌入了活动mq实例 现在我需要为这个服务器创建一个客户端 我读过asnswer: 再重复一遍: 我注册了两个连接工厂: @Bean @Primary public ConnectionFactory bitFinexExchangeJmsConnectionFactory() { return new ActiveMQConnectionFactory("tcp://localhost:61616"); } @Bean public Connection
@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应用程序中编写了以下主要方法:
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");
在客户端中,我看到只有发送给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");
ConfigurableApplicationContext上下文=SpringApplication.run(RobotApplication.class,args);
JmsTemplate bitfinexJmsTemplate=context.getBean(JmsTemplate.class,“bitfinexJmsTemplate”);
convertAndSend(“机器人命令”,“发送给bitfinex的消息”);
JmsTemplate hitBtcJmsTemplate=context.getBean(JmsTemplate.class,“hitBtcJmsTemplate”);
hitBtcJmsTemplate.setConnectionFactory((ConnectionFactory)context.getBean(“hitbtcExchangeJmsConnectionFactory”);// 您应该指定@限定符
。如果您使用@Autowired
获得一个bean,那么您可以这样做
@Autowired
@Qualifier("hitBtcJmsTemplate")
JmsTemplate hitBtcJmsTemplate;
如果要从ApplicationContext获取它,必须使用BeanFactory。因为Beanfactory有一个指定限定符的方法。你可以这样做
BeanFactoryAnnotationUtils.qualifiedBeanOfType(applicationContext.getBeanFactory(), JmsTemplate.class, "hitBtcJmsTemplate");
您使用了错误的getBean方法
<T> T getBean(java.lang.Class<T> requiredType,
java.lang.Object... args)
定义bitfinexJmsTemplate的Bean注释后面的括号()是故意的吗?@DaveH实际上我可以删除它。我只是尝试使用注释设置名称,但决定删除it@DaveH事实上,我试着删除它并测试了一次-没有效果
JmsTemplate bitfinexJmsTemplate = context.getBean("bitfinexJmsTemplate", JmsTemplate.class);
JmsTemplate hitBtcJmsTemplate = context.getBean("hitBtcJmsTemplate", JmsTemplate.class);