fanoutBindings() {
- Queue fanoutQueue1 = new Queue(fanoutQueue1Name, false);
- Queue fanoutQueue2 = new Queue(fanoutQueue2Name, false);
-
- FanoutExchange fanoutExchange = new FanoutExchange(fanoutExchangeName);
-
- return Arrays.asList(
- fanoutQueue1,
- fanoutQueue2,
- fanoutExchange,
- BindingBuilder.bind(fanoutQueue1).to(fanoutExchange),
- BindingBuilder.bind(fanoutQueue2).to(fanoutExchange)
- );
- }
-
- @Bean
- public SimpleRabbitListenerContainerFactory broadcastContainer(ConnectionFactory connectionFactory, SimpleRabbitListenerContainerFactoryConfigurer configurer) {
- SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
- configurer.configure(factory, connectionFactory);
- return factory;
- }
-
-}
diff --git a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageConsumers.java b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageConsumers.java
deleted file mode 100644
index 0ae04ea092..0000000000
--- a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageConsumers.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package com.baeldung.springamqpsimple.broadcast;
-
-import com.baeldung.springamqpsimple.MessageConsumer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.amqp.rabbit.annotation.RabbitListener;
-import org.springframework.stereotype.Component;
-
-@Component
-public class BroadcastMessageConsumers {
- private static final Logger logger = LoggerFactory.getLogger(MessageConsumer.class);
-
- @RabbitListener(queues = {BroadcastConfig.fanoutQueue1Name})
- public void receiveMessageFromFanout1(String message) {
- logger.info("Received fanout 1 message: " + message);
- }
-
- @RabbitListener(queues = {BroadcastConfig.fanoutQueue2Name})
- public void receiveMessageFromFanout2(String message) {
- logger.info("Received fanout 2 message: " + message);
- }
-
- @RabbitListener(queues = {BroadcastConfig.topicQueue1Name})
- public void receiveMessageFromTopic1(String message) {
- logger.info("Received topic 1 message: " + message);
- }
-
- @RabbitListener(queues = {BroadcastConfig.topicQueue2Name})
- public void receiveMessageFromTopic2(String message) {
- logger.info("Received topic 2 message: " + message);
- }
-}
diff --git a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageController.java b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageController.java
deleted file mode 100644
index 75d4d20e55..0000000000
--- a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageController.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.baeldung.springamqpsimple.broadcast;
-
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.ResponseStatus;
-
-@Controller
-public class BroadcastMessageController {
-
- private final BroadcastMessageProducer messageProducer;
-
- @Autowired
- public BroadcastMessageController(BroadcastMessageProducer messageProducer) {
- this.messageProducer = messageProducer;
- }
-
- @RequestMapping(value="/broadcast", method= RequestMethod.POST)
- @ResponseStatus(value= HttpStatus.CREATED)
- public void sendMessage(@RequestBody String message) {
- messageProducer.sendMessages(message);
- }
-}
diff --git a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageProducer.java b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageProducer.java
deleted file mode 100644
index 590da24962..0000000000
--- a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/broadcast/BroadcastMessageProducer.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package com.baeldung.springamqpsimple.broadcast;
-
-import org.springframework.amqp.rabbit.core.RabbitTemplate;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.stereotype.Component;
-
-@Component
-public class BroadcastMessageProducer {
-
- private final RabbitTemplate rabbitTemplate;
-
- @Autowired
- public BroadcastMessageProducer(RabbitTemplate rabbitTemplate) {
- this.rabbitTemplate = rabbitTemplate;
- }
-
- public void sendMessages(String message) {
- rabbitTemplate.convertAndSend(BroadcastConfig.fanoutExchangeName, "", message);
- rabbitTemplate.convertAndSend(BroadcastConfig.topicExchangeName, "user.not-important.info", message);
- rabbitTemplate.convertAndSend(BroadcastConfig.topicExchangeName, "user.important.error", message);
- }
-}
diff --git a/spring-amqp-simple/src/main/resources/application.yaml b/spring-amqp-simple/src/main/resources/application.yaml
deleted file mode 100644
index 47e7a3b9e0..0000000000
--- a/spring-amqp-simple/src/main/resources/application.yaml
+++ /dev/null
@@ -1,5 +0,0 @@
-spring:
- rabbitmq:
- username: guest
- password: guest
- host: 10.10.10.105
\ No newline at end of file
diff --git a/spring-amqp-simple/src/main/resources/logback.xml b/spring-amqp-simple/src/main/resources/logback.xml
deleted file mode 100644
index 7d900d8ea8..0000000000
--- a/spring-amqp-simple/src/main/resources/logback.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
- %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/spring-amqp-simple/src/test/java/org/baeldung/SpringContextManualTest.java b/spring-amqp-simple/src/test/java/org/baeldung/SpringContextManualTest.java
deleted file mode 100644
index 03cb34eeb5..0000000000
--- a/spring-amqp-simple/src/test/java/org/baeldung/SpringContextManualTest.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package org.baeldung;
-
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.test.context.junit4.SpringRunner;
-
-import com.baeldung.springamqpsimple.SpringAmqpApplication;
-
-@RunWith(SpringRunner.class)
-@SpringBootTest(classes = SpringAmqpApplication.class)
-public class SpringContextManualTest {
-
- @Test
- public void whenSpringContextIsBootstrapped_thenNoExceptions() {
- }
-}
diff --git a/spring-amqp-simple/src/test/resources/application.yaml b/spring-amqp-simple/src/test/resources/application.yaml
deleted file mode 100644
index aa7a91bac5..0000000000
--- a/spring-amqp-simple/src/test/resources/application.yaml
+++ /dev/null
@@ -1,5 +0,0 @@
-spring:
- rabbitmq:
- username: guest
- password: guest
- host: localhost
\ No newline at end of file
diff --git a/spring-amqp/README.md b/spring-amqp/README.md
index b0d16c9305..5e29011995 100644
--- a/spring-amqp/README.md
+++ b/spring-amqp/README.md
@@ -1,3 +1,4 @@
## Relevant articles:
-- [Messaging With Spring AMQP](http://www.baeldung.com/spring-amqp)
+- [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
index c021bd49ff..3e6789dcb6 100755
--- a/spring-amqp/pom.xml
+++ b/spring-amqp/pom.xml
@@ -3,37 +3,26 @@
4.0.0
com.baeldung
spring-amqp
- 0.1-SNAPSHOT
+ 1.0.0-SNAPSHOT
spring-amqp
- jar
Introduction to Spring-AMQP
+ parent-boot-2
com.baeldung
- parent-modules
- 1.0.0-SNAPSHOT
+ 0.0.1-SNAPSHOT
+ ../parent-boot-2
- org.springframework.amqp
- spring-rabbit
- ${spring-rabbit}
-
-
- commons-logging
- commons-logging
-
-
+ org.springframework.boot
+ spring-boot-starter-amqp
-
- spring-amqp
-
-
- 1.6.6.RELEASE
+ com.baeldung.springamqp.simple.HelloWorldMessageApp
diff --git a/spring-amqp/src/main/java/com/baeldung/springamqp/broadcast/BroadcastConfig.java b/spring-amqp/src/main/java/com/baeldung/springamqp/broadcast/BroadcastConfig.java
new file mode 100644
index 0000000000..12c5987cc4
--- /dev/null
+++ b/spring-amqp/src/main/java/com/baeldung/springamqp/broadcast/BroadcastConfig.java
@@ -0,0 +1,52 @@
+package com.baeldung.springamqp.broadcast;
+
+import org.springframework.amqp.core.*;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class BroadcastConfig {
+
+ private static final boolean NON_DURABLE = false;
+
+ public final static String FANOUT_QUEUE_1_NAME = "com.baeldung.spring-amqp-simple.fanout.queue1";
+ public final static String FANOUT_QUEUE_2_NAME = "com.baeldung.spring-amqp-simple.fanout.queue2";
+ public final static String FANOUT_EXCHANGE_NAME = "com.baeldung.spring-amqp-simple.fanout.exchange";
+
+ public final static String TOPIC_QUEUE_1_NAME = "com.baeldung.spring-amqp-simple.topic.queue1";
+ public final static String TOPIC_QUEUE_2_NAME = "com.baeldung.spring-amqp-simple.topic.queue2";
+ public final static String TOPIC_EXCHANGE_NAME = "com.baeldung.spring-amqp-simple.topic.exchange";
+ public static final String BINDING_PATTERN_IMPORTANT = "*.important.*";
+ public static final String BINDING_PATTERN_ERROR = "#.error";
+
+ @Bean
+ public Declarables topicBindings() {
+ Queue topicQueue1 = new Queue(TOPIC_QUEUE_1_NAME, NON_DURABLE);
+ Queue topicQueue2 = new Queue(TOPIC_QUEUE_2_NAME, NON_DURABLE);
+
+ TopicExchange topicExchange = new TopicExchange(TOPIC_EXCHANGE_NAME, NON_DURABLE, false);
+
+ return new Declarables(topicQueue1, topicQueue2, topicExchange, BindingBuilder
+ .bind(topicQueue1)
+ .to(topicExchange)
+ .with(BINDING_PATTERN_IMPORTANT), BindingBuilder
+ .bind(topicQueue2)
+ .to(topicExchange)
+ .with(BINDING_PATTERN_ERROR));
+ }
+
+ @Bean
+ public Declarables fanoutBindings() {
+ Queue fanoutQueue1 = new Queue(FANOUT_QUEUE_1_NAME, NON_DURABLE);
+ Queue fanoutQueue2 = new Queue(FANOUT_QUEUE_2_NAME, NON_DURABLE);
+
+ FanoutExchange fanoutExchange = new FanoutExchange(FANOUT_EXCHANGE_NAME, NON_DURABLE, false);
+
+ return new Declarables(fanoutQueue1, fanoutQueue2, fanoutExchange, BindingBuilder
+ .bind(fanoutQueue1)
+ .to(fanoutExchange), BindingBuilder
+ .bind(fanoutQueue2)
+ .to(fanoutExchange));
+ }
+
+}
diff --git a/spring-amqp/src/main/java/com/baeldung/springamqp/broadcast/BroadcastMessageApp.java b/spring-amqp/src/main/java/com/baeldung/springamqp/broadcast/BroadcastMessageApp.java
new file mode 100644
index 0000000000..64bf3ffb93
--- /dev/null
+++ b/spring-amqp/src/main/java/com/baeldung/springamqp/broadcast/BroadcastMessageApp.java
@@ -0,0 +1,59 @@
+package com.baeldung.springamqp.broadcast;
+
+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;
+
+import static com.baeldung.springamqp.broadcast.BroadcastConfig.*;
+
+/**
+ * Simple test application to send messages to rabbitMQ.
+ *
+ *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/consumer/Consumer.java b/spring-amqp/src/main/java/com/baeldung/springamqp/consumer/Consumer.java
deleted file mode 100644
index 42d7e88cbd..0000000000
--- a/spring-amqp/src/main/java/com/baeldung/springamqp/consumer/Consumer.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package com.baeldung.springamqp.consumer;
-
-public class Consumer {
- public void listen(String foo) {
- System.out.println(foo);
- }
-}
\ No newline at end of file
diff --git a/spring-amqp/src/main/java/com/baeldung/springamqp/producer/Producer.java b/spring-amqp/src/main/java/com/baeldung/springamqp/producer/Producer.java
deleted file mode 100644
index b4067ed795..0000000000
--- a/spring-amqp/src/main/java/com/baeldung/springamqp/producer/Producer.java
+++ /dev/null
@@ -1,17 +0,0 @@
-package com.baeldung.springamqp.producer;
-
-import org.springframework.amqp.core.AmqpTemplate;
-import org.springframework.amqp.rabbit.core.RabbitTemplate;
-import org.springframework.context.support.AbstractApplicationContext;
-import org.springframework.context.support.ClassPathXmlApplicationContext;
-
-public class Producer {
-
- public static void main(String[] args) throws InterruptedException {
- AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- AmqpTemplate template = ctx.getBean(RabbitTemplate.class);
- template.convertAndSend("Hello, world!");
- Thread.sleep(1000);
- ctx.destroy();
- }
-}
\ No newline at end of file
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);
+ }
+
+}
diff --git a/spring-amqp/src/main/resources/beans.xml b/spring-amqp/src/main/resources/beans.xml
deleted file mode 100644
index f6a966b0f6..0000000000
--- a/spring-amqp/src/main/resources/beans.xml
+++ /dev/null
@@ -1,35 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/spring-amqp/src/main/resources/logback.xml b/spring-amqp/src/main/resources/logback.xml
deleted file mode 100644
index 7d900d8ea8..0000000000
--- a/spring-amqp/src/main/resources/logback.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
- %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
-
-
-
-
-
-
-
-
\ No newline at end of file