BAEL-503: initial commit of a simple Spring AMQL example application (#1467)

This commit is contained in:
Justin Wilson 2017-03-22 12:47:23 +00:00 committed by maibin
parent d66703b5d9
commit e71358a9de
9 changed files with 217 additions and 0 deletions

View File

@ -115,6 +115,7 @@
<module>spring-akka</module>
<module>spring-amqp</module>
<module>spring-all</module>
<module>spring-amqp-simple</module>
<module>spring-apache-camel</module>
<module>spring-batch</module>
<module>spring-boot</module>

View File

@ -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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<groupId>com.baeldung</groupId>
<artifactId>spring-amqp-simple</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Spring AMQP Simple App</name>
<properties>
<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-web</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>

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,4 @@
spring:
rabbitmq:
username: baeldung
password: baeldung

View File

@ -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<Void> 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);
}
}