Add support for Spring Rabbit (via Spring AMQP) to Boot
- If RabbitTemplate is on the classpath, turn on autodetection. - Create a RabbitTemplate, a Rabbit ConnectionFactory, and a RabbitAdmin is spring.rabbitmq.dynamic:true - Enable some **spring.rabbitmq** properties like host, port, username, password, and dynamic - Add tests to verify functionality - Add Groovy CLI functionality. Base it on @EnableRabbitMessaging. Add spring-amqp to the path. - Create rabbit.groovy test to prove it all works. - Make Queue and TopicExchange top-level Spring beans in rabbit.groovy test script
This commit is contained in:
committed by
Dave Syer
parent
fa6e6fde6c
commit
941d163709
64
spring-boot-cli/samples/rabbit.groovy
Normal file
64
spring-boot-cli/samples/rabbit.groovy
Normal file
@@ -0,0 +1,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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user