├── .gitignore ├── README.md ├── docker-compose.yml ├── img └── cover.png ├── pom.xml └── src ├── main ├── java │ └── io │ │ └── tpd │ │ └── rabbitmqexample │ │ ├── PracticalTipListener.java │ │ ├── PracticalTipMessage.java │ │ ├── PracticalTipSender.java │ │ ├── RabbitmqExampleApplication.java │ │ └── SimplifiedPracticalTipMessage.java └── resources │ └── application.properties └── test └── java └── io └── tpd └── rabbitmqexample └── RabbitmqExampleApplicationTests.java /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | /nbproject/private/ 21 | /build/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Boot 2 and RabbitMQ JSON configuration 2 | 3 | Simple Spring Boot 2 app that demonstrates how to use JSON serialization with RabbitMQ. 4 | 5 | This is the source code used in the video Sending and Receiving messages using Spring Boot AMQP and RabbitMQ, available on [Youtube](https://www.youtube.com/watch?v=yuApTF_HeWM). 6 | 7 | [![Sending and Receiving messages using Spring Boot AMQP and RabbitMQ](img/cover.png)](https://www.youtube.com/watch?v=yuApTF_HeWM) 8 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | 3 | services: 4 | rabbitmq: 5 | image: rabbitmq:3.7-alpine 6 | hostname: rabbitmq 7 | ports: 8 | - "5672:5672" 9 | -------------------------------------------------------------------------------- /img/cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepracticaldeveloper/rabbitmq-json-basic-example/ebcde137b5026cc1f857799963871c8fecdf1e2d/img/cover.png -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | io.tpd 7 | rabbitmq-example 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | rabbitmq-example 12 | RabbitMQ example project created by ThePracticalDeveloper.com 13 | https://thepracticaldeveloper.com 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 2.1.0.RELEASE 19 | 20 | 21 | 22 | 23 | UTF-8 24 | UTF-8 25 | 1.8 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-amqp 32 | 33 | 34 | com.fasterxml.jackson.core 35 | jackson-core 36 | 2.9.6 37 | 38 | 39 | com.fasterxml.jackson.core 40 | jackson-annotations 41 | 2.9.6 42 | 43 | 44 | com.fasterxml.jackson.core 45 | jackson-databind 46 | 2.9.6 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-test 51 | test 52 | 53 | 54 | 55 | 56 | 57 | 58 | org.springframework.boot 59 | spring-boot-maven-plugin 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/rabbitmqexample/PracticalTipListener.java: -------------------------------------------------------------------------------- 1 | package io.tpd.rabbitmqexample; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.messaging.Message; 6 | import org.springframework.amqp.rabbit.annotation.RabbitListener; 7 | import org.springframework.stereotype.Service; 8 | 9 | @Service 10 | public class PracticalTipListener { 11 | 12 | private static final Logger log = LoggerFactory.getLogger(PracticalTipListener.class); 13 | 14 | @RabbitListener(queues = RabbitmqExampleApplication.DEFAULT_PARSING_QUEUE) 15 | public void consumeDefaultMessage(final Message message) { 16 | log.info("Received message, tip is: {}", message.toString()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/rabbitmqexample/PracticalTipMessage.java: -------------------------------------------------------------------------------- 1 | package io.tpd.rabbitmqexample; 2 | 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | 5 | public class PracticalTipMessage { 6 | 7 | private final String text; 8 | private final int priority; 9 | private final boolean secret; 10 | 11 | public PracticalTipMessage(@JsonProperty("text") final String text, 12 | @JsonProperty("priority") final int priority, 13 | @JsonProperty("secret") final boolean secret) { 14 | this.text = text; 15 | this.priority = priority; 16 | this.secret = secret; 17 | } 18 | 19 | public String getText() { 20 | return text; 21 | } 22 | 23 | public int getPriority() { 24 | return priority; 25 | } 26 | 27 | public boolean isSecret() { 28 | return secret; 29 | } 30 | 31 | @Override 32 | public String toString() { 33 | return "PracticalTipMessage{" + 34 | "text='" + text + '\'' + 35 | ", priority=" + priority + 36 | ", secret=" + secret + 37 | '}'; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/rabbitmqexample/PracticalTipSender.java: -------------------------------------------------------------------------------- 1 | package io.tpd.rabbitmqexample; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 6 | import org.springframework.scheduling.annotation.Scheduled; 7 | import org.springframework.stereotype.Service; 8 | 9 | @Service 10 | public class PracticalTipSender { 11 | 12 | private static final Logger log = LoggerFactory.getLogger(PracticalTipSender.class); 13 | 14 | private final RabbitTemplate rabbitTemplate; 15 | 16 | public PracticalTipSender(final RabbitTemplate rabbitTemplate) { 17 | this.rabbitTemplate = rabbitTemplate; 18 | } 19 | 20 | @Scheduled(fixedDelay = 3000L) 21 | public void sendPracticalTip() { 22 | PracticalTipMessage tip = new PracticalTipMessage("Always use Immutable classes in Java", 1, false); 23 | rabbitTemplate.convertAndSend(RabbitmqExampleApplication.EXCHANGE_NAME, RabbitmqExampleApplication.ROUTING_KEY, tip); 24 | log.info("Practical Tip sent"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/rabbitmqexample/RabbitmqExampleApplication.java: -------------------------------------------------------------------------------- 1 | package io.tpd.rabbitmqexample; 2 | 3 | import com.fasterxml.jackson.databind.ObjectMapper; 4 | import org.springframework.amqp.core.Binding; 5 | import org.springframework.amqp.core.BindingBuilder; 6 | import org.springframework.amqp.core.Queue; 7 | import org.springframework.amqp.core.TopicExchange; 8 | import org.springframework.amqp.rabbit.connection.ConnectionFactory; 9 | import org.springframework.amqp.rabbit.core.RabbitTemplate; 10 | import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; 11 | import org.springframework.boot.SpringApplication; 12 | import org.springframework.boot.autoconfigure.SpringBootApplication; 13 | import org.springframework.context.annotation.Bean; 14 | import org.springframework.scheduling.annotation.EnableScheduling; 15 | 16 | @SpringBootApplication 17 | @EnableScheduling 18 | public class RabbitmqExampleApplication { 19 | 20 | public static final String EXCHANGE_NAME = "tips_tx"; 21 | public static final String DEFAULT_PARSING_QUEUE = "default_parser_q"; 22 | public static final String ROUTING_KEY = "tips"; 23 | 24 | public static void main(String[] args) { 25 | SpringApplication.run(RabbitmqExampleApplication.class, args); 26 | } 27 | 28 | @Bean 29 | public TopicExchange tipsExchange() { 30 | return new TopicExchange(EXCHANGE_NAME); 31 | } 32 | 33 | @Bean 34 | public Queue defaultParsingQueue() { 35 | return new Queue(DEFAULT_PARSING_QUEUE); 36 | } 37 | 38 | @Bean 39 | public Binding queueToExchangeBinding() { 40 | return BindingBuilder.bind(defaultParsingQueue()).to(tipsExchange()).with(ROUTING_KEY); 41 | } 42 | 43 | @Bean 44 | public Jackson2JsonMessageConverter messageConverter() { 45 | ObjectMapper mapper = new ObjectMapper(); 46 | return new Jackson2JsonMessageConverter(mapper); 47 | } 48 | 49 | @Bean 50 | public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) { 51 | RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory); 52 | rabbitTemplate.setMessageConverter(messageConverter()); 53 | return rabbitTemplate; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/io/tpd/rabbitmqexample/SimplifiedPracticalTipMessage.java: -------------------------------------------------------------------------------- 1 | package io.tpd.rabbitmqexample; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 4 | import com.fasterxml.jackson.annotation.JsonProperty; 5 | 6 | @JsonIgnoreProperties(ignoreUnknown = true) 7 | public class SimplifiedPracticalTipMessage { 8 | 9 | private final String text; 10 | 11 | public SimplifiedPracticalTipMessage(@JsonProperty("text") final String text) { 12 | this.text = text; 13 | } 14 | 15 | public String getText() { 16 | return text; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return "SimplifiedPracticalTipMessage{" + 22 | "text='" + text + '\'' + 23 | '}'; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thepracticaldeveloper/rabbitmq-json-basic-example/ebcde137b5026cc1f857799963871c8fecdf1e2d/src/main/resources/application.properties -------------------------------------------------------------------------------- /src/test/java/io/tpd/rabbitmqexample/RabbitmqExampleApplicationTests.java: -------------------------------------------------------------------------------- 1 | package io.tpd.rabbitmqexample; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class RabbitmqExampleApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------