From e71358a9dec1baab03c32323940facc5645f73c6 Mon Sep 17 00:00:00 2001 From: Justin Wilson Date: Wed, 22 Mar 2017 12:47:23 +0000 Subject: [PATCH] BAEL-503: initial commit of a simple Spring AMQL example application (#1467) --- pom.xml | 1 + spring-amqp-simple/pom.xml | 46 ++++++++++++++++++ .../springamqpsimple/MessageConsumer.java | 15 ++++++ .../springamqpsimple/MessageController.java | 26 ++++++++++ .../springamqpsimple/MessageProducer.java | 20 ++++++++ .../SpringAmqpApplication.java | 12 +++++ .../springamqpsimple/SpringAmqpConfig.java | 48 +++++++++++++++++++ .../src/main/resources/application.yaml | 4 ++ .../MessageControllerTest.java | 45 +++++++++++++++++ 9 files changed, 217 insertions(+) create mode 100644 spring-amqp-simple/pom.xml create mode 100644 spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageConsumer.java create mode 100644 spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageController.java create mode 100644 spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageProducer.java create mode 100644 spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/SpringAmqpApplication.java create mode 100644 spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/SpringAmqpConfig.java create mode 100644 spring-amqp-simple/src/main/resources/application.yaml create mode 100644 spring-amqp-simple/src/test/java/com/baeldung/springamqpsimple/MessageControllerTest.java diff --git a/pom.xml b/pom.xml index 1a1108cb99..15e0e3322e 100644 --- a/pom.xml +++ b/pom.xml @@ -115,6 +115,7 @@ spring-akka spring-amqp spring-all + spring-amqp-simple spring-apache-camel spring-batch spring-boot diff --git a/spring-amqp-simple/pom.xml b/spring-amqp-simple/pom.xml new file mode 100644 index 0000000000..38738d875f --- /dev/null +++ b/spring-amqp-simple/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.2.RELEASE + + + com.baeldung + spring-amqp-simple + 1.0.0-SNAPSHOT + Spring AMQP Simple App + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-amqp + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageConsumer.java b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageConsumer.java new file mode 100644 index 0000000000..b757dfebe8 --- /dev/null +++ b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageConsumer.java @@ -0,0 +1,15 @@ +package com.baeldung.springamqpsimple; + +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; + +@Component +public class MessageConsumer { + + private static final Logger logger = LogManager.getLogger(MessageConsumer.class); + + public void receiveMessage(String message) { + logger.info("Received Message: " + message); + } +} diff --git a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageController.java b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageController.java new file mode 100644 index 0000000000..deef22c4d6 --- /dev/null +++ b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageController.java @@ -0,0 +1,26 @@ +package com.baeldung.springamqpsimple; + +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 MessageController { + + private final MessageProducer messageProducer; + + @Autowired + public MessageController(MessageProducer messageProducer) { + this.messageProducer = messageProducer; + } + + @RequestMapping(value="/messages", method= RequestMethod.POST) + @ResponseStatus(value= HttpStatus.CREATED) + public void sendMessage(@RequestBody String message) { + messageProducer.sendMessage(message); + } +} diff --git a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageProducer.java b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageProducer.java new file mode 100644 index 0000000000..225f37bdd0 --- /dev/null +++ b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/MessageProducer.java @@ -0,0 +1,20 @@ +package com.baeldung.springamqpsimple; + +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class MessageProducer { + + private final RabbitTemplate rabbitTemplate; + + @Autowired + public MessageProducer(RabbitTemplate rabbitTemplate) { + this.rabbitTemplate = rabbitTemplate; + } + + public void sendMessage(String message) { + rabbitTemplate.convertAndSend(SpringAmqpConfig.queueName, message); + } +} diff --git a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/SpringAmqpApplication.java b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/SpringAmqpApplication.java new file mode 100644 index 0000000000..b84a49a230 --- /dev/null +++ b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/SpringAmqpApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.springamqpsimple; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringAmqpApplication { + + public static void main(String[] args) throws InterruptedException { + SpringApplication.run(SpringAmqpApplication.class, args); + } +} diff --git a/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/SpringAmqpConfig.java b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/SpringAmqpConfig.java new file mode 100644 index 0000000000..78d79dd47a --- /dev/null +++ b/spring-amqp-simple/src/main/java/com/baeldung/springamqpsimple/SpringAmqpConfig.java @@ -0,0 +1,48 @@ +package com.baeldung.springamqpsimple; + +import org.springframework.amqp.core.*; +import org.springframework.amqp.rabbit.connection.ConnectionFactory; +import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; +import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; + +@Configuration +@Profile("!test") +public class SpringAmqpConfig { + + public final static String queueName = "com.baeldung.spring-amqp-simple.queue"; + public final static String exchangeName = "com.baeldung.spring-amqp-simple.exchange"; + + @Bean + Queue queue() { + return new Queue(queueName, false); + } + + @Bean + Exchange exchange() { + return new DirectExchange(exchangeName); + } + + @Bean + Binding binding(Queue queue, DirectExchange exchange) { + return BindingBuilder.bind(queue).to(exchange).with(queueName); + } + + @Bean + SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, + MessageListenerAdapter listenerAdapter) { + SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(); + container.setConnectionFactory(connectionFactory); + container.setQueueNames(queueName); + container.setMessageListener(listenerAdapter); + return container; + } + + @Bean + MessageListenerAdapter listenerAdapter(MessageConsumer messageReceiver) { + return new MessageListenerAdapter(messageReceiver, "receiveMessage"); + } + +} diff --git a/spring-amqp-simple/src/main/resources/application.yaml b/spring-amqp-simple/src/main/resources/application.yaml new file mode 100644 index 0000000000..4aca1bb783 --- /dev/null +++ b/spring-amqp-simple/src/main/resources/application.yaml @@ -0,0 +1,4 @@ +spring: + rabbitmq: + username: baeldung + password: baeldung \ No newline at end of file diff --git a/spring-amqp-simple/src/test/java/com/baeldung/springamqpsimple/MessageControllerTest.java b/spring-amqp-simple/src/test/java/com/baeldung/springamqpsimple/MessageControllerTest.java new file mode 100644 index 0000000000..c62c86290a --- /dev/null +++ b/spring-amqp-simple/src/test/java/com/baeldung/springamqpsimple/MessageControllerTest.java @@ -0,0 +1,45 @@ +package com.baeldung.springamqpsimple; + + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; + +@RunWith(SpringRunner.class) +@ActiveProfiles("test") +@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT) +public class MessageControllerTest { + + @Autowired + private TestRestTemplate restTemplate; + + @MockBean + private RabbitTemplate rabbitTemplate; + + @Test + public void whenPostingMessage_thenMessageIsCreated() { + final String message = "Hello World!"; + ResponseEntity responseEntity = restTemplate.postForEntity("/messages", message, Void.class); + + assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode()); + } + + @Test + public void whenPostingMessage_thenMessageIsSentToBroker() { + final String message = "Hello World!"; + restTemplate.postForEntity("/messages", message, Void.class); + + verify(rabbitTemplate).convertAndSend(SpringAmqpConfig.queueName, message); + } +} \ No newline at end of file