Spring Boot中使用RabbitMQ
This commit is contained in:
46
Chapter5-2-1/pom.xml
Normal file
46
Chapter5-2-1/pom.xml
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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>com.didispace</groupId>
|
||||
<artifactId>rabbitmq-hello</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>rabbitmq-hello</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.7.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
22
Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java
Normal file
22
Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
6
Chapter5-2-1/src/main/resources/application.properties
Normal file
6
Chapter5-2-1/src/main/resources/application.properties
Normal file
@@ -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
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user