gpt4 book ai didi

java - 当两个应用程序都使用嵌入式 activemq 时如何将 Jms 消息从一个 spring-boot 应用程序发送到另一个

转载 作者:搜寻专家 更新时间:2023-11-01 02:21:19 25 4
gpt4 key购买 nike

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

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

并在 Receiver.java ...

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

在发件人应用程序中我简单地使用:

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

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

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

我已经阅读了以下帖子,并没有发现它非常鼓舞人心:

Spring boot - sharing embedded JMS broker with separate service

结论是:

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.

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.

有人知道我想做的事情是否真的可行吗?如果没有,是否有关于如何使用嵌入式代理在 spring-boot 应用程序之间实现消息传递的任何建议?

最佳答案

如果您的 2 个 spring boot 应用程序在同一个 jvm 上,您只需添加两者之一的 application.properties :

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

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 应用程序中,您需要:

在 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 中

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 连接器:

@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;
}

关于java - 当两个应用程序都使用嵌入式 activemq 时如何将 Jms 消息从一个 spring-boot 应用程序发送到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41369023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com