Create a custom JMS ConnectionFactory with ActiveMQ Artemis in Spring Boot 3 – Spring-boot

by
Alexei Petrov
activemq-artemis spring-boot spring-jms

Quick Fix: To create a custom JMS ConnectionFactory with ActiveMQ Artemis in Spring Boot 3, set ‘spring.artemis.mode=embedded’ and ‘spring.artemis.embedded.enabled=true’. Then, create a ConnectionFactory bean with the ‘vm://0’ URL and return it.

The Problem:

I’m trying to set up a Spring Boot 3 application that uses ActiveMQ Artemis as the embedded broker. I want to use a custom ConnectionFactory for my JMS messaging, but I’m unable to get it working. When I try to connect to the broker using my custom connection factory, I get the error AMQ219007: Cannot connect to server(s). Tried with all available servers..

I’ve tried setting the ActiveMQConnectionFactory URL to localhost, 0.0.0.0, and the machine’s actual IP address, but none of these seem to work.

As a workaround, I created a wrapper component that returns the same connection factory instance for different profiles, but this is not a recommended solution.

The Solutions:

Solution 1: Using ActiveMQConnectionFactory

To create a custom JMS ConnectionFactory with ActiveMQ Artemis in Spring Boot 3, you can follow these steps:

  1. Add the necessary Maven dependencies to your project. The `artemis-jakarta-server` dependency will provide the ActiveMQ Artemis client library, and the `spring-boot-starter-artemis` dependency will provide the Spring Boot integration for ActiveMQ Artemis.
  2. Configure the ActiveMQ Artemis broker. You can do this by setting the `spring.artemis.mode` property to `embedded` and the `spring.artemis.embedded.enabled` property to `true` in your application.properties file. This will configure Spring Boot to use an embedded ActiveMQ Artemis broker.
  3. Create a custom JMS ConnectionFactory bean. You can do this by creating a Spring bean that implements the `ConnectionFactory` interface. In your bean, you can use the `ActiveMQConnectionFactory` class to create a connection factory for the embedded ActiveMQ Artemis broker.
  4. Inject the custom JMS ConnectionFactory into your Spring components. You can do this by using the `@Autowired` annotation.
  5. Use the custom JMS ConnectionFactory to create JMS connections and sessions. You can use the `ConnectionFactory` interface to create `Connection` objects, and the `Connection` interface to create `Session` objects.

By following these steps, you can create a custom JMS ConnectionFactory with ActiveMQ Artemis in Spring Boot 3.

Q&A

Is it feasible to have multiple Embedded ActiveMQ Artemis brokers on a single JVM?

Yes, it is possible to have multiple embedded brokers on a single JVM.

What is the recommended approach to creating custom JMS ConnectionFactory instances with ActiveMQ Artemis in a Spring Boot 3 application?

Use the ActiveMQConnectionFactory class to create custom connection factories.

Video Explanation:

The following video, titled "Java Spring Boot - Artemis JMS Broker - Sender and Receiver ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

Learn how to create Java Spring Boot Web Service which to communicate with ArtemisMQ. We are creating classes responsible for sending ...