Skip to content

Commit 0a0cfe4

Browse files
committed
GH-1261: Remove unnecessary volatile modifiers
Resolves #1261 `active` and `running` are only refered to under the lifecycle lock. Others are set during bean creation.
1 parent 0401051 commit 0a0cfe4

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/AbstractMessageListenerContainer.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public abstract class AbstractMessageListenerContainer extends RabbitAccessor
133133
private static final boolean MICROMETER_PRESENT = ClassUtils.isPresent(
134134
"io.micrometer.core.instrument.MeterRegistry", AbstractMessageListenerContainer.class.getClassLoader());
135135

136+
private final Object lifecycleMonitor = new Object();
137+
136138
private final ContainerDelegate delegate = this::actualInvokeListener;
137139

138140
protected final Object consumersMonitor = new Object(); //NOSONAR
@@ -183,29 +185,25 @@ public abstract class AbstractMessageListenerContainer extends RabbitAccessor
183185

184186
private int phase = Integer.MAX_VALUE;
185187

186-
private volatile boolean active = false;
187-
188-
private volatile boolean running = false;
189-
190-
private final Object lifecycleMonitor = new Object();
188+
private boolean active = false;
191189

192-
private volatile List<Queue> queues = new CopyOnWriteArrayList<>();
190+
private boolean running = false;
193191

194192
private ErrorHandler errorHandler = new ConditionalRejectingErrorHandler();
195193

196194
private boolean exposeListenerChannel = true;
197195

198-
private volatile MessageListener messageListener;
196+
private MessageListener messageListener;
199197

200-
private volatile AcknowledgeMode acknowledgeMode = AcknowledgeMode.AUTO;
198+
private AcknowledgeMode acknowledgeMode = AcknowledgeMode.AUTO;
201199

202-
private volatile boolean deBatchingEnabled = DEFAULT_DEBATCHING_ENABLED;
200+
private boolean deBatchingEnabled = DEFAULT_DEBATCHING_ENABLED;
203201

204-
private volatile boolean initialized;
202+
private boolean initialized;
205203

206204
private Collection<MessagePostProcessor> afterReceivePostProcessors;
207205

208-
private volatile ApplicationContext applicationContext;
206+
private ApplicationContext applicationContext;
209207

210208
private String listenerId;
211209

@@ -214,17 +212,17 @@ public abstract class AbstractMessageListenerContainer extends RabbitAccessor
214212
@Nullable
215213
private ConsumerTagStrategy consumerTagStrategy;
216214

217-
private volatile boolean exclusive;
215+
private boolean exclusive;
218216

219-
private volatile boolean noLocal;
217+
private boolean noLocal;
220218

221-
private volatile boolean defaultRequeueRejected = true;
219+
private boolean defaultRequeueRejected = true;
222220

223-
private volatile int prefetchCount = DEFAULT_PREFETCH_COUNT;
221+
private int prefetchCount = DEFAULT_PREFETCH_COUNT;
224222

225223
private long idleEventInterval;
226224

227-
private volatile long lastReceive = System.currentTimeMillis();
225+
private long lastReceive = System.currentTimeMillis();
228226

229227
private boolean statefulRetryFatalWithNullMessageId = true;
230228

@@ -250,6 +248,8 @@ public abstract class AbstractMessageListenerContainer extends RabbitAccessor
250248

251249
private JavaLangErrorHandler javaLangErrorHandler = error -> System.exit(EXIT_99);
252250

251+
private volatile List<Queue> queues = new CopyOnWriteArrayList<>();
252+
253253
private volatile boolean lazyLoad;
254254

255255
@Override

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public class SimpleMessageListenerContainer extends AbstractMessageListenerConta
102102

103103
private final BlockingQueue<ListenerContainerConsumerFailedEvent> abortEvents = new LinkedBlockingQueue<>();
104104

105+
private final ActiveObjectCounter<BlockingQueueConsumer> cancellationLock = new ActiveObjectCounter<>();
106+
105107
private long startConsumerMinInterval = DEFAULT_START_CONSUMER_MIN_INTERVAL;
106108

107109
private long stopConsumerMinInterval = DEFAULT_STOP_CONSUMER_MIN_INTERVAL;
@@ -118,8 +120,6 @@ public class SimpleMessageListenerContainer extends AbstractMessageListenerConta
118120

119121
private Set<BlockingQueueConsumer> consumers;
120122

121-
private final ActiveObjectCounter<BlockingQueueConsumer> cancellationLock = new ActiveObjectCounter<>();
122-
123123
private Integer declarationRetries;
124124

125125
private Long retryDeclarationInterval;

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/SimpleMessageListenerContainerIntegration2Tests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ public void testErrorStopsContainer() throws Exception {
626626
this.container = createContainer((m) -> {
627627
throw new Error("testError");
628628
}, false, this.queue.getName());
629+
this.container.setjavaLangErrorHandler(error -> { });
629630
final CountDownLatch latch = new CountDownLatch(1);
630631
this.container.setApplicationEventPublisher(event -> {
631632
if (event instanceof ListenerContainerConsumerFailedEvent) {

0 commit comments

Comments
 (0)