【问题标题】:Is there a way to expose ActiveMQ port when it is in-memory有没有办法在内存中公开 ActiveMQ 端口
【发布时间】:2021-03-24 23:15:15
【问题描述】:

我目前正在开发一个 Spring Boot 应用程序,它使用 ActiveMQ“经典”与启用 MQTT 的设备进行通信。主要问题是我需要内存中的 ActiveMQ,因为当 Spring Boot 应用程序没有时,发送到主题的所有消息都无法读取,因为主题的 @JmsListener 仅在运行时有效(因为同样是那个时候主题的订阅者)。我可以使用docker-compose 创建一个堆栈并在 ActiveMQ 容器关闭时锁定所有内容,但我不能将它用于我实际正在做的项目中。 那么,有没有办法暴露内存中 ActiveMQ 的端口,或者在 Spring Boot 项目启动时启动 ActiveMQ 守护进程并在 Spring Boot 停止时停止它?

这是application.properties 文件

    # Embedded ActiveMQ Configuration Example
    spring.activemq.broker-url=vm://embedded?broker.persistent=false,useShutdownHook=false
    spring.activemq.close-timeout=15000
    spring.activemq.in-memory=true
    spring.activemq.non-blocking-redelivery=false
    spring.activemq.password=admin
    spring.activemq.user=admin
    spring.activemq.send-timeout=0
    spring.activemq.packages.trust-all=false
    spring.activemq.packages.trusted=com.memorynotfound
    spring.activemq.pool.block-if-full=true
    spring.activemq.pool.block-if-full-timeout=-1
    spring.activemq.pool.create-connection-on-startup=true
    spring.activemq.pool.enabled=false
    spring.activemq.pool.expiry-timeout=0
    spring.activemq.pool.idle-timeout=30000
    spring.activemq.pool.max-connections=1
    spring.activemq.pool.max-sessions-per-connection=500
    spring.activemq.pool.reconnect-on-exception=true
    spring.activemq.pool.time-between-expiration-check=-1
    spring.activemq.pool.use-anonymous-producers=true

【问题讨论】:

  • this 回答你的问题了吗?

标签: java spring-boot activemq


【解决方案1】:

是的,有几种方法可以在内存中启动代理。我发现它有助于理解 ActiveMQ 本质上只是一个库,因此您在如何引导方面有很多选择。

ActiveMQ 通常与 Spring 连接在一起,因此您通常可以将其添加到 spring beans 文件中并获取所需的所有配置(传输连接器等)

从类路径加载 activemq.xml 文件的示例(即 src/main/resources 或 src/test/reosurces,如果在单元测试中)

import org.apache.activemq.broker.BrokerFactory;
import org.apache.activemq.broker.BrokerService;
...
BrokerService broker = BrokerFactory.createBroker(new URI(xbean:activemq.xml));

Maven

<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-spring</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.xbean</groupId>
    <artifactId>xbean-spring</artifactId>
</dependency>

备用Java代码1:

EmbeddedActiveMQBroker customizedBroker = new EmbeddedActiveMQBroker("bean:customize-activemq.xml");

ActiveMQ testing

添加示例:

Embedded ActiveMQ unit test

【讨论】:

  • 认为他想知道如何通过 Spring Boot 代理此配置,因为他正在使用他们的 ActiveMQ integration。当然,我可能错了。
  • 非常感谢,我用它为 MQTT 创建了类似的配置。我通常使用配置类而不是 xml。顺便说一句,它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-25
  • 2021-11-16
  • 1970-01-01
  • 2019-03-25
相关资源
最近更新 更多