JMS Application with Spring Boot

Spring provides a JMS Integration framework that simplifies the use of JMS API.

The key class provided by Spring is JmsTemplate : JmsTemplate class handles the creation and releasing of resources when sending or synchronously receiving messages. Starting with Spring 4.1, JmsMessagingTemplate is built on top of JmsTemplate which provides an integration with the messaging abstraction, i.e., org.springframework.messaging.Message. This, in turn, allows to create a message to send in generic manner.

In order to connect and be able to send/receive messages, a ConnectionFactory must be configured. ConnectionFactory is one of the JMS administered objects which are preconfigured by an administrator. A client with the help of the configuration will make the connection with a JMS provider.

Spring provides two types of ConnectionFactory :

  • SingleConnectionFactory : is an implementation of ConnectionFactory interface, that will return the same connection on all createConnection() calls and ignore calls to close()
  • CachingConnectionFactory : extends the functionality of the SingleConnectionFactory and adds enhances it with a caching of Sessions, MessageProducers and MessageConsumers

Also, Message Listener Container must be configured.

Let’s see it in an example. It is a Maven Spring Boot JMS application which sends messages to ArtemisMQ JMS Server (more about building an application with spring boot – here). Here is final pom.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>SpringBootJMS</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.4.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-artemis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>artemis-jms-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

Next

Leave a Reply

Your email address will not be published. Required fields are marked *