2013-09-25 23:31:03 +08:00
|
|
|
package org.test
|
|
|
|
|
|
|
|
import java.util.concurrent.CountDownLatch
|
|
|
|
|
|
|
|
@Log
|
2019-02-08 22:51:38 +08:00
|
|
|
@Configuration(proxyBeanMethods = false)
|
2014-09-05 21:27:46 +08:00
|
|
|
@EnableRabbit
|
2013-09-25 23:31:03 +08:00
|
|
|
class RabbitExample implements CommandLineRunner {
|
|
|
|
|
2013-11-05 14:02:20 +08:00
|
|
|
private CountDownLatch latch = new CountDownLatch(1)
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
RabbitTemplate rabbitTemplate
|
|
|
|
|
|
|
|
void run(String... args) {
|
|
|
|
log.info "Sending RabbitMQ message..."
|
2014-09-05 21:27:46 +08:00
|
|
|
rabbitTemplate.convertAndSend("spring-boot", "Greetings from Spring Boot via RabbitMQ")
|
2013-11-05 14:02:20 +08:00
|
|
|
latch.await()
|
|
|
|
}
|
2013-09-25 23:31:03 +08:00
|
|
|
|
2014-09-05 21:27:46 +08:00
|
|
|
@RabbitListener(queues = 'spring-boot')
|
2013-11-05 14:02:20 +08:00
|
|
|
def receive(String message) {
|
|
|
|
log.info "Received ${message}"
|
|
|
|
latch.countDown()
|
|
|
|
}
|
2014-09-05 21:27:46 +08:00
|
|
|
|
|
|
|
@Bean
|
2020-02-05 04:23:42 +08:00
|
|
|
org.springframework.amqp.core.Queue queue() {
|
|
|
|
new org.springframework.amqp.core.Queue("spring-boot", false)
|
2014-09-05 21:27:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|