Loading
 
What's Next: View All Tutorial →

Spring Boot ActiveMQ JMS Listener Integration

A simple program to demostrate how to use Activemq using spring boot and creating JMS Listener Download ActiveMQ http://activemq.apache.org/download.html Download the latest version of activemq...

Published Date: 06/09/2018  Last Modified Date: 06/09/2018    New Post



A simple program to demostrate how to use Activemq using spring boot and creating JMS Listener

Download ActiveMQ


http://activemq.apache.org/download.html

Download the latest version of activemq and extract the folder

Go bin directory and execute following command
 
D:\apache-activemq-5.15.5\bin>activemq start

The ActiveMq will be intialized
 
http://localhost:8161/admin/index.jsp
User Name: admin
Password: admin

In the Menu bar, we can select Queue, Topic etc to view the details

http://localhost:8161/admin/queues.jsp
 

Let's go to Springboot programs

Maven Dependency

 
<?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.demo</groupId>
    <artifactId>spring-boot-jms-activemq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-jms-activemq</name>
    <description>Demo project for Spring Boot JPA</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.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.apache.activemq</groupId>
            <artifactId>activemq-broker</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
           
        </plugins>
    </build>


</project>
 

JMSConfig.java

 
package com.demo.spring;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;

@Configuration
public class JmsConfig {

    String BROKER_URL = "tcp://localhost:61616";
    String BROKER_USERNAME = "admin";
    String BROKER_PASSWORD = "admin";

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
        cf.setBrokerURL(BROKER_URL);
        cf.setPassword(BROKER_USERNAME);
        cf.setUserName(BROKER_PASSWORD);
        return cf;
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(connectionFactory());
        template.setDefaultDestinationName("testQueue");
        return template;
    }

    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setConcurrency("1-1");
        return factory;
    }

}


tcp://localhost:61616  is the default portocol 
 

MyJMSListener.java

 
package com.demo.spring;

import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.apache.activemq.Message;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

@Component
public class MyJmsListener {

    /*
     *
     * It create a JMS Listener and consume every message from inbound.queue and
     * post back the same message to outbound.queue
     *
     */
    @JmsListener(destination = "inbound.queue")
    @SendTo("outbound.queue")
    public String receiveMessage(final Message strMessage) throws JMSException {
        String messageData = null;
        System.out.println("Received message " + strMessage);
        String response = null;
        if (strMessage instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) strMessage;
            messageData = textMessage.getText();
            response = "Message: " + messageData;
        }
        return response;
    }

    /*
     *
     * It create a JMS Listener and consume every message from inbound.topic and
     * post back the same message to outbound.topic
     */
    @JmsListener(destination = "inbound.topic")
    @SendTo("outbound.topic")
    public String receiveMessageFromTopic(final Message strMessage) throws JMSException {
        String messageData = null;
        System.out.println("Received message " + strMessage);
        String response = null;
        if (strMessage instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) strMessage;
            messageData = textMessage.getText();
            response = "Message: " + messageData;
        }
        return response;
    }

}


Application.java

 
package com.demo.spring;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

@SpringBootApplication
@EnableJms
public class Application {

    public static void main(String[] args) throws JMSException {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        JmsTemplate jt = ctx.getBean(JmsTemplate.class);

        /*Post 10 message to testQueue*/
        for (int i = 0; i < 10; i++) {
            final int count = i;
            jt.send("testQueue", new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    TextMessage message = session.createTextMessage();
                    message.setText("message" + count);
                    return message;
                }
            });
        }
        /*Receive 10 message from testQueue*/
        for (int i = 0; i < 10; i++) {

            Message message = jt.receive("testQueue");
            if (message instanceof TextMessage) {
                TextMessage tm = (TextMessage) message;
                System.out.println(tm.getText());
            }

        }

    }

}
 

Output:

2018-09-06 16:08:53.810  INFO 1452 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2018-09-06 16:08:54.128  INFO 1452 --- [           main] com.demo.spring.Application              : Started Application in 2.372 seconds (JVM running for 2.801)
message0
message1
message2
message3
message4
message5
message6
message7
message8
message9


 
Awaiting for Administrator approval




← Back to list


Related Post




×