当两个应用程序都使用嵌入式activemq时,如何将Jms消息从一个弹簧启动应用程序发送到另一个 [英] How to send Jms message from one spring-boot application to another when both apps use embedded activemq

查看:122
本文介绍了当两个应用程序都使用嵌入式activemq时,如何将Jms消息从一个弹簧启动应用程序发送到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个spring-boot应用程序.在接收方应用程序的Application.java中,我有:

I have two spring-boot applications. in receiver-application's Application.java I have:

@Bean
public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory, DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    configurer.configure(factory, connectionFactory);
    return factory;
}

以及在Receiver.java中...

and in Receiver.java ...

@JmsListener(destination = "myQueue", containerFactory = "myFactory")
public void receiveMessage(String tradeString) throws JSONException, IOException {
    tradeImpl = new ObjectMapper().readValue(tradeString, TradeImpl.class);
}

在发件人应用程序中,我只使用:

In sender-application I simply use:

public void send(trade) {
   String queueName = "myQueue";
   String tradeString = new ObjectMapper().writeValueAsString(trade);
   jmsTemplate.convertAndSend(queueName, tradeString);
}

所以我只是将消息发送到接收方应用程序中指定的队列名称. 我在日志中得到以下内容

So I'm just sending the message to the queue name specified in receiver-application. I get the following in the logs

无法启动JMX连接器无法绑定到URL [rmi://localhost:1099>/jmxrmi]:javax.naming.NameAlreadyBoundException:jmxrmi [根异常是java.rmi.AlreadyBoundException:

Failed to start JMX connector Cannot bind to URL [rmi://localhost:1099>/jmxrmi]: javax.naming.NameAlreadyBoundException: jmxrmi [Root exception is java.rmi.AlreadyBoundException:

我阅读了以下帖子,但并不觉得很令人鼓舞:

I have read the following post and didn't find it very encouraging:

Spring Boot –与单独的服务共享嵌入式JMS代理

它的结尾是:

但是正如我提到的,我之前没有做过这个,也不知道是否可行.在Spring Boot文档中没有找到明确的消息,这种消息在这种组合下不起作用.

But As I mentioned I didn't make this working before and not sure if it is possible. Didn't find in Spring Boot docs explicit message it doesn't work in this combination.

我怀疑嵌入式Spring Boot JMS代理背后的想法是仅允许本地内存集成测试,而不是将嵌入式JMS代理暴露给外界.

I suspect the idea behind embedded Spring Boot JMS broker is to allow local in memory integration testing only, not to expose embedded JMS brokers to the outside world.

有人知道我想做的事确实可行吗?如果不是,是否有关于如何使用嵌入式代理在春季启动应用程序之间实现消息传递的建议?

Does anybody know if what I want to do is indeed possible? And if not, are there any suggestions on how I can achieve messaging between to spring-boot apps using embedded brokers?

推荐答案

如果您的2个Spring Boot应用程序位于同一jvm上,则只需仅添加两个应用程序之一的application.properties即可:

If your 2 spring boot apps are on the same jvm you simply have to add in application.properties of only one of the two :

spring.activemq.broker-url=vm://localhost

Spring Boot在检测到时也可以配置ConnectionFactory ActiveMQ在类路径上可用.如果经纪人在场, 嵌入式代理会自动启动并配置(只要 没有通过配置指定代理URL.

Spring Boot can also configure a ConnectionFactory when it detects that ActiveMQ is available on the classpath. If the broker is present, an embedded broker is started and configured automatically (as long as no broker URL is specified through configuration).

如果您的2个Spring Boot应用程序位于2个不同的jvm上: 在一个spring boot应用程序中,您需要:

If your 2 spring boot apps are on 2 differents jvm: In one spring boot app you need to have :

在pom.xml中

    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-activemq -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-activemq</artifactId>
        <version>1.4.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
    </dependency>

第二个:

在application.properties中

In application.properties

spring.activemq.broker-url=tcp://localhost:61616

在pom.xml中

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jms</artifactId>
</dependency>

如果您有2个jvm,每个应用程序1个,默认情况下,spring boot将仅使用vm连接器配置AMQ,在第一个应用程序中,您需要添加tcp连接器,如下所示:

For the case that you have 2 jvm, 1 for each app, By default spring boot will configure AMQ with only vm connector, in the first app you need to add tcp connector like this :

@Bean(initMethod = "start", destroyMethod = "stop")
public BrokerService broker() throws Exception {
    final BrokerService broker = new BrokerService();
    broker.addConnector("tcp://localhost:61616");
    broker.addConnector("vm://localhost");
    broker.setPersistent(false);
    return broker;
}

这篇关于当两个应用程序都使用嵌入式activemq时,如何将Jms消息从一个弹簧启动应用程序发送到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆