From e5656ab36fef4396b77a69f1f0acc27269d98379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=9F=E6=B0=B8=E8=B6=85?= Date: Sun, 25 Sep 2016 17:02:39 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=E4=B8=AD=E4=BD=BF=E7=94=A8RabbitM?= =?UTF-8?q?Q?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter5-2-1/pom.xml | 46 +++++++++++++++++++ .../java/com/didispace/HelloApplication.java | 13 ++++++ .../com/didispace/rabbit/RabbitConfig.java | 20 ++++++++ .../java/com/didispace/rabbit/Receiver.java | 24 ++++++++++ .../java/com/didispace/rabbit/Sender.java | 22 +++++++++ .../src/main/resources/application.properties | 6 +++ .../com/didispace/HelloApplicationTests.java | 22 +++++++++ 7 files changed, 153 insertions(+) create mode 100644 Chapter5-2-1/pom.xml create mode 100644 Chapter5-2-1/src/main/java/com/didispace/HelloApplication.java create mode 100644 Chapter5-2-1/src/main/java/com/didispace/rabbit/RabbitConfig.java create mode 100644 Chapter5-2-1/src/main/java/com/didispace/rabbit/Receiver.java create mode 100644 Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java create mode 100644 Chapter5-2-1/src/main/resources/application.properties create mode 100644 Chapter5-2-1/src/test/java/com/didispace/HelloApplicationTests.java diff --git a/Chapter5-2-1/pom.xml b/Chapter5-2-1/pom.xml new file mode 100644 index 0000000..36c6709 --- /dev/null +++ b/Chapter5-2-1/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + com.didispace + rabbitmq-hello + 0.0.1-SNAPSHOT + jar + + rabbitmq-hello + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.3.7.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-amqp + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/Chapter5-2-1/src/main/java/com/didispace/HelloApplication.java b/Chapter5-2-1/src/main/java/com/didispace/HelloApplication.java new file mode 100644 index 0000000..bcc80ab --- /dev/null +++ b/Chapter5-2-1/src/main/java/com/didispace/HelloApplication.java @@ -0,0 +1,13 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HelloApplication { + + public static void main(String[] args) { + SpringApplication.run(HelloApplication.class, args); + } + +} diff --git a/Chapter5-2-1/src/main/java/com/didispace/rabbit/RabbitConfig.java b/Chapter5-2-1/src/main/java/com/didispace/rabbit/RabbitConfig.java new file mode 100644 index 0000000..fb66b35 --- /dev/null +++ b/Chapter5-2-1/src/main/java/com/didispace/rabbit/RabbitConfig.java @@ -0,0 +1,20 @@ +package com.didispace.rabbit; + +import org.springframework.amqp.core.Queue; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author 翟永超 + * @create 2016/9/25. + * @blog http://blog.didispace.com + */ +@Configuration +public class RabbitConfig { + + @Bean + public Queue helloQueue() { + return new Queue("hello"); + } + +} diff --git a/Chapter5-2-1/src/main/java/com/didispace/rabbit/Receiver.java b/Chapter5-2-1/src/main/java/com/didispace/rabbit/Receiver.java new file mode 100644 index 0000000..6e01402 --- /dev/null +++ b/Chapter5-2-1/src/main/java/com/didispace/rabbit/Receiver.java @@ -0,0 +1,24 @@ +package com.didispace.rabbit; + +import org.springframework.amqp.rabbit.annotation.RabbitHandler; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Component; + +import java.util.Date; + +/** + * @author 翟永超 + * @create 2016/9/25. + * @blog http://blog.didispace.com + */ +@Component +@RabbitListener(queues = "hello") +public class Receiver { + + @RabbitHandler + public void process(String hello) { + System.out.println("Receiver : " + hello); + } + +} diff --git a/Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java b/Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java new file mode 100644 index 0000000..93afe3a --- /dev/null +++ b/Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java @@ -0,0 +1,22 @@ +package com.didispace.rabbit; + +import org.springframework.amqp.core.AmqpTemplate; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Date; + +@Component +public class Sender { + + @Autowired + private AmqpTemplate rabbitTemplate; + + public void send() { + String context = "hello " + new Date(); + System.out.println("Sender : " + context); + this.rabbitTemplate.convertAndSend("hello", context); + } + +} \ No newline at end of file diff --git a/Chapter5-2-1/src/main/resources/application.properties b/Chapter5-2-1/src/main/resources/application.properties new file mode 100644 index 0000000..0e1db36 --- /dev/null +++ b/Chapter5-2-1/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.application.name=rabbitmq-hello + +spring.rabbitmq.host=localhost +spring.rabbitmq.port=5672 +spring.rabbitmq.username=springcloud +spring.rabbitmq.password=123456 diff --git a/Chapter5-2-1/src/test/java/com/didispace/HelloApplicationTests.java b/Chapter5-2-1/src/test/java/com/didispace/HelloApplicationTests.java new file mode 100644 index 0000000..82f32c2 --- /dev/null +++ b/Chapter5-2-1/src/test/java/com/didispace/HelloApplicationTests.java @@ -0,0 +1,22 @@ +package com.didispace; + +import com.didispace.rabbit.Sender; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = HelloApplication.class) +public class HelloApplicationTests { + + @Autowired + private Sender sender; + + @Test + public void hello() throws Exception { + sender.send(); + } + +}