Spring Boot Learning note--spring boot and ACTIVEMQ integration

Source: Internet
Author: User
Tags message queue

Spring boot also provides automatic configuration support for the JMS (Java message Service,java message Service), which mainly supports JMS implementations such as ACTIVEMQ, Artemis, and so on. Here take ACTIVEMQ as an example.

First, the use of embedded ActiveMQ1. Add Activemq Start dependency

Add Activemq dependency information to the pom.xml of the project:

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-activemq</artifactId>    <version>2.0.2.RELEASE</version></dependency>

Or:

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-activemq --><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-activemq</artifactId></dependency>

Because my project uses 2.0.2.RELEASE versions, I can omit the version information.
After you add the SPRING-BOOT-STARTER-ACTIVEMQ dependency, the project automatically loads the jar packages needed to run the ACTIVEMQ into your project, and you can use ACTIVEMQ in your project.

2. Create a Message Queuing object

Write a method for creating a message queue in Demoapplication.java:
Demoapplication.java:

Package Com.zifeiy.demo;import Javax.jms.Queue;import Org.apache.activemq.command.ActiveMQQueue;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cache.annotation.EnableCaching;import Org.springframework.context.annotation.Bean;@SpringBootApplication@EnableCaching Public classDemoApplication {@Bean     PublicQueueQueue() {return New Activemqqueue("Active.queue"); } Public Static void Main(string[] args) {springapplication.Run(DemoApplication.class, args); }}

In the preceding code, @Bean annotations are used to define a bean.

3. Create a message producer

Create a queue message for the controller class Queuecontroller, and write the method of sending the message in the class:
Queuecontroller.java:

Package Com.zifeiy.demo.controller;import Javax.jms.Queue;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jms.core.JmsMessagingTemplate;import org.springframework.web.bind.annotation.RequestMapping;import Org.springframework.web.bind.annotation.RestController;/** Queue Message Controller */@RestController Public classQueuecontroller {@Autowired    PrivateJmsmessagingtemplate jmsmessagingtemplate;@Autowired    PrivateQueue queue;/** Message Producers     */    @RequestMapping("/send") Public void Send() {//Specify the destination and content of the message sent         This.jmsmessagingtemplate.Convertandsend( This.Queue,"New message sent!" "); }}

In the preceding code, the Send () method specifies that the message is sent to the queue object by using the Jmsmessagetemplate convertandsend () method, which sends the message "the newly sent messages!" ”。

4. Create a message listener

Create a client controller class Customercontroller and write a method for listening and reading messages in the type:
Customercontroller.java:

package com.zifeiy.demo.controller;import org.springframework.jms.annotation.JmsListener;import org.springframework.web.bind.annotation.RestController;/* * 客户控制器 */@RestControllerpublicclass CustomerController {    /*     * 监听和读取消息     */    @JmsListener(destination="active.queue")    publicvoidreadActiveQueue(String message) {        System.out.println("接受到:" + message);    }}
5. Start the project and test the application

After starting the spring boot project and entering the address in the browser http://localhost:8080/send , the Eclipse console will accept the following information:

接受到:新发送的消息!
Second, the use of external activemq

To modify the configuration file, add the following:

spring.activemq.broker-url=tcp://192.168.2.100:61616

In the above configuration, 192.168.2.100 is the IP address of the remote host, and 61616 is the service port number of the ACTIVEMQ.
Access to the ACTIVEMQ management interface is available on the 8161 port of the remote host.

Spring Boot Learning note--spring boot and ACTIVEMQ integration

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.