有没有办法在内存中公开ActiveMQ端口

Is there a way to expose ActiveMQ port when it is in-memory

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

这是 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

是的,有几种方法可以启动内存中的代理。我发现了解 ActiveMQ 本质上只是一个库会有所帮助,因此您在如何 bootstrap.

方面有很多选择

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));

行家

<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