-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Spring Boot 3.0 RabbitMQ examples
- Loading branch information
Showing
29 changed files
with
670 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
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,58 @@ | ||
<?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.neo</groupId> | ||
<artifactId>spring-boot-rabbitmq</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>spring-boot-rabbitmq</name> | ||
<description>Demo project for Spring Boot and rabbitmq</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.0.0</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>17</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> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest-core</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
spring-boot-rabbitmq/src/main/java/com/neo/RabbitMQApplication.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,12 @@ | ||
package com.neo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class RabbitMQApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(RabbitMQApplication.class, args); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
spring-boot-rabbitmq/src/main/java/com/neo/model/User.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,37 @@ | ||
package com.neo.model; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Created by summer on 2016/11/29. | ||
*/ | ||
public class User implements Serializable{ | ||
|
||
private String name; | ||
|
||
private String pass; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getPass() { | ||
return pass; | ||
} | ||
|
||
public void setPass(String pass) { | ||
this.pass = pass; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "User{" + | ||
"name='" + name + '\'' + | ||
", pass='" + pass + '\'' + | ||
'}'; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/FanoutRabbitConfig.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,49 @@ | ||
package com.neo.rabbit; | ||
|
||
import org.springframework.amqp.core.Binding; | ||
import org.springframework.amqp.core.BindingBuilder; | ||
import org.springframework.amqp.core.FanoutExchange; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@Configuration | ||
public class FanoutRabbitConfig { | ||
|
||
@Bean | ||
public Queue AMessage() { | ||
return new Queue("fanout.A"); | ||
} | ||
|
||
@Bean | ||
public Queue BMessage() { | ||
return new Queue("fanout.B"); | ||
} | ||
|
||
@Bean | ||
public Queue CMessage() { | ||
return new Queue("fanout.C"); | ||
} | ||
|
||
@Bean | ||
FanoutExchange fanoutExchange() { | ||
return new FanoutExchange("fanoutExchange"); | ||
} | ||
|
||
@Bean | ||
Binding bindingExchangeA(Queue AMessage,FanoutExchange fanoutExchange) { | ||
return BindingBuilder.bind(AMessage).to(fanoutExchange); | ||
} | ||
|
||
@Bean | ||
Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) { | ||
return BindingBuilder.bind(BMessage).to(fanoutExchange); | ||
} | ||
|
||
@Bean | ||
Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) { | ||
return BindingBuilder.bind(CMessage).to(fanoutExchange); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
spring-boot-rabbitmq/src/main/java/com/neo/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,27 @@ | ||
package com.neo.rabbit; | ||
|
||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@Configuration | ||
public class RabbitConfig { | ||
|
||
@Bean | ||
public Queue helloQueue() { | ||
return new Queue("hello"); | ||
} | ||
|
||
@Bean | ||
public Queue neoQueue() { | ||
return new Queue("neo"); | ||
} | ||
|
||
@Bean | ||
public Queue objectQueue() { | ||
return new Queue("object"); | ||
} | ||
|
||
|
||
} |
41 changes: 41 additions & 0 deletions
41
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/TopicRabbitConfig.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,41 @@ | ||
package com.neo.rabbit; | ||
|
||
import org.springframework.amqp.core.Binding; | ||
import org.springframework.amqp.core.BindingBuilder; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.amqp.core.TopicExchange; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
|
||
@Configuration | ||
public class TopicRabbitConfig { | ||
|
||
final static String message = "topic.message"; | ||
final static String messages = "topic.messages"; | ||
|
||
@Bean | ||
public Queue queueMessage() { | ||
return new Queue(TopicRabbitConfig.message); | ||
} | ||
|
||
@Bean | ||
public Queue queueMessages() { | ||
return new Queue(TopicRabbitConfig.messages); | ||
} | ||
|
||
@Bean | ||
TopicExchange exchange() { | ||
return new TopicExchange("topicExchange"); | ||
} | ||
|
||
@Bean | ||
Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) { | ||
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message"); | ||
} | ||
|
||
@Bean | ||
Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) { | ||
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/fanout/FanoutReceiverA.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,16 @@ | ||
package com.neo.rabbit.fanout; | ||
|
||
import org.springframework.amqp.rabbit.annotation.RabbitHandler; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RabbitListener(queues = "fanout.A") | ||
public class FanoutReceiverA { | ||
|
||
@RabbitHandler | ||
public void process(String message) { | ||
System.out.println("fanout Receiver A : " + message); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/fanout/FanoutReceiverB.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,16 @@ | ||
package com.neo.rabbit.fanout; | ||
|
||
import org.springframework.amqp.rabbit.annotation.RabbitHandler; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RabbitListener(queues = "fanout.B") | ||
public class FanoutReceiverB { | ||
|
||
@RabbitHandler | ||
public void process(String message) { | ||
System.out.println("fanout Receiver B: " + message); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/fanout/FanoutReceiverC.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,16 @@ | ||
package com.neo.rabbit.fanout; | ||
|
||
import org.springframework.amqp.rabbit.annotation.RabbitHandler; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RabbitListener(queues = "fanout.C") | ||
public class FanoutReceiverC { | ||
|
||
@RabbitHandler | ||
public void process(String message) { | ||
System.out.println("fanout Receiver C: " + message); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/fanout/FanoutSender.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,19 @@ | ||
package com.neo.rabbit.fanout; | ||
|
||
import org.springframework.amqp.core.AmqpTemplate; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class FanoutSender { | ||
|
||
@Autowired | ||
private AmqpTemplate rabbitTemplate; | ||
|
||
public void send() { | ||
String context = "hi, fanout msg "; | ||
System.out.println("Sender : " + context); | ||
this.rabbitTemplate.convertAndSend("fanoutExchange","", context); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/hello/HelloReceiver.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,19 @@ | ||
package com.neo.rabbit.hello; | ||
|
||
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; | ||
|
||
@Component | ||
@RabbitListener(queues = "hello") | ||
public class HelloReceiver { | ||
|
||
@RabbitHandler | ||
public void process(String hello) { | ||
System.out.println("Receiver : " + hello); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/hello/HelloSender.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.neo.rabbit.hello; | ||
|
||
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 HelloSender { | ||
|
||
@Autowired | ||
private AmqpTemplate rabbitTemplate; | ||
|
||
public void send() { | ||
String context = "hello " + new Date(); | ||
System.out.println("Sender : " + context); | ||
this.rabbitTemplate.convertAndSend("hello", context); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/many/NeoReceiver1.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,16 @@ | ||
package com.neo.rabbit.many; | ||
|
||
import org.springframework.amqp.rabbit.annotation.RabbitHandler; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RabbitListener(queues = "neo") | ||
public class NeoReceiver1 { | ||
|
||
@RabbitHandler | ||
public void process(String neo) { | ||
System.out.println("Receiver 1: " + neo); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
spring-boot-rabbitmq/src/main/java/com/neo/rabbit/many/NeoReceiver2.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,16 @@ | ||
package com.neo.rabbit.many; | ||
|
||
import org.springframework.amqp.rabbit.annotation.RabbitHandler; | ||
import org.springframework.amqp.rabbit.annotation.RabbitListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RabbitListener(queues = "neo") | ||
public class NeoReceiver2 { | ||
|
||
@RabbitHandler | ||
public void process(String neo) { | ||
System.out.println("Receiver 2: " + neo); | ||
} | ||
|
||
} |
Oops, something went wrong.