如何从同一应用程序向多个 jms(活动 mq)代理发送消息?
How to send messages to multiple jms (active mq) brokers from the same application?
我在本地安装了 2 个具有嵌入式活动 mq 实例的应用程序(服务器)。
现在我需要为此服务器创建一个客户端。
我已阅读答案:
并尝试重复此操作:
我注册了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 启动应用程序中编写了以下主要方法:
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");
两个服务器都收到了消息。
由此可见,我的配置是错误的。请帮忙改正。
您应该指定 @Qualifier
。如果您使用 @Autowired
获取 bean,那么您可以这样做
@Autowired
@Qualifier("hitBtcJmsTemplate")
JmsTemplate hitBtcJmsTemplate;
如果你想从ApplicationContext中获取它,你必须使用BeanFactory。因为Beanfactory有一个方法来指定Qualifier。你可以这样做
BeanFactoryAnnotationUtils.qualifiedBeanOfType(applicationContext.getBeanFactory(), JmsTemplate.class, "hitBtcJmsTemplate");
你使用了错误的 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);
我在本地安装了 2 个具有嵌入式活动 mq 实例的应用程序(服务器)。
现在我需要为此服务器创建一个客户端。
我已阅读答案:
并尝试重复此操作:
我注册了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 启动应用程序中编写了以下主要方法:
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");
两个服务器都收到了消息。
由此可见,我的配置是错误的。请帮忙改正。
您应该指定 @Qualifier
。如果您使用 @Autowired
获取 bean,那么您可以这样做
@Autowired
@Qualifier("hitBtcJmsTemplate")
JmsTemplate hitBtcJmsTemplate;
如果你想从ApplicationContext中获取它,你必须使用BeanFactory。因为Beanfactory有一个方法来指定Qualifier。你可以这样做
BeanFactoryAnnotationUtils.qualifiedBeanOfType(applicationContext.getBeanFactory(), JmsTemplate.class, "hitBtcJmsTemplate");
你使用了错误的 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);