Skip to content

Commit

Permalink
Spring Boot中使用RabbitMQ
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc87112 committed Sep 25, 2016
1 parent 2e13917 commit e5656ab
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Chapter5-2-1/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
13 changes: 13 additions & 0 deletions Chapter5-2-1/src/main/java/com/didispace/HelloApplication.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
20 changes: 20 additions & 0 deletions Chapter5-2-1/src/main/java/com/didispace/rabbit/RabbitConfig.java
Original file line number Diff line number Diff line change
@@ -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");
}

}
24 changes: 24 additions & 0 deletions Chapter5-2-1/src/main/java/com/didispace/rabbit/Receiver.java
Original file line number Diff line number Diff line change
@@ -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 changes: 22 additions & 0 deletions Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions Chapter5-2-1/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();
}

}

0 comments on commit e5656ab

Please sign in to comment.