forked from dyc87112/SpringBoot-Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
Chapter5-2-1/src/main/java/com/didispace/HelloApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
Chapter5-2-1/src/main/java/com/didispace/rabbit/RabbitConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
Chapter5-2-1/src/main/java/com/didispace/rabbit/Receiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
Chapter5-2-1/src/main/java/com/didispace/rabbit/Sender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
22 changes: 22 additions & 0 deletions
22
Chapter5-2-1/src/test/java/com/didispace/HelloApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |