Skip to content

Commit 30570a2

Browse files
garyrussellartembilan
authored andcommitted
GH-1140: RabbitTemplate and CGLIB Proxies
Resolves #1140 `start()` and `stop()` methods are `final`, causing NPEs with CGLIB proxies. - remove `final` modifiers - pull `Lifecycle` up to `RabbitOperations` so JDK proxies can be used - add default no-op implementations for backward compatibility Also implement `DisposableBean` so that reply containers are stopped.
1 parent 2557338 commit 30570a2

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitOperations.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import org.springframework.amqp.core.MessagePostProcessor;
2323
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
2424
import org.springframework.amqp.rabbit.connection.CorrelationData;
25+
import org.springframework.context.Lifecycle;
2526
import org.springframework.core.ParameterizedTypeReference;
2627
import org.springframework.lang.Nullable;
2728

@@ -33,7 +34,7 @@
3334
* @author Gary Russell
3435
* @author Artem Bilan
3536
*/
36-
public interface RabbitOperations extends AmqpTemplate {
37+
public interface RabbitOperations extends AmqpTemplate, Lifecycle {
3738

3839
/**
3940
* Execute the callback with a channel and reliably close the channel afterwards.
@@ -421,6 +422,22 @@ <T> T convertSendAndReceiveAsType(String exchange, String routingKey, Object mes
421422
@Nullable CorrelationData correlationData,
422423
ParameterizedTypeReference<T> responseType) throws AmqpException;
423424

425+
426+
@Override
427+
default void start() {
428+
// No-op - implemented for backward compatibility
429+
}
430+
431+
@Override
432+
default void stop() {
433+
// No-op - implemented for backward compatibility
434+
}
435+
436+
@Override
437+
default boolean isRunning() {
438+
return false;
439+
}
440+
424441
/**
425442
* Callback for using the same channel for multiple RabbitTemplate
426443
* operations.

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,7 +80,7 @@
8080
import org.springframework.beans.factory.BeanFactory;
8181
import org.springframework.beans.factory.BeanFactoryAware;
8282
import org.springframework.beans.factory.BeanNameAware;
83-
import org.springframework.context.Lifecycle;
83+
import org.springframework.beans.factory.DisposableBean;
8484
import org.springframework.context.expression.BeanFactoryResolver;
8585
import org.springframework.context.expression.MapAccessor;
8686
import org.springframework.core.ParameterizedTypeReference;
@@ -149,7 +149,7 @@
149149
*/
150150
public class RabbitTemplate extends RabbitAccessor // NOSONAR type line count
151151
implements BeanFactoryAware, RabbitOperations, MessageListener,
152-
ListenerContainerAware, PublisherCallbackChannel.Listener, Lifecycle, BeanNameAware {
152+
ListenerContainerAware, PublisherCallbackChannel.Listener, BeanNameAware, DisposableBean {
153153

154154
private static final String UNCHECKED = "unchecked";
155155

@@ -879,7 +879,7 @@ public int getUnconfirmedCount() {
879879
}
880880

881881
@Override
882-
public final void start() {
882+
public void start() {
883883
doStart();
884884
}
885885

@@ -892,7 +892,7 @@ protected void doStart() {
892892
}
893893

894894
@Override
895-
public final void stop() {
895+
public void stop() {
896896
synchronized (this.directReplyToContainers) {
897897
this.directReplyToContainers.values()
898898
.stream()
@@ -920,6 +920,11 @@ public boolean isRunning() {
920920
}
921921
}
922922

923+
@Override
924+
public void destroy() {
925+
stop();
926+
}
927+
923928
private void evaluateFastReplyTo() {
924929
this.usingFastReplyTo = useDirectReplyTo();
925930
this.evaluatedFastReplyTo = true;

0 commit comments

Comments
 (0)