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