0

I'm currently developing a Spring Boot application that uses ActiveMQ "Classic" to communicate with MQTT enabled devices. The main problem is that I need to have in-memory ActiveMQ because when the Spring Boot application doesn't, all the messages sent to the topics can't be read because the @JmsListener for the topics only work in runtime(because the same is subscriber of the topic in that moment). I could use a docker-compose to create a stack and lock everything when ActiveMQ container is down but I can't use it into the project that I'm actually doing. So, is there a way to expose the ports of in-memory ActiveMQ or a way to start an ActiveMQ deamon when the Spring Boot project start and stop it when Spring Boot stops?

Here is the application.properties file

    # 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
Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
pingmyheart
  • 27
  • 1
  • 7
  • Does [this](https://stackoverflow.com/questions/48504265/is-it-possible-to-connect-to-spring-boot-embedded-activemq-instance-from-another) answer your question? – Hopey One Mar 24 '21 at 03:30

1 Answers1

0

Yes, there are a couple ways to fire up the broker in memory. I've found it helps to understand that ActiveMQ is essentially just a library, so you've got lots of options in how to bootstrap.

ActiveMQ is generally wired together with Spring, so you can generally add it to a spring beans file and get all the config you want (transport connectors, etc)

Sample loading activemq.xml file from classpath (ie. src/main/resources or src/test/reosurces if in a unit test)

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>

Alternate Java code1:

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

ActiveMQ testing

Add'l example:

Embedded ActiveMQ unit test

Matt Pavlovich
  • 4,087
  • 1
  • 9
  • 17
  • I *think* he wants to know how to proxy this configuration through Spring Boot since he's using their [ActiveMQ integration](https://docs.spring.io/spring-boot/docs/2.0.x/reference/html/boot-features-messaging.html#boot-features-activemq). I could be wrong about that, of course. – Justin Bertram Mar 24 '21 at 16:49
  • Thanks a lot, I used it to create a similar configuration for MQTT. Instead of xml I usually use a configuration class. BTW it now works. – pingmyheart Apr 04 '21 at 17:19