@@ -7,58 +7,57 @@ import java.util.concurrent.CountDownLatch
7
7
@EnableRabbitMessaging
8
8
class RabbitExample implements CommandLineRunner {
9
9
10
- private CountDownLatch latch = new CountDownLatch (1 )
11
-
12
- @Autowired
13
- RabbitTemplate rabbitTemplate
14
-
15
- private String queueName = " spring-boot"
16
-
17
- @Bean
18
- Queue queue () {
19
- new Queue (queueName, false )
20
- }
21
-
22
- @Bean
23
- TopicExchange exchange () {
24
- new TopicExchange (" spring-boot-exchange" )
25
- }
26
-
27
- /**
28
- * The queue and topic exchange cannot be inlined inside this method and have
29
- * dynamic creation with Spring AMQP work properly.
30
- */
31
- @Bean
32
- Binding binding (Queue queue , TopicExchange exchange ) {
33
- BindingBuilder
34
- .bind(queue)
35
- .to(exchange)
36
- .with(" spring-boot" )
37
- }
38
-
39
- @Bean
40
- SimpleMessageListenerContainer container (CachingConnectionFactory connectionFactory ) {
41
- return new SimpleMessageListenerContainer (
42
- connectionFactory : connectionFactory,
43
- queueNames : [queueName],
44
- messageListener : new MessageListenerAdapter (new Receiver (latch :latch), " receive" )
45
- )
46
- }
47
-
48
- void run (String ... args ) {
49
- log. info " Sending RabbitMQ message..."
50
- rabbitTemplate. convertAndSend(queueName, " Greetings from Spring Boot via RabbitMQ" )
51
- latch. await()
52
- }
53
-
10
+ private CountDownLatch latch = new CountDownLatch (1 )
11
+
12
+ @Autowired
13
+ RabbitTemplate rabbitTemplate
14
+
15
+ private String queueName = " spring-boot"
16
+
17
+ @Bean
18
+ Queue queue () {
19
+ new Queue (queueName, false )
20
+ }
21
+
22
+ @Bean
23
+ TopicExchange exchange () {
24
+ new TopicExchange (" spring-boot-exchange" )
25
+ }
26
+
27
+ /**
28
+ * The queue and topic exchange cannot be inlined inside this method and have
29
+ * dynamic creation with Spring AMQP work properly.
30
+ */
31
+ @Bean
32
+ Binding binding (Queue queue , TopicExchange exchange ) {
33
+ BindingBuilder
34
+ .bind(queue)
35
+ .to(exchange)
36
+ .with(" spring-boot" )
37
+ }
38
+
39
+ @Bean
40
+ SimpleMessageListenerContainer container (CachingConnectionFactory connectionFactory ) {
41
+ return new SimpleMessageListenerContainer (
42
+ connectionFactory : connectionFactory,
43
+ queueNames : [queueName],
44
+ messageListener : new MessageListenerAdapter (new Receiver (latch :latch), " receive" )
45
+ )
46
+ }
47
+
48
+ void run (String ... args ) {
49
+ log. info " Sending RabbitMQ message..."
50
+ rabbitTemplate. convertAndSend(queueName, " Greetings from Spring Boot via RabbitMQ" )
51
+ latch. await()
52
+ }
54
53
}
55
54
56
55
@Log
57
56
class Receiver {
58
- CountDownLatch latch
57
+ CountDownLatch latch
59
58
60
- def receive (String message ) {
61
- log. info " Received ${ message} "
62
- latch. countDown()
63
- }
59
+ def receive (String message ) {
60
+ log. info " Received ${ message} "
61
+ latch. countDown()
62
+ }
64
63
}
0 commit comments