Skip to content

Commit 8ed4619

Browse files
committed
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 spring-projectsgh-1494
1 parent 02a8a9c commit 8ed4619

File tree

4 files changed

+20
-46
lines changed

4 files changed

+20
-46
lines changed

Diff for: spring-boot-cli/samples/rabbit.groovy

+10-41
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,29 @@ import java.util.concurrent.CountDownLatch
44

55
@Log
66
@Configuration
7-
@EnableRabbitMessaging
7+
@EnableRabbit
88
class RabbitExample implements CommandLineRunner {
99

1010
private CountDownLatch latch = new CountDownLatch(1)
1111

1212
@Autowired
1313
RabbitTemplate rabbitTemplate
1414

15-
private String queueName = "spring-boot"
16-
17-
@Bean
18-
Queue queue() {
19-
new Queue(queueName, false)
20-
}
21-
22-
@Bean
23-
TopicExchange exchange() {
24-
new TopicExchange("spring-boot-exchange")
25-
}
26-
27-
/**
28-
* The queue and topic exchange cannot be inlined inside this method and have
29-
* dynamic creation with Spring AMQP work properly.
30-
*/
31-
@Bean
32-
Binding binding(Queue queue, TopicExchange exchange) {
33-
BindingBuilder
34-
.bind(queue)
35-
.to(exchange)
36-
.with("spring-boot")
37-
}
38-
39-
@Bean
40-
SimpleMessageListenerContainer container(CachingConnectionFactory connectionFactory) {
41-
return new SimpleMessageListenerContainer(
42-
connectionFactory: connectionFactory,
43-
queueNames: [queueName],
44-
messageListener: new MessageListenerAdapter(new Receiver(latch:latch), "receive")
45-
)
46-
}
47-
4815
void run(String... args) {
4916
log.info "Sending RabbitMQ message..."
50-
rabbitTemplate.convertAndSend(queueName, "Greetings from Spring Boot via RabbitMQ")
17+
rabbitTemplate.convertAndSend("spring-boot", "Greetings from Spring Boot via RabbitMQ")
5118
latch.await()
5219
}
53-
}
54-
55-
@Log
56-
class Receiver {
57-
CountDownLatch latch
5820

21+
@RabbitListener(queues = 'spring-boot')
5922
def receive(String message) {
6023
log.info "Received ${message}"
6124
latch.countDown()
6225
}
63-
}
26+
27+
@Bean
28+
Queue queue() {
29+
new Queue("spring-boot", false)
30+
}
31+
32+
}

Diff for: spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/RabbitCompilerAutoConfiguration.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
* {@link CompilerAutoConfiguration} for Spring Rabbit.
2929
*
3030
* @author Greg Turnquist
31+
* @author Stephane Nicoll
3132
*/
3233
public class RabbitCompilerAutoConfiguration extends CompilerAutoConfiguration {
3334

3435
@Override
3536
public boolean matches(ClassNode classNode) {
36-
// Slightly weird detection algorithm because there is no @Enable annotation for
37-
// Integration
38-
return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbitMessaging");
37+
return AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbit")
38+
|| AstUtils.hasAtLeastOneAnnotation(classNode, "EnableRabbitMessaging");
3939
}
4040

4141
@Override
@@ -47,7 +47,9 @@ public void applyDependencies(DependencyCustomizer dependencies)
4747

4848
@Override
4949
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
50-
imports.addStarImports("org.springframework.amqp.rabbit.core",
50+
imports.addStarImports("org.springframework.amqp.rabbit.annotation",
51+
"org.springframework.amqp.rabbit.core",
52+
"org.springframework.amqp.rabbit.config",
5153
"org.springframework.amqp.rabbit.connection",
5254
"org.springframework.amqp.rabbit.listener",
5355
"org.springframework.amqp.rabbit.listener.adapter",

Diff for: spring-boot-cli/src/main/java/org/springframework/boot/groovy/EnableRabbitMessaging.java

+3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626

2727
/**
2828
* Pseudo annotation used to trigger {@link RabbitCompilerAutoConfiguration}.
29+
*
30+
* @deprecated since 1.2.0 in favor of {@code EnableRabbit}
2931
*/
3032
@Target(ElementType.TYPE)
3133
@Documented
3234
@Retention(RetentionPolicy.RUNTIME)
35+
@Deprecated
3336
public @interface EnableRabbitMessaging {
3437

3538
}

Diff for: spring-boot-docs/src/main/asciidoc/spring-boot-cli.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ The following items are used as ``grab hints'':
122122
|`@Test`
123123
|JUnit.
124124

125-
|`@EnableRabbitMessaging`
125+
|`@EnableRabbit`
126126
|RabbitMQ.
127127

128128
|`@EnableReactor`

0 commit comments

Comments
 (0)