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
pull/1519/head
Stephane Nicoll 10 years ago
parent 02a8a9c07b
commit 8ed461947f

@ -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)
}
}

@ -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",

@ -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 {
}

@ -122,7 +122,7 @@ The following items are used as ``grab hints'':
|`@Test`
|JUnit.
|`@EnableRabbitMessaging`
|`@EnableRabbit`
|RabbitMQ.
|`@EnableReactor`

Loading…
Cancel
Save