Note: Springboot support for AMQP specification middleware is only RABBITMQ
First step: Add dependencies
<!--amqp--><dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-amqp</artifactid></dependency>
Step Two: Configure APPLICATION.YML
SPRING:APPLICATION:NAME:BOOT-RABBITMQ rabbitmq:host:192.168.175.13 port:5672 Username:liuy passwor d:123456
Step three: Write the AMQP queue configuration
Package Com.liuy.config;import Org.springframework.amqp.core.queue;import Org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;/** * AMQP Queue Configuration * @description AMQP queue Configuration * @author Luis * @version 1.0 * @date: August 22, 2017 pm 12:33:36 */@Configurationpublic class A mqpconfiguration {@Beanpublic Queue queue () {return new queue ("Test.queue");}}
Fourth step: Write listening and sending
Package Com.liuy.component;import Org.springframework.amqp.core.amqptemplate;import Org.springframework.amqp.rabbit.annotation.rabbitlistener;import Org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.component;/** * Monitor and send * @description monitor and send * @author Luis * @version 1.0 * @date: August 22, 2017 pm 12:34:55 */@Componentpublic class Amqpcompone NT {@Autowiredprivate amqptemplate amqptemplate;public void Send (String msg) {this.amqpTemplate.convertAndSend (" Test.queue ", msg);} @RabbitListener (queues = "test.queue") public void Receivequeue (String text) {System.out.println ("accepted to:" + text);}}
Fifth step: Writing the Startup class
Package Com.liuy;import Org.springframework.amqp.rabbit.annotation.enablerabbit;import Org.springframework.boot.springapplication;import org.springframework.boot.autoconfigure.SpringBootApplication; Import org.springframework.boot.web.servlet.servletcomponentscan;/** * Integrated rabbitmq--startup class * @description Integrated rabbitmq--Startup class * @author Luis * @version 1.0 * @date: August 22, 2017 12:39:04 */@ Enablerabbit@servletcomponentscan@springbootapplicationpublic class Application {public static void main (string[] args) {Springapplication.run (application.class, args);}}
Sixth step: Test
Package Com.liuy;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.boot.test.context.springboottest;import Org.springframework.test.context.junit4.springrunner;import com.liuy.component.amqpcomponent;/** * Integrated RABBITMQ monitoring and sending test * @description integrated RABBITMQ monitoring and sending test * @author Luis * @version 1.0 * @date: August 22, 2017 pm 12:35:43 */@RunWith (S Pringrunner.class) @SpringBootTestpublic class Amqpcomponenttest {@Autowiredprivate amqpcomponent amqpcomponent;@ testpublic void Send () {amqpcomponent.send ("Integrated RABBITMQ listening and sending Test");}}
Effect:
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9F/5D/wKioL1mbw5XCNFHkAAC246iSTt4608.png "title=" 2d3e1dc9-e7a6-450a-b473-935390eab5b3.png "alt=" Wkiol1mbw5xcnfhkaac246istt4608.png "/>
This article is from "I Love Big gold" blog, please be sure to keep this source http://1754966750.blog.51cto.com/7455444/1958300
Springboot (24): Integrated RABBITMQ