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(); + } + +}