2018年3月9日 | Leave a comment 解决: Spring Boot + rabbitmq demo Java package demo.spmq1; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.amqp.core.Binding; import org.springframework.amqp.core.Binding.DestinationType; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Lazy; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @SpringBootApplication @EnableScheduling public class App { private static final Logger LOG = Logger.getLogger(Sender.class.getName()); public static void main(String[] argv) { SpringApplication.run(App.class, argv); } @Component @Lazy(false) public static class Sender { final AmqpTemplate template; final AmqpAdmin admin; @Autowired public Sender(AmqpTemplate template, AmqpAdmin admin) { this.template = template; this.admin = admin; this.admin.declareExchange(new TopicExchange("ex1")); this.admin.declareQueue(new Queue("q2")); final Queue q1 = this.admin.declareQueue(); this.admin.declareBinding(new Binding("q2", DestinationType.QUEUE, "ex1", "", null)); this.admin.declareBinding(new Binding(q1.getName(), DestinationType.QUEUE, "ex1", "", null)); } @Scheduled(fixedRate = 1000) public void send() { LOG.info("send begin"); final Map<String, Object> map = new HashMap<>(); map.put("one", 1); map.put("t", new Date()); map.put("name", "你好"); template.convertAndSend("ex1", "", map); } } @Component @Lazy(false) public static class R2 { // @RabbitListener(bindings = { // @QueueBinding(exchange = @Exchange("ex1"), value = @Queue())}) @RabbitListener(queues = {"q2"}) public void onrecv(Map map) { System.out.println("R2 recv:" + map); } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 package demo.spmq1; import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.logging.Logger;import org.springframework.amqp.core.AmqpAdmin;import org.springframework.amqp.core.AmqpTemplate;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.Binding.DestinationType;import org.springframework.amqp.core.Queue;import org.springframework.amqp.core.TopicExchange;import org.springframework.amqp.rabbit.annotation.RabbitListener;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Lazy;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component; @SpringBootApplication@EnableSchedulingpublic class App { private static final Logger LOG = Logger.getLogger(Sender.class.getName()); public static void main(String[] argv) { SpringApplication.run(App.class, argv); } @Component @Lazy(false) public static class Sender { final AmqpTemplate template; final AmqpAdmin admin; @Autowired public Sender(AmqpTemplate template, AmqpAdmin admin) { this.template = template; this.admin = admin; this.admin.declareExchange(new TopicExchange("ex1")); this.admin.declareQueue(new Queue("q2")); final Queue q1 = this.admin.declareQueue(); this.admin.declareBinding(new Binding("q2", DestinationType.QUEUE, "ex1", "", null)); this.admin.declareBinding(new Binding(q1.getName(), DestinationType.QUEUE, "ex1", "", null)); } @Scheduled(fixedRate = 1000) public void send() { LOG.info("send begin"); final Map<String, Object> map = new HashMap<>(); map.put("one", 1); map.put("t", new Date()); map.put("name", "你好"); template.convertAndSend("ex1", "", map); } } @Component @Lazy(false) public static class R2 {// @RabbitListener(bindings = {// @QueueBinding(exchange = @Exchange("ex1"), value = @Queue())}) @RabbitListener(queues = {"q2"}) public void onrecv(Map map) { System.out.println("R2 recv:" + map); } }} pom.xml <?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>demo</groupId> <artifactId>spmq1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies> </project> 123456789101112131415161718192021222324 <?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>demo</groupId> <artifactId>spmq1</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies></project> 参考: 《Spring boot refrence》