Spring Boot Integrated ACTIVEMQ

Source: Internet
Author: User

It is relatively simple to integrate ACTIVEMQ in spring boot, there is no need to install any services, the default use of memory Activemq, of course, with ACTIVEMQ server is better. Here we briefly describe how to use this section is mainly divided into the following several steps:

(1) New Maven Java Project;

(2) The introduction of dependency in Pom.xml;

(3) Coding test

(4) configuration information

Now take a look at each step:

(1) New Maven Java Project;

A new project named SPRING-BOOT-ACTIVEMQ

(2) The introduction of dependency in Pom.xml;

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

xsi:schemalocation= "Http://www.1.qixoo.com.maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd" >

<modelVersion>4.0.0</modelVersion>



<groupId>com.kfit</groupId>

<artifactId>spring-boot-activemq</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>



<name>spring-boot-activemq</name>

<url>http://maven.apache.org</url>



<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<!--JDK version number, Angel use 1.8 here, you can modify the JDK version number for your local configuration.

<java.version>1.8</java.version>

</properties>



<!--

Spring Boot parent Node dependency,

After introducing this, the relevant introduction does not need to add the version configuration,

Spring boot will automatically select the most appropriate version to add.

-

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.4.0.RELEASE</version>

</parent>



<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<scope>test</scope>

</dependency>



<!--spring Boot Web support:mvc,aop...-->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>



<!--ACTIVEMQ Support--

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-activemq</artifactId>

</dependency>



</dependencies>

</project>

Here is the introduction of ACTIVEMQ dependencies, where it is important to note that the Spring boot version is 1.4.0 before most of the examples are 1.3.3, here must be 1.4+ or not support ACTIVEMQ.

(3) Coding test

Here are mainly related to several roles, message producers, Message Queuing, message consumers. So it just needs to be solved and the code is done.

Message Queuing queue, which is written here in the startup class App.java, is injected in a @bean manner:

Package com.kfit;



Import Javax.jms.Queue;



Import Org.apache.activemq.command.ActiveMQQueue;

Import org.springframework.boot.SpringApplication;

Import org.springframework.boot.autoconfigure.SpringBootApplication;

Import Org.springframework.context.annotation.Bean;



/**

*

* @author Angel--Guardian Angel

* @version v.0.1

* @date August 23, 2016

*/

@SpringBootApplication

public class App {

@Bean

Public queue Queue () {

Returnnew activemqqueue ("Sample.queue");

}



public static void Main (string[] args) {

Springapplication.run (App.class, args);

}

}

A activemqqueue is injected here.

Message producer Com.kfit.demo.Producer:

Package Com.kfit.demo;



Import Javax.jms.Queue;



Import org.springframework.beans.factory.annotation.Autowired;

Import Org.springframework.jms.core.JmsMessagingTemplate;

Import org.springframework.scheduling.annotation.EnableScheduling;

Import org.springframework.scheduling.annotation.Scheduled;

Import org.springframework.stereotype.Component;



/**

* Message producer.

* @author Angel--Guardian Angel

* @version v.0.1

* @date August 23, 2016

*/

@Component

@EnableScheduling

public class Producer {



@Autowired

Private Jmsmessagingtemplate jmsmessagingtemplate;



@Autowired

Private queue queue;



@Scheduled (fixeddelay=3000)//3s execution 1 times

public void Send () {

This.jmsMessagingTemplate.convertAndSend (This.queue, "hi,activemq");

}



}

This uses jmsmessagingtemplate for the operation of the message, and then uses the task to schedule the publication of the message 1 times for 3 seconds.

Message Consumer Com.kfit.demo.Consumer:

Package Com.kfit.demo;

Import Org.springframework.jms.annotation.JmsListener;

Import org.springframework.stereotype.Component;



/**

* Message consumers.

* @author Angel--Guardian Angel

* @version v.0.1

* @date August 23, 2016

*/

@Component

public class Consumer {

@JmsListener (Destination = "Sample.queue")

public void Receivequeue (String text) {

System.out.println (text);

}

}

Here the main is to join the @jmslistener to listen, then receive the message and then print.

All right, here we go. Run the following program to observe the print information of the console:

Hi,activemq

Hi,activemq

Hi,activemq



(4) configuration information

We did not configure the ACTIVEMQ information above, in fact Spring boot provides the default configuration, which we can configure in Application.properties:

# ACTIVEMQ (activemqproperties)

Spring.activemq.broker-url= # URL of the ACTIVEMQ broker. Auto-generated by default. For instance ' tcp://localhost:61616 '

Spring.activemq.in-memory=true # Specify if the default broker URL shouldbe in memory. Ignored if an explicit broker has been specified.

spring.activemq.password= # Login password of the broker.

spring.activemq.user= # Login user of the broker.

Spring.activemq.packages.trust-all=false # Trust all packages.

spring.activemq.packages.trusted= # comma-separated List of specific Packagesto trust (when not trusting all packages).

spring.activemq.pool.configuration.*= # See pooledconnectionfactory.

Spring.activemq.pool.enabled=false # Whether apooledconnectionfactory should be created instead of a Regularconnectionfactory.

Spring.activemq.pool.expiry-timeout=0 # Connection Expiration timeout inmilliseconds.

spring.activemq.pool.idle-timeout=30000 # Connection Idle timeout inmilliseconds.

Spring.activemq.pool.max-connections=1 # Maximum number of pooled connections.

Spring Boot Integrated ACTIVEMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.