Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, May 1, 2020

Spring Boot ActiveMQ Standalone Application Example

1. Introduction


In this article, You'll learn how to create a Standalone ActiveMQ Demo application in spring boot using the Producer-Consumer model.

Versions used for spring boot activemq standalone application:

/usr/local/Cellar/activemq/5.15.12
Java 1.8
Spring Boot 2.5

In the previous article, we have shown how to implement ActiveMQ in-memory application in Spring Boot. In this example, Leveraged spring boot builtin active MQ instead of using local or remote active MQ.

Spring Boot ActiveMQ Standalone Application Example




2. Spring Boot ActiveMQ Standalone - Pom.xml


All the dependencies are needed should be in pom.xml then it resolves all internal dependencies automatically.

Main dependencies are defined below.

spring-boot-starter-activemq
spring-boot-starter-web

Complete 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->    </parent>
    <groupId>com.javaprogramto.activemq</groupId>
    <artifactId>activemq-standalone-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>activemq-standalone-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

3. Spring Boot ActiveMQ Standalone - Registered the Queue and JmsTemplate


Create a configuration class that creates Queue, ActiveMQConnectionFactory, and JmsTemplate instances using @Bean annotations.

Create a queue with the name "standalone-activemq-queue".

Most importantly add @EnableJms annotation to enable JMS capabilities.


[package com.javaprogramto.activemq.activemqstandalonedemo.config;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;
import javax.jms.Queue;
@Configuration
@EnableJms
public class ActiveMQConfigurations {

    @Value("${activemq.broker-url-config}")
    private String brokerUrl;
    /**
     * setting the stand alone queue name
     * @return
     */
    @Bean
    public Queue quque(){
        return new ActiveMQQueue("standalone-activemq-queue");
    }
    /**
     *
     * Create and set broker url to the active mq connection factory.
     * @return
     */
    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory(){
        ActiveMQConnectionFactory activeMQConnectionFactory =  new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL(brokerUrl);
        return activeMQConnectionFactory;
    }
    @Bean
    public JmsTemplate jmsTemplate(){
        return new JmsTemplate(activeMQConnectionFactory());
    }


}]


4. Spring Boot ActiveMQ Standalone - Create Producer Implementation


Create a rest endpoint to create the messages and use convertAndSend() method that pushes the message to the queue.

[
package com.javaprogramto.activemq.activemqstandalonedemo.produce;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;
@RestController@RequestMapping("/standalone/publish")
public class MessageProducer {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired private Queue queue;
@Autowired private JmsTemplate jmsTemplate;
@RequestMapping("/{message}")
public String publish(@PathVariable("message") String message ){
jmsTemplate.convertAndSend(queue, message);
logger.info("Message Published : "+message);
return "Message Published";
}
}]

5. Spring Boot ActiveMQ Standalone - Create Consumer Implementation


Create a method with @JmsListner annotation with the destination queue name. So whenever a message is pushed to the queue "standalone-activemq-queue" then the same message is given to the @JmsListner method.


[package com.javaprogramto.activemq.activemqstandalonedemo.consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@Componentpublic class MessageConsumer {
Logger logger = LoggerFactory.getLogger(getClass());
@JmsListener(destination = "standalone-activemq-queue")
public void consume(String message){
logger.info("Message received : "+message);
}
}]

6. Spring Boot ActiveMQ Standalone - application.properties


Add all properties that are required for the active MQ and embedded tomcat server.

spring.activemq.pool.enabled=falseactivemq.broker-url-config=tcp://localhost:61616server.port=8081

7. Start ActiveMQ installed on Local Machine


If you are on mac install activemq using homebrew.

[brew install activemq -> to install active mq

brew services start activemq --> to run active mq in background

brew services stop activemq --> to stop the active MQ running in the background]

or you can navigate to the folder where active is extracted.
There you can start './activeMQ &' command to start the queue.

8. Spring Boot ActiveMQ Standalone - Main Spring Boot Application


[package com.javaprogramto.activemq.activemqstandalonedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class ActivemqStandaloneDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ActivemqStandaloneDemoApplication.class, args);
}
}]


9. Start Spring Boot ActiveMQ Standalone Application


Once you run the application, then the application will be started on port 8081.

Hit the below rest endpoint so that message is pushed to the queue.

http://localhost:8081/standalone/publish/helllo


[2020-05-01 17:20:33.475  INFO 64550 --- [nio-8081-exec-1] c.j.a.a.produce.MessageProducer          : Message Published : helllo
2020-05-01 17:21:01.061  INFO 64550 --- [enerContainer-1] c.j.a.a.consumer.MessageConsumer         : Message received : helllo
2020-05-01 17:21:01.074  INFO 64550 --- [nio-8081-exec-3] c.j.a.a.produce.MessageProducer          : Message Published : helllo
2020-05-01 17:21:03.389  INFO 64550 --- [enerContainer-1] c.j.a.a.consumer.MessageConsumer         : Message received : helllo]


10. ActiveMQ Admin Console


Access the below URL from browser then it will prompt for the user and password. By default, it is admin/admin.


Verify on ActiveMQ admin console

Spring Boot ActiveMQ Standalone admin console

11. Conclusion


In this article, You've seen how to use external standalone activeMQ in spring boot application,

All the code is shown in this article is over GitHub.

You can download the project directly and can run in your local without any errors.


[View on GitHub ##eye##]

[Download ##file-download##]


If you have any queries in building this application please post in the comment section.

Spring Boot ActiveMQ Standalone Example

No comments:

Post a Comment

Please do not add any spam links in the comments section.