├── .gitignore
├── README.md
├── pom.xml
├── tutorial-1
├── pom.xml
├── tutorial-1-consumer
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── consumer
│ │ ├── AmqpConfig.java
│ │ ├── ConsumerApplication.java
│ │ └── receiver
│ │ └── SimpleReceiver.java
└── tutorial-1-producer
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── consumer
│ ├── ProducerApplication.java
│ └── sender
│ └── SimpleSender.java
├── tutorial-2
├── pom.xml
├── tutorial-2-consumer
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── consumer
│ │ ├── AmqpConfig.java
│ │ ├── ConsumerApplication.java
│ │ └── receiver
│ │ └── SimpleReceiver.java
├── tutorial-2-domain
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── domain
│ │ └── Message.java
└── tutorial-2-producer
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── producer
│ ├── ProducerApplication.java
│ └── sender
│ └── SimpleSender.java
├── tutorial-3
├── pom.xml
├── tutorial-3-consumer
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── consumer
│ │ ├── AmqpConfig.java
│ │ ├── ConsumerApplication.java
│ │ └── receiver
│ │ └── SimpleReceiver.java
├── tutorial-3-domain
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── domain
│ │ └── Student.java
└── tutorial-3-producer
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── producer
│ ├── ProducerApplication.java
│ └── sender
│ └── SimpleSender.java
├── tutorial-4
├── pom.xml
├── tutorial-4-consumer
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── consumer
│ │ ├── AmqpConfig.java
│ │ ├── ConsumerApplication.java
│ │ └── receiver
│ │ └── SimpleReceiver.java
├── tutorial-4-domain
│ ├── pom.xml
│ └── src
│ │ └── main
│ │ └── java
│ │ └── domain
│ │ └── Student.java
└── tutorial-4-producer
│ ├── pom.xml
│ └── src
│ └── main
│ └── java
│ └── producer
│ ├── ProducerApplication.java
│ └── sender
│ └── SimpleSender.java
└── tutorial-5
├── pom.xml
├── tutorial-5-consumer
├── pom.xml
└── src
│ └── main
│ └── java
│ └── consumer
│ ├── AmqpConfig.java
│ ├── ConsumerApplication.java
│ └── receiver
│ └── SimpleReceiver.java
├── tutorial-5-domain
├── pom.xml
└── src
│ └── main
│ └── java
│ └── domain
│ └── Student.java
└── tutorial-5-producer
├── pom.xml
└── src
└── main
└── java
└── producer
├── ProducerApplication.java
└── sender
└── SimpleSender.java
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | *.iml
3 | .idea
4 | .settings
5 | .classpath
6 | .project
7 | .DS_Store
8 | *~
9 | /.git
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # spring-boot-amqp-tutorial
2 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | spring-boot-amqp-tutorial
9 | pom
10 | 1.0
11 |
12 | tutorial-1
13 | tutorial-2
14 | tutorial-3
15 | tutorial-4
16 | tutorial-5
17 |
18 |
19 |
20 | 1.8
21 | @
22 | UTF-8
23 | UTF-8
24 | ${java.version}
25 | ${java.version}
26 |
27 |
28 |
29 |
30 |
31 | io.spring.platform
32 | platform-bom
33 | 1.1.3.RELEASE
34 | pom
35 | import
36 |
37 |
38 |
39 |
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-amqp
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/tutorial-1/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | tutorial-1
8 | 1.0
9 | pom
10 |
11 |
12 | cero.ninja.tutorial.springboot.amqp
13 | spring-boot-amqp-tutorial
14 | 1.0
15 |
16 |
17 |
18 | tutorial-1-consumer
19 | tutorial-1-producer
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-1/tutorial-1-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-1
9 | 1.0
10 |
11 |
12 | tutorial-1-consumer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-1-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-1/tutorial-1-consumer/src/main/java/consumer/AmqpConfig.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.amqp.core.Queue;
4 | import org.springframework.amqp.rabbit.annotation.EnableRabbit;
5 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
6 | import org.springframework.amqp.rabbit.core.RabbitAdmin;
7 | import org.springframework.context.annotation.Bean;
8 | import org.springframework.context.annotation.Configuration;
9 |
10 | @EnableRabbit
11 | @Configuration
12 | public class AmqpConfig {
13 | static final String QUEUE_NAME = "tutorial-1";
14 |
15 | @Bean
16 | RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
17 | return new RabbitAdmin(connectionFactory);
18 | }
19 |
20 | @Bean
21 | Queue queue(RabbitAdmin rabbitAdmin) {
22 | Queue queue = new Queue(QUEUE_NAME, false);
23 | rabbitAdmin.declareQueue(queue);
24 | return queue;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/tutorial-1/tutorial-1-consumer/src/main/java/consumer/ConsumerApplication.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ConsumerApplication {
8 | public static void main(String[] args) {
9 | SpringApplication.run(ConsumerApplication.class, args);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/tutorial-1/tutorial-1-consumer/src/main/java/consumer/receiver/SimpleReceiver.java:
--------------------------------------------------------------------------------
1 | package consumer.receiver;
2 |
3 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
4 | import org.springframework.stereotype.Component;
5 |
6 | @Component
7 | public class SimpleReceiver {
8 | @RabbitListener(queues = "tutorial-1")
9 | public void receive(String message) {
10 | System.out.println(message);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/tutorial-1/tutorial-1-producer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-1
9 | 1.0
10 |
11 |
12 | tutorial-1-producer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-1-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-1/tutorial-1-producer/src/main/java/consumer/ProducerApplication.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import consumer.sender.SimpleSender;
4 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
5 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
6 | import org.springframework.boot.SpringApplication;
7 | import org.springframework.boot.autoconfigure.SpringBootApplication;
8 | import org.springframework.context.ConfigurableApplicationContext;
9 | import org.springframework.context.annotation.Bean;
10 |
11 | @SpringBootApplication
12 | public class ProducerApplication {
13 | public static void main(String[] args) {
14 | ConfigurableApplicationContext context = SpringApplication.run(ProducerApplication.class, args);
15 | SimpleSender sender = context.getBean(SimpleSender.class);
16 | sender.send();
17 | context.close();
18 | }
19 |
20 | @Bean
21 | RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
22 | return new RabbitTemplate(connectionFactory);
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/tutorial-1/tutorial-1-producer/src/main/java/consumer/sender/SimpleSender.java:
--------------------------------------------------------------------------------
1 | package consumer.sender;
2 |
3 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
4 | import org.springframework.beans.factory.annotation.Autowired;
5 | import org.springframework.stereotype.Component;
6 |
7 | @Component
8 | public class SimpleSender {
9 | @Autowired
10 | RabbitTemplate rabbitTemplate;
11 |
12 | public void send() {
13 | rabbitTemplate.convertAndSend("tutorial-1", "Hello World!");
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/tutorial-2/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | tutorial-2
8 | 1.0
9 | pom
10 |
11 |
12 | cero.ninja.tutorial.springboot.amqp
13 | spring-boot-amqp-tutorial
14 | 1.0
15 |
16 |
17 |
18 | tutorial-2-consumer
19 | tutorial-2-domain
20 | tutorial-2-producer
21 |
22 |
23 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-2
9 | 1.0
10 |
11 |
12 | tutorial-2-consumer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-2-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-consumer/src/main/java/consumer/AmqpConfig.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.amqp.core.Queue;
4 | import org.springframework.amqp.rabbit.annotation.EnableRabbit;
5 | import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
6 | import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
7 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
8 | import org.springframework.amqp.rabbit.core.RabbitAdmin;
9 | import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
10 | import org.springframework.context.annotation.Bean;
11 | import org.springframework.context.annotation.Configuration;
12 | import org.springframework.messaging.converter.MappingJackson2MessageConverter;
13 | import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
14 |
15 | @EnableRabbit
16 | @Configuration
17 | public class AmqpConfig implements RabbitListenerConfigurer {
18 | static final String QUEUE_NAME = "tutorial-2";
19 |
20 | @Bean
21 | RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
22 | return new RabbitAdmin(connectionFactory);
23 | }
24 |
25 | @Bean
26 | Queue queue(RabbitAdmin rabbitAdmin) {
27 | Queue queue = new Queue(QUEUE_NAME, true);
28 | rabbitAdmin.declareQueue(queue);
29 | return queue;
30 | }
31 |
32 | @Override
33 | public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
34 | registrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
35 | }
36 |
37 | @Bean
38 | public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
39 | DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
40 | factory.setMessageConverter(new MappingJackson2MessageConverter());
41 | return factory;
42 | }
43 |
44 | @Bean
45 | public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
46 | SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
47 | factory.setConnectionFactory(connectionFactory);
48 | // factory.setPrefetchCount(5);
49 | // factory.setAcknowledgeMode(AcknowledgeMode.NONE);
50 | return factory;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-consumer/src/main/java/consumer/ConsumerApplication.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ConsumerApplication {
8 | public static void main(String[] args) {
9 | SpringApplication.run(ConsumerApplication.class, args);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-consumer/src/main/java/consumer/receiver/SimpleReceiver.java:
--------------------------------------------------------------------------------
1 | package consumer.receiver;
2 |
3 | import domain.Message;
4 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
5 | import org.springframework.stereotype.Component;
6 |
7 | @Component
8 | public class SimpleReceiver {
9 | @RabbitListener(queues = "tutorial-2")
10 | public void receive(Message message) throws InterruptedException {
11 | System.out.println(message.id + ":" + message.text);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-domain/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-2
9 | 1.0
10 |
11 |
12 | tutorial-2-domain
13 |
14 |
15 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-domain/src/main/java/domain/Message.java:
--------------------------------------------------------------------------------
1 | package domain;
2 |
3 | public class Message {
4 | public Integer id;
5 | public String text;
6 | }
7 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-producer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-2
9 | 1.0
10 |
11 |
12 | tutorial-2-producer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-2-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-producer/src/main/java/producer/ProducerApplication.java:
--------------------------------------------------------------------------------
1 | package producer;
2 |
3 | import producer.sender.SimpleSender;
4 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
5 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
6 | import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
7 | import org.springframework.boot.SpringApplication;
8 | import org.springframework.boot.autoconfigure.SpringBootApplication;
9 | import org.springframework.context.ConfigurableApplicationContext;
10 | import org.springframework.context.annotation.Bean;
11 |
12 | @SpringBootApplication
13 | public class ProducerApplication {
14 | public static void main(String[] args) {
15 | ConfigurableApplicationContext context = SpringApplication.run(ProducerApplication.class, args);
16 | SimpleSender sender = context.getBean(SimpleSender.class);
17 | sender.send();
18 | context.close();
19 | }
20 |
21 | @Bean
22 | RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
23 | RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
24 | rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
25 | return rabbitTemplate;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/tutorial-2/tutorial-2-producer/src/main/java/producer/sender/SimpleSender.java:
--------------------------------------------------------------------------------
1 | package producer.sender;
2 |
3 | import domain.Message;
4 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Component;
7 |
8 | import java.util.stream.IntStream;
9 |
10 | @Component
11 | public class SimpleSender {
12 | @Autowired
13 | RabbitTemplate rabbitTemplate;
14 |
15 | public void send() {
16 | Message message = new Message();
17 | IntStream.range(0, 10)
18 | .forEach(i -> {
19 | message.id = i;
20 | message.text = "Hello - " + i;
21 |
22 | rabbitTemplate.convertAndSend("tutorial-2", message);
23 | });
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/tutorial-3/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | tutorial-3
8 | 1.0
9 | pom
10 |
11 |
12 | cero.ninja.tutorial.springboot.amqp
13 | spring-boot-amqp-tutorial
14 | 1.0
15 |
16 |
17 |
18 | tutorial-3-consumer
19 | tutorial-3-domain
20 | tutorial-3-producer
21 |
22 |
23 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-3
9 | 1.0
10 |
11 |
12 | tutorial-3-consumer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-3-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-consumer/src/main/java/consumer/AmqpConfig.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.amqp.core.BindingBuilder;
4 | import org.springframework.amqp.core.FanoutExchange;
5 | import org.springframework.amqp.core.Queue;
6 | import org.springframework.amqp.rabbit.annotation.EnableRabbit;
7 | import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
8 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
9 | import org.springframework.amqp.rabbit.core.RabbitAdmin;
10 | import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
11 | import org.springframework.context.annotation.Bean;
12 | import org.springframework.context.annotation.Configuration;
13 | import org.springframework.messaging.converter.MappingJackson2MessageConverter;
14 | import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
15 |
16 | @EnableRabbit
17 | @Configuration
18 | public class AmqpConfig implements RabbitListenerConfigurer {
19 | static final String EXCHANGE_NAME = "tutorial-3-exchange";
20 | static final String QUEUE1_NAME = "tutorial-3-1";
21 | static final String QUEUE2_NAME = "tutorial-3-2";
22 |
23 | @Bean
24 | RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
25 | return new RabbitAdmin(connectionFactory);
26 | }
27 |
28 | @Bean
29 | FanoutExchange exchange(RabbitAdmin rabbitAdmin) {
30 | FanoutExchange exchange = new FanoutExchange(EXCHANGE_NAME);
31 | rabbitAdmin.declareExchange(exchange);
32 |
33 | Queue queue1 = new Queue(QUEUE1_NAME);
34 | Queue queue2 = new Queue(QUEUE2_NAME);
35 | rabbitAdmin.declareQueue(queue1);
36 | rabbitAdmin.declareQueue(queue2);
37 |
38 | rabbitAdmin.declareBinding(BindingBuilder.bind(queue1)
39 | .to(exchange));
40 | rabbitAdmin.declareBinding(BindingBuilder.bind(queue2)
41 | .to(exchange));
42 |
43 | return exchange;
44 | }
45 |
46 | @Override
47 | public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
48 | registrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
49 | }
50 |
51 | @Bean
52 | public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
53 | DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
54 | factory.setMessageConverter(new MappingJackson2MessageConverter());
55 | return factory;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-consumer/src/main/java/consumer/ConsumerApplication.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ConsumerApplication {
8 | public static void main(String[] args) {
9 | SpringApplication.run(ConsumerApplication.class, args);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-consumer/src/main/java/consumer/receiver/SimpleReceiver.java:
--------------------------------------------------------------------------------
1 | package consumer.receiver;
2 |
3 | import domain.Student;
4 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
5 | import org.springframework.stereotype.Component;
6 |
7 | @Component
8 | public class SimpleReceiver {
9 | @RabbitListener(queues = "tutorial-3-1")
10 | public void receive1(Student student) throws InterruptedException {
11 | System.out.println("3-1:" + student.id);
12 | }
13 |
14 | @RabbitListener(queues = "tutorial-3-2")
15 | public void receive2(Student student) throws InterruptedException {
16 | System.out.println("3-2:" + student.id);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-domain/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-3
9 | 1.0
10 |
11 |
12 | tutorial-3-domain
13 |
14 |
15 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-domain/src/main/java/domain/Student.java:
--------------------------------------------------------------------------------
1 | package domain;
2 |
3 | public class Student {
4 | public Integer id;
5 | public String name;
6 | public Integer score;
7 | }
8 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-producer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-3
9 | 1.0
10 |
11 |
12 | tutorial-3-producer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-3-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-producer/src/main/java/producer/ProducerApplication.java:
--------------------------------------------------------------------------------
1 | package producer;
2 |
3 | import org.springframework.amqp.core.Exchange;
4 | import org.springframework.amqp.core.FanoutExchange;
5 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
6 | import org.springframework.amqp.rabbit.core.RabbitAdmin;
7 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
8 | import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
9 | import org.springframework.boot.SpringApplication;
10 | import org.springframework.boot.autoconfigure.SpringBootApplication;
11 | import org.springframework.context.ConfigurableApplicationContext;
12 | import org.springframework.context.annotation.Bean;
13 | import producer.sender.SimpleSender;
14 |
15 | import java.io.IOException;
16 |
17 | @SpringBootApplication
18 | public class ProducerApplication {
19 | static final String EXCHANGE_NAME = "tutorial-3-exchange";
20 |
21 | public static void main(String[] args) {
22 | ConfigurableApplicationContext context = SpringApplication.run(ProducerApplication.class, args);
23 | SimpleSender sender = context.getBean(SimpleSender.class);
24 | sender.send();
25 | context.close();
26 | }
27 |
28 | @Bean
29 | Exchange exchange(ConnectionFactory connectionFactory) {
30 | FanoutExchange exchange = new FanoutExchange(EXCHANGE_NAME);
31 | new RabbitAdmin(connectionFactory).declareExchange(exchange);
32 | return exchange;
33 | }
34 |
35 | @Bean
36 | RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) throws IOException {
37 | RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
38 | rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
39 | return rabbitTemplate;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/tutorial-3/tutorial-3-producer/src/main/java/producer/sender/SimpleSender.java:
--------------------------------------------------------------------------------
1 | package producer.sender;
2 |
3 | import domain.Student;
4 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Component;
7 |
8 | import java.util.stream.IntStream;
9 |
10 | @Component
11 | public class SimpleSender {
12 | @Autowired
13 | RabbitTemplate rabbitTemplate;
14 |
15 | public void send() {
16 | Student student = new Student();
17 | IntStream.range(0, 10)
18 | .forEach(i -> {
19 | student.id = i;
20 | student.name = "TEST" + i;
21 | student.score = 100 - i;
22 |
23 | rabbitTemplate.convertAndSend("tutorial-3-exchange", "", student);
24 | });
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/tutorial-4/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | tutorial-4
8 | 1.0
9 | pom
10 |
11 |
12 | cero.ninja.tutorial.springboot.amqp
13 | spring-boot-amqp-tutorial
14 | 1.0
15 |
16 |
17 |
18 | tutorial-4-consumer
19 | tutorial-4-domain
20 | tutorial-4-producer
21 |
22 |
23 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-4
9 | 1.0
10 |
11 |
12 | tutorial-4-consumer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-4-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-consumer/src/main/java/consumer/AmqpConfig.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.amqp.core.BindingBuilder;
4 | import org.springframework.amqp.core.DirectExchange;
5 | import org.springframework.amqp.core.Queue;
6 | import org.springframework.amqp.rabbit.annotation.EnableRabbit;
7 | import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
8 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
9 | import org.springframework.amqp.rabbit.core.RabbitAdmin;
10 | import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
11 | import org.springframework.context.annotation.Bean;
12 | import org.springframework.context.annotation.Configuration;
13 | import org.springframework.messaging.converter.MappingJackson2MessageConverter;
14 | import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
15 |
16 | @EnableRabbit
17 | @Configuration
18 | public class AmqpConfig implements RabbitListenerConfigurer {
19 | static final String EXCHANGE_NAME = "tutorial-4-exchange";
20 | static final String QUEUE1_NAME = "tutorial-4-1";
21 | static final String QUEUE2_NAME = "tutorial-4-2";
22 |
23 | @Bean
24 | RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
25 | return new RabbitAdmin(connectionFactory);
26 | }
27 |
28 | @Bean
29 | DirectExchange exchange(RabbitAdmin rabbitAdmin) {
30 | DirectExchange exchange = new DirectExchange(EXCHANGE_NAME);
31 | rabbitAdmin.declareExchange(exchange);
32 |
33 | Queue queue1 = new Queue(QUEUE1_NAME);
34 | Queue queue2 = new Queue(QUEUE2_NAME);
35 | rabbitAdmin.declareQueue(queue1);
36 | rabbitAdmin.declareQueue(queue2);
37 |
38 | rabbitAdmin.declareBinding(BindingBuilder.bind(queue1)
39 | .to(exchange).with("mod0"));
40 | rabbitAdmin.declareBinding(BindingBuilder.bind(queue2)
41 | .to(exchange).with("mod1"));
42 | rabbitAdmin.declareBinding(BindingBuilder.bind(queue2)
43 | .to(exchange).with("mod2"));
44 |
45 | return exchange;
46 | }
47 |
48 | @Override
49 | public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
50 | registrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
51 | }
52 |
53 | @Bean
54 | public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
55 | DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
56 | factory.setMessageConverter(new MappingJackson2MessageConverter());
57 | return factory;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-consumer/src/main/java/consumer/ConsumerApplication.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ConsumerApplication {
8 | public static void main(String[] args) {
9 | SpringApplication.run(ConsumerApplication.class, args);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-consumer/src/main/java/consumer/receiver/SimpleReceiver.java:
--------------------------------------------------------------------------------
1 | package consumer.receiver;
2 |
3 | import domain.Student;
4 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
5 | import org.springframework.stereotype.Component;
6 |
7 | @Component
8 | public class SimpleReceiver {
9 | @RabbitListener(queues = "tutorial-4-1")
10 | public void receive1(Student student) throws InterruptedException {
11 | System.out.println("4-1:" + student.id + " - " + student.score);
12 | }
13 |
14 | @RabbitListener(queues = "tutorial-4-2")
15 | public void receive2(Student student) throws InterruptedException {
16 | System.out.println("4-2:" + student.id + " - " + student.score);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-domain/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-4
9 | 1.0
10 |
11 |
12 | tutorial-4-domain
13 |
14 |
15 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-domain/src/main/java/domain/Student.java:
--------------------------------------------------------------------------------
1 | package domain;
2 |
3 | public class Student {
4 | public Integer id;
5 | public String name;
6 | public Integer score;
7 | }
8 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-producer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-4
9 | 1.0
10 |
11 |
12 | tutorial-4-producer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-4-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-producer/src/main/java/producer/ProducerApplication.java:
--------------------------------------------------------------------------------
1 | package producer;
2 |
3 | import org.springframework.amqp.core.DirectExchange;
4 | import org.springframework.amqp.core.Exchange;
5 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
6 | import org.springframework.amqp.rabbit.core.RabbitAdmin;
7 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
8 | import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
9 | import org.springframework.boot.SpringApplication;
10 | import org.springframework.boot.autoconfigure.SpringBootApplication;
11 | import org.springframework.context.ConfigurableApplicationContext;
12 | import org.springframework.context.annotation.Bean;
13 | import producer.sender.SimpleSender;
14 |
15 | import java.io.IOException;
16 |
17 | @SpringBootApplication
18 | public class ProducerApplication {
19 | static final String EXCHANGE_NAME = "tutorial-4-exchange";
20 |
21 | public static void main(String[] args) {
22 | ConfigurableApplicationContext context = SpringApplication.run(ProducerApplication.class, args);
23 | SimpleSender sender = context.getBean(SimpleSender.class);
24 | sender.send();
25 | context.close();
26 | }
27 |
28 | @Bean
29 | Exchange exchange(ConnectionFactory connectionFactory) {
30 | DirectExchange exchange = new DirectExchange(EXCHANGE_NAME);
31 | new RabbitAdmin(connectionFactory).declareExchange(exchange);
32 | return exchange;
33 | }
34 |
35 | @Bean
36 | RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) throws IOException {
37 | RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
38 | rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
39 | return rabbitTemplate;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/tutorial-4/tutorial-4-producer/src/main/java/producer/sender/SimpleSender.java:
--------------------------------------------------------------------------------
1 | package producer.sender;
2 |
3 | import domain.Student;
4 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Component;
7 |
8 | import java.util.stream.IntStream;
9 |
10 | @Component
11 | public class SimpleSender {
12 | @Autowired
13 | RabbitTemplate rabbitTemplate;
14 |
15 | public void send() {
16 | Student student = new Student();
17 | IntStream.range(0, 10)
18 | .forEach(i -> {
19 | student.id = i;
20 | student.name = "TEST" + i;
21 | student.score = 100 - i;
22 |
23 | String key = "mod" + i % 3;
24 | rabbitTemplate.convertAndSend("tutorial-4-exchange", key, student);
25 | });
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/tutorial-5/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | tutorial-5
8 | 1.0
9 | pom
10 |
11 |
12 | cero.ninja.tutorial.springboot.amqp
13 | spring-boot-amqp-tutorial
14 | 1.0
15 |
16 |
17 |
18 | tutorial-5-consumer
19 | tutorial-5-domain
20 | tutorial-5-producer
21 |
22 |
23 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-consumer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-5
9 | 1.0
10 |
11 |
12 | tutorial-5-consumer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-5-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-consumer/src/main/java/consumer/AmqpConfig.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.amqp.core.BindingBuilder;
4 | import org.springframework.amqp.core.Exchange;
5 | import org.springframework.amqp.core.Queue;
6 | import org.springframework.amqp.core.TopicExchange;
7 | import org.springframework.amqp.rabbit.annotation.EnableRabbit;
8 | import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
9 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
10 | import org.springframework.amqp.rabbit.core.RabbitAdmin;
11 | import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
12 | import org.springframework.context.annotation.Bean;
13 | import org.springframework.context.annotation.Configuration;
14 | import org.springframework.messaging.converter.MappingJackson2MessageConverter;
15 | import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
16 |
17 | @EnableRabbit
18 | @Configuration
19 | public class AmqpConfig implements RabbitListenerConfigurer {
20 | static final String EXCHANGE_NAME = "tutorial-5-exchange";
21 | static final String QUEUE1_NAME = "tutorial-5-1";
22 | static final String QUEUE2_NAME = "tutorial-5-2";
23 |
24 | @Bean
25 | RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
26 | return new RabbitAdmin(connectionFactory);
27 | }
28 |
29 | @Bean
30 | Exchange exchange(RabbitAdmin rabbitAdmin) {
31 | TopicExchange exchange = new TopicExchange(EXCHANGE_NAME);
32 | rabbitAdmin.declareExchange(exchange);
33 |
34 | Queue queue1 = new Queue(QUEUE1_NAME);
35 | Queue queue2 = new Queue(QUEUE2_NAME);
36 | rabbitAdmin.declareQueue(queue1);
37 | rabbitAdmin.declareQueue(queue2);
38 |
39 | rabbitAdmin.declareBinding(BindingBuilder.bind(queue1)
40 | .to(exchange).with("0.0"));
41 | rabbitAdmin.declareBinding(BindingBuilder.bind(queue2)
42 | .to(exchange).with("0.*"));
43 | rabbitAdmin.declareBinding(BindingBuilder.bind(queue2)
44 | .to(exchange).with("*.0"));
45 |
46 | return exchange;
47 | }
48 |
49 | @Override
50 | public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
51 | registrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
52 | }
53 |
54 | @Bean
55 | public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
56 | DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
57 | factory.setMessageConverter(new MappingJackson2MessageConverter());
58 | return factory;
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-consumer/src/main/java/consumer/ConsumerApplication.java:
--------------------------------------------------------------------------------
1 | package consumer;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class ConsumerApplication {
8 | public static void main(String[] args) {
9 | SpringApplication.run(ConsumerApplication.class, args);
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-consumer/src/main/java/consumer/receiver/SimpleReceiver.java:
--------------------------------------------------------------------------------
1 | package consumer.receiver;
2 |
3 | import domain.Student;
4 | import org.springframework.amqp.rabbit.annotation.RabbitListener;
5 | import org.springframework.stereotype.Component;
6 |
7 | @Component
8 | public class SimpleReceiver {
9 | @RabbitListener(queues = "tutorial-5-1")
10 | public void receive1(Student student) throws InterruptedException {
11 | System.out.println("5-1:" + student.id + " - " + student.score);
12 | }
13 |
14 | @RabbitListener(queues = "tutorial-5-2")
15 | public void receive2(Student student) throws InterruptedException {
16 | System.out.println("5-2:" + student.id + " - " + student.score);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-domain/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-5
9 | 1.0
10 |
11 |
12 | tutorial-5-domain
13 |
14 |
15 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-domain/src/main/java/domain/Student.java:
--------------------------------------------------------------------------------
1 | package domain;
2 |
3 | public class Student {
4 | public Integer id;
5 | public String name;
6 | public Integer score;
7 | }
8 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-producer/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | cero.ninja.tutorial.springboot.amqp
8 | tutorial-5
9 | 1.0
10 |
11 |
12 | tutorial-5-producer
13 |
14 |
15 |
16 | cero.ninja.tutorial.springboot.amqp
17 | tutorial-5-domain
18 | 1.0
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-producer/src/main/java/producer/ProducerApplication.java:
--------------------------------------------------------------------------------
1 | package producer;
2 |
3 | import org.springframework.amqp.core.Exchange;
4 | import org.springframework.amqp.core.TopicExchange;
5 | import org.springframework.amqp.rabbit.connection.ConnectionFactory;
6 | import org.springframework.amqp.rabbit.core.RabbitAdmin;
7 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
8 | import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
9 | import org.springframework.boot.SpringApplication;
10 | import org.springframework.boot.autoconfigure.SpringBootApplication;
11 | import org.springframework.context.ConfigurableApplicationContext;
12 | import org.springframework.context.annotation.Bean;
13 | import producer.sender.SimpleSender;
14 |
15 | import java.io.IOException;
16 |
17 | @SpringBootApplication
18 | public class ProducerApplication {
19 | static final String EXCHANGE_NAME = "tutorial-5-exchange";
20 |
21 | public static void main(String[] args) {
22 | ConfigurableApplicationContext context = SpringApplication.run(ProducerApplication.class, args);
23 | SimpleSender sender = context.getBean(SimpleSender.class);
24 | sender.send();
25 | context.close();
26 | }
27 |
28 | @Bean
29 | Exchange exchange(ConnectionFactory connectionFactory) {
30 | TopicExchange exchange = new TopicExchange(EXCHANGE_NAME);
31 | new RabbitAdmin(connectionFactory).declareExchange(exchange);
32 | return exchange;
33 | }
34 |
35 | @Bean
36 | RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) throws IOException {
37 | RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
38 | rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
39 | return rabbitTemplate;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/tutorial-5/tutorial-5-producer/src/main/java/producer/sender/SimpleSender.java:
--------------------------------------------------------------------------------
1 | package producer.sender;
2 |
3 | import domain.Student;
4 | import org.springframework.amqp.rabbit.core.RabbitTemplate;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Component;
7 |
8 | import java.util.stream.IntStream;
9 |
10 | @Component
11 | public class SimpleSender {
12 | @Autowired
13 | RabbitTemplate rabbitTemplate;
14 |
15 | public void send() {
16 | Student student = new Student();
17 | IntStream.range(0, 10)
18 | .forEach(i -> {
19 | student.id = i;
20 | student.name = "TEST" + i;
21 | student.score = 100 - i;
22 |
23 | String key = String.valueOf((student.score % 2) + "." + (student.score % 3));
24 | rabbitTemplate.convertAndSend("tutorial-5-exchange", key, student);
25 | });
26 | }
27 | }
28 |
--------------------------------------------------------------------------------