Commit 8ed46194 authored by Stephane Nicoll's avatar Stephane Nicoll

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
parent 02a8a9c0
...@@ -4,7 +4,7 @@ import java.util.concurrent.CountDownLatch ...@@ -4,7 +4,7 @@ import java.util.concurrent.CountDownLatch
@Log @Log
@Configuration @Configuration
@EnableRabbitMessaging @EnableRabbit
class RabbitExample implements CommandLineRunner { class RabbitExample implements CommandLineRunner {
private CountDownLatch latch = new CountDownLatch(1) private CountDownLatch latch = new CountDownLatch(1)
...@@ -12,52 +12,21 @@ class RabbitExample implements CommandLineRunner { ...@@ -12,52 +12,21 @@ class RabbitExample implements CommandLineRunner {
@Autowired @Autowired
RabbitTemplate rabbitTemplate 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) { void run(String... args) {
log.info "Sending RabbitMQ message..." 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() latch.await()
} }
}
@Log
class Receiver {
CountDownLatch latch
@RabbitListener(queues = 'spring-boot')
def receive(String message) { def receive(String message) {
log.info "Received ${message}" log.info "Received ${message}"
latch.countDown() latch.countDown()
} }
}
@Bean
Queue queue() {
new Queue("spring-boot", false)
}
}
\ No newline at end of file
...@@ -28,14 +28,14 @@ import org.springframework.boot.groovy.EnableRabbitMessaging; ...@@ -28,14 +28,14 @@ import org.springframework.boot.groovy.EnableRabbitMessaging;
* {@link CompilerAutoConfiguration} for Spring Rabbit. * {@link CompilerAutoConfiguration} for Spring Rabbit.
* *
* @author Greg Turnquist * @author Greg Turnquist
* @author Stephane Nicoll
*/ */
public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration { public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override @Override
public boolean matches(ClassNode classNode) { public boolean matches(ClassNode classNode) {
// Slightly weird detection algorithm because there is no @Enable annotation for return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbit")
// Integration || AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbitMessaging");
return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbitMessaging");
} }
@Override @Override
...@@ -47,7 +47,9 @@ public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration { ...@@ -47,7 +47,9 @@ public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override @Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException { 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.connection",
"org.springframework.amqp.rabbit.listener", "org.springframework.amqp.rabbit.listener",
"org.springframework.amqp.rabbit.listener.adapter", "org.springframework.amqp.rabbit.listener.adapter",
......
...@@ -26,10 +26,13 @@ import org.springframework.boot.cli.compiler.autoconfigure.RabbitCompilerAutoCon ...@@ -26,10 +26,13 @@ import org.springframework.boot.cli.compiler.autoconfigure.RabbitCompilerAutoCon
/** /**
* Pseudo annotation used to trigger {@link RabbitCompilerAutoConfiguration}. * Pseudo annotation used to trigger {@link RabbitCompilerAutoConfiguration}.
*
* @deprecated since 1.2.0 in favor of {@code EnableRabbit}
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Documented @Documented
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Deprecated
public @interface EnableRabbitMessaging { public @interface EnableRabbitMessaging {
} }
...@@ -122,7 +122,7 @@ The following items are used as ``grab hints'': ...@@ -122,7 +122,7 @@ The following items are used as ``grab hints'':
|`@Test` |`@Test`
|JUnit. |JUnit.
|`@EnableRabbitMessaging` |`@EnableRabbit`
|RabbitMQ. |RabbitMQ.
|`@EnableReactor` |`@EnableReactor`
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment