$show=/label

Spring Boot ActiveMQ Standalone Application Example

SHARE:

A quick guide to how to develop a standalone ActiveMQ application in Spring Boot. Activemq standalone Example program to implement producer and consumer application that connects to active amq installed on different locations (local, remote, or distributed).

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

COMMENTS

BLOGGER

About Us

Author: Venkatesh - I love to learn and share the technical stuff.
Name

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,
ltr
item
JavaProgramTo.com: Spring Boot ActiveMQ Standalone Application Example
Spring Boot ActiveMQ Standalone Application Example
A quick guide to how to develop a standalone ActiveMQ application in Spring Boot. Activemq standalone Example program to implement producer and consumer application that connects to active amq installed on different locations (local, remote, or distributed).
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2iBSCp_cSWWuURhzQLzoYcTveRC_6jyqkgB8AhWJ3kJPmmqawRiW_el2jTXKFAkEyryxy5On5HWm2y_QdCFnml445wufPuNNKQPAAucneHJsAbMjiIIPk2muvg9PQzChsU2DMiAX4TcY/s640/Spring+Boot+ActiveMQ+Standalone+Application+Example-min.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2iBSCp_cSWWuURhzQLzoYcTveRC_6jyqkgB8AhWJ3kJPmmqawRiW_el2jTXKFAkEyryxy5On5HWm2y_QdCFnml445wufPuNNKQPAAucneHJsAbMjiIIPk2muvg9PQzChsU2DMiAX4TcY/s72-c/Spring+Boot+ActiveMQ+Standalone+Application+Example-min.png
JavaProgramTo.com
https://www.javaprogramto.com/2020/05/spring-boot-activemq-standalone-example.html
https://www.javaprogramto.com/
https://www.javaprogramto.com/
https://www.javaprogramto.com/2020/05/spring-boot-activemq-standalone-example.html
true
3124782013468838591
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content