From 8ed461947f4d69ae0cf8a9e243e8955d38d6ad95 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 5 Sep 2014 15:27:46 +0200 Subject: [PATCH] Improve RabbitMQ support in CLI This commit deprecates the proprietary EnableRabbitMessaging annotation in favour of the standard @EnableRabbit introduced as of Spring Rabbit 1.4. Fixes gh-1494 --- spring-boot-cli/samples/rabbit.groovy | 51 ++++--------------- .../RabbitCompilerAutoConfiguration.java | 10 ++-- .../boot/groovy/EnableRabbitMessaging.java | 3 ++ .../src/main/asciidoc/spring-boot-cli.adoc | 2 +- 4 files changed, 20 insertions(+), 46 deletions(-) diff --git a/spring-boot-cli/samples/rabbit.groovy b/spring-boot-cli/samples/rabbit.groovy index 87ac1c2a384..70d04481f95 100644 --- a/spring-boot-cli/samples/rabbit.groovy +++ b/spring-boot-cli/samples/rabbit.groovy @@ -4,7 +4,7 @@ import java.util.concurrent.CountDownLatch @Log @Configuration -@EnableRabbitMessaging +@EnableRabbit class RabbitExample implements CommandLineRunner { private CountDownLatch latch = new CountDownLatch(1) @@ -12,52 +12,21 @@ class RabbitExample implements CommandLineRunner { @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") + rabbitTemplate.convertAndSend("spring-boot", "Greetings from Spring Boot via RabbitMQ") latch.await() } -} - -@Log -class Receiver { - CountDownLatch latch + @RabbitListener(queues = 'spring-boot') def receive(String message) { log.info "Received ${message}" latch.countDown() } -} + + @Bean + Queue queue() { + new Queue("spring-boot", false) + } + +} \ No newline at end of file diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/RabbitCompilerAutoConfiguration.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/RabbitCompilerAutoConfiguration.java index 95f80037e13..c6099a82723 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/RabbitCompilerAutoConfiguration.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/RabbitCompilerAutoConfiguration.java @@ -28,14 +28,14 @@ import org.springframework.boot.groovy.EnableRabbitMessaging; * {@link CompilerAutoConfiguration} for Spring Rabbit. * * @author Greg Turnquist + * @author Stephane Nicoll */ public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration { @Override public boolean matches(ClassNode classNode) { - // Slightly weird detection algorithm because there is no @Enable annotation for - // Integration - return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbitMessaging"); + return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbit") + || AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbitMessaging"); } @Override @@ -47,7 +47,9 @@ public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration { @Override public void applyImports(ImportCustomizer imports) throws CompilationFailedException { - imports.addStarImports("org.springframework.amqp.rabbit.core", + imports.addStarImports("org.springframework.amqp.rabbit.annotation", + "org.springframework.amqp.rabbit.core", + "org.springframework.amqp.rabbit.config", "org.springframework.amqp.rabbit.connection", "org.springframework.amqp.rabbit.listener", "org.springframework.amqp.rabbit.listener.adapter", diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableRabbitMessaging.java b/spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableRabbitMessaging.java index 4fd1391b59b..a5a7d737e2b 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableRabbitMessaging.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableRabbitMessaging.java @@ -26,10 +26,13 @@ import org.springframework.boot.cli.compiler.autoconfigure.RabbitCompilerAutoCon /** * Pseudo annotation used to trigger {@link RabbitCompilerAutoConfiguration}. + * + * @deprecated since 1.2.0 in favor of {@code EnableRabbit} */ @Target(ElementType.TYPE) @Documented @Retention(RetentionPolicy.RUNTIME) +@Deprecated public @interface EnableRabbitMessaging { } diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-cli.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-cli.adoc index 4c1f272969f..4d6528bea34 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-cli.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-cli.adoc @@ -122,7 +122,7 @@ The following items are used as ``grab hints'': |`@Test` |JUnit. -|`@EnableRabbitMessaging` +|`@EnableRabbit` |RabbitMQ. |`@EnableReactor`