Skip to content

Commit e5656ab

Browse files
committed
Spring Boot中使用RabbitMQ
1 parent 2e13917 commit e5656ab

File tree

7 files changed

+153
-0
lines changed

7 files changed

+153
-0
lines changed

Chapter5-2-1/pom.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>rabbitmq-hello</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>rabbitmq-hello</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.3.7.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-amqp</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
</dependencies>
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-maven-plugin</artifactId>
43+
</plugin>
44+
</plugins>
45+
</build>
46+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class HelloApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(HelloApplication.class, args);
11+
}
12+
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.didispace.rabbit;
2+
3+
import org.springframework.amqp.core.Queue;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
/**
8+
* @author 翟永超
9+
* @create 2016/9/25.
10+
* @blog http://blog.didispace.com
11+
*/
12+
@Configuration
13+
public class RabbitConfig {
14+
15+
@Bean
16+
public Queue helloQueue() {
17+
return new Queue("hello");
18+
}
19+
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.didispace.rabbit;
2+
3+
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
4+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
5+
import org.springframework.cache.annotation.Cacheable;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.Date;
9+
10+
/**
11+
* @author 翟永超
12+
* @create 2016/9/25.
13+
* @blog http://blog.didispace.com
14+
*/
15+
@Component
16+
@RabbitListener(queues = "hello")
17+
public class Receiver {
18+
19+
@RabbitHandler
20+
public void process(String hello) {
21+
System.out.println("Receiver : " + hello);
22+
}
23+
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.didispace.rabbit;
2+
3+
import org.springframework.amqp.core.AmqpTemplate;
4+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.Date;
9+
10+
@Component
11+
public class Sender {
12+
13+
@Autowired
14+
private AmqpTemplate rabbitTemplate;
15+
16+
public void send() {
17+
String context = "hello " + new Date();
18+
System.out.println("Sender : " + context);
19+
this.rabbitTemplate.convertAndSend("hello", context);
20+
}
21+
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring.application.name=rabbitmq-hello
2+
3+
spring.rabbitmq.host=localhost
4+
spring.rabbitmq.port=5672
5+
spring.rabbitmq.username=springcloud
6+
spring.rabbitmq.password=123456
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.didispace;
2+
3+
import com.didispace.rabbit.Sender;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.SpringApplicationConfiguration;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@SpringApplicationConfiguration(classes = HelloApplication.class)
12+
public class HelloApplicationTests {
13+
14+
@Autowired
15+
private Sender sender;
16+
17+
@Test
18+
public void hello() throws Exception {
19+
sender.send();
20+
}
21+
22+
}

0 commit comments

Comments
 (0)