In this tutorials, we are going to see how we can use Spring Boot Apache ActiveMQ. Spring Boot internally provides an in-memory activemq starter to dealing with JMS messages.

Spring Boot Apache ActiveMQ In Memory:

As part of this example, I am going to create a producer and consumer programs, the producer can produce the message, as soon as the producer produces a message consumer will consume the message.

Technologies:

  • Spring Boot 2.0.5
  • ActiveMQ
  • Java8

Application Structure:

Spring Boot ActiveMQ Inmemory Example

Project Dependencies:

pom.xml
<?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.onlinetutorialspoint</groupId>
  <artifactId>SpringBoot-ActiveMQ-InMemory-Example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringBoot-ActiveMQ-InMemory-Example</name>
  <description>Spring Boot ActiveMq In Memory Example</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Spring ActiveMQ properties – Enabling spring in-memory ActiveMQ.

application.properties
server.port=8080
spring.activemq.in-memory=true
spring.activemq.pool.enable=false

Creating Producer as rest controller.

Providing rest endpoint /publish to send messages to JMS queue using JmsTemplate.

Producer.java
package com.onlinetutorialspoint.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;

@RestController
public class Producer {

    @Autowired
    private Queue queue;

    @Autowired
    private JmsTemplate jmsTemplate;

    @GetMapping("publish/{msg}")
    public String publish(@PathVariable("msg") final String msg){
        jmsTemplate.convertAndSend(queue,msg);
        return "Your message <b>"+msg+"</b> published successfully";
    }
}

JMS Configuration – Creating ActiveMQQueue and giving queue name as simple-jms-queue

JMSConfig.java
package com.onlinetutorialspoint.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;

import javax.jms.Queue;
import java.awt.*;

@Configuration
@EnableJms
public class JMSConfig {
    @Bean
    public Queue queue(){
        return new ActiveMQQueue("simple-jms-queue");
    }
}

Creating Consumer.

Creating JmsListener and add this listener to above-created simple-jms-queue. It keeps listens to the simple-jms-queue, as soon as this queue get updated the listener will notify.

Consumer.java
package com.onlinetutorialspoint.listener;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    @JmsListener(destination = "simple-jms-queue")
    public void listener(String msg){
        System.out.println("Received Message : "+msg);
    }
}

Main class

SpringBootActiveMqInMemoryExampleApplication.java
package com.onlinetutorialspoint;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootActiveMqInMemoryExampleApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringBootActiveMqInMemoryExampleApplication.class, args);
  }
}

Run it.

Terminal

mvn spring-boot:run
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.6.RELEASE)

2018-10-28 21:52:11.003  INFO 13024 --- [           main] ngBootActiveMqInMemoryExampleApplication : Starting SpringBootActiveMqInMemoryExampleApplication on DESKTOP-RN4SMHT with PID 13024 (E:\work\SpringBoot-ActiveMQ-InMemory-Example\target\classes started by Lenovo in E:\work\SpringBoot-ActiveMQ-InMemory-Example)
2018-10-28 21:52:11.038  INFO 13024 --- [           main] ngBootActiveMqInMemoryExampleApplication : No active profile set, falling back to default profiles: default
2018-10-28 21:52:11.256  INFO 13024 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6f15d60e: startup date [Sun Oct 28 21:52:11 IST 2018]; root of context hierarchy
....
....

Access the application and send any message.

Spring Boot ActiveMQ Inmemory Example 2

Message get published successfully, then we can check for application logs to confirm whether the consumer consumed the message or not.

Spring Boot ActiveMQ Inmemory Example 3

Yes, we can see the received message from the consumer. This is how we can use the Spring Boot in memory ActiveMQ.

Reference:

Apache ActiveMQ

Happy Learning 🙂

Download Example