diff --git a/spring-amqp/README.md b/spring-amqp/README.md
new file mode 100644
index 0000000000..0ae4eda12e
--- /dev/null
+++ b/spring-amqp/README.md
@@ -0,0 +1,8 @@
+## Spring AMQP
+
+This module contains articles about Spring with the AMQP messaging system
+
+## Relevant articles:
+
+- [Messaging With Spring AMQP](https://www.baeldung.com/spring-amqp)
+- [RabbitMQ Message Dispatching with Spring AMQP](https://www.baeldung.com/rabbitmq-spring-amqp)
\ No newline at end of file
diff --git a/spring-amqp/pom.xml b/spring-amqp/pom.xml
new file mode 100644
index 0000000000..3e6789dcb6
--- /dev/null
+++ b/spring-amqp/pom.xml
@@ -0,0 +1,28 @@
+
To run this particular application with mvn you use the following command:
+ * {@code + * mvn spring-boot:run -Dstart-class=com.baeldung.springamqp.broadcast.BroadcastMessageApp + * } + */ +@SpringBootApplication +public class BroadcastMessageApp { + + private static String ROUTING_KEY_USER_IMPORTANT_WARN = "user.important.warn"; + private static String ROUTING_KEY_USER_IMPORTANT_ERROR = "user.important.error"; + + public static void main(String[] args) { + SpringApplication.run(BroadcastMessageApp.class, args); + } + + @Bean + public ApplicationRunner runner(RabbitTemplate rabbitTemplate) { + String message = " payload is broadcast"; + return args -> { + rabbitTemplate.convertAndSend(BroadcastConfig.FANOUT_EXCHANGE_NAME, "", "fanout" + message); + rabbitTemplate.convertAndSend(BroadcastConfig.TOPIC_EXCHANGE_NAME, ROUTING_KEY_USER_IMPORTANT_WARN, "topic important warn" + message); + rabbitTemplate.convertAndSend(BroadcastConfig.TOPIC_EXCHANGE_NAME, ROUTING_KEY_USER_IMPORTANT_ERROR, "topic important error" + message); + }; + } + + @RabbitListener(queues = { FANOUT_QUEUE_1_NAME }) + public void receiveMessageFromFanout1(String message) { + System.out.println("Received fanout 1 message: " + message); + } + + @RabbitListener(queues = { FANOUT_QUEUE_2_NAME }) + public void receiveMessageFromFanout2(String message) { + System.out.println("Received fanout 2 message: " + message); + } + + @RabbitListener(queues = { TOPIC_QUEUE_1_NAME }) + public void receiveMessageFromTopic1(String message) { + System.out.println("Received topic 1 (" + BINDING_PATTERN_IMPORTANT + ") message: " + message); + } + + @RabbitListener(queues = { TOPIC_QUEUE_2_NAME }) + public void receiveMessageFromTopic2(String message) { + System.out.println("Received topic 2 (" + BINDING_PATTERN_ERROR + ") message: " + message); + } +} diff --git a/spring-amqp/src/main/java/com/baeldung/springamqp/simple/HelloWorldMessageApp.java b/spring-amqp/src/main/java/com/baeldung/springamqp/simple/HelloWorldMessageApp.java new file mode 100644 index 0000000000..25dcdf29c1 --- /dev/null +++ b/spring-amqp/src/main/java/com/baeldung/springamqp/simple/HelloWorldMessageApp.java @@ -0,0 +1,38 @@ +package com.baeldung.springamqp.simple; + +import org.springframework.amqp.core.Queue; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +public class HelloWorldMessageApp { + + private static final boolean NON_DURABLE = false; + private static final String MY_QUEUE_NAME = "myQueue"; + + public static void main(String[] args) { + SpringApplication.run(HelloWorldMessageApp.class, args); + } + + @Bean + public ApplicationRunner runner(RabbitTemplate template) { + return args -> { + template.convertAndSend("myQueue", "Hello, world!"); + }; + } + + @Bean + public Queue myQueue() { + return new Queue(MY_QUEUE_NAME, NON_DURABLE); + } + + @RabbitListener(queues = MY_QUEUE_NAME) + public void listen(String in) { + System.out.println("Message read from myQueue : " + in); + } + +}