Consistent volatile access to running flag in Lifecycle implementations

Issue: SPR-16596
Issue: SPR-16488

(cherry picked from commit d4a8f76)
This commit is contained in:
Juergen Hoeller
2018-03-15 15:17:55 +01:00
parent 6158634d67
commit b8c92ce931
15 changed files with 76 additions and 109 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -76,7 +76,7 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
private boolean active = false;
private boolean running = false;
private volatile boolean running = false;
private final List<Object> pausedTasks = new LinkedList<Object>();
@@ -344,9 +344,7 @@ public abstract class AbstractJmsListeningContainer extends JmsDestinationAccess
*/
@Override
public final boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return (this.running && runningAllowed());
}
return (this.running && runningAllowed());
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -111,10 +111,10 @@ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHan
private MessageHeaderInitializer headerInitializer;
private final Object lifecycleMonitor = new Object();
private volatile boolean running = false;
private final Object lifecycleMonitor = new Object();
/**
* Create an instance of SimpAnnotationMethodMessageHandler with the given
@@ -298,9 +298,7 @@ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHan
@Override
public final boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return this.running;
}
return this.running;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -153,9 +153,7 @@ public abstract class AbstractBrokerMessageHandler
@Override
public void start() {
synchronized (this.lifecycleMonitor) {
if (logger.isInfoEnabled()) {
logger.info("Starting...");
}
logger.info("Starting...");
this.clientInboundChannel.subscribe(this);
this.brokerChannel.subscribe(this);
if (this.clientInboundChannel instanceof InterceptableChannel) {
@@ -173,9 +171,7 @@ public abstract class AbstractBrokerMessageHandler
@Override
public void stop() {
synchronized (this.lifecycleMonitor) {
if (logger.isInfoEnabled()) {
logger.info("Stopping...");
}
logger.info("Stopping...");
stopInternal();
this.clientInboundChannel.unsubscribe(this);
this.brokerChannel.unsubscribe(this);
@@ -206,9 +202,7 @@ public abstract class AbstractBrokerMessageHandler
*/
@Override
public final boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return this.running;
}
return this.running;
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -64,10 +64,10 @@ public class UserDestinationMessageHandler implements MessageHandler, SmartLifec
private MessageHeaderInitializer headerInitializer;
private final Object lifecycleMonitor = new Object();
private volatile boolean running = false;
private final Object lifecycleMonitor = new Object();
/**
* Create an instance with the given client and broker channels subscribing
@@ -180,9 +180,7 @@ public class UserDestinationMessageHandler implements MessageHandler, SmartLifec
@Override
public final boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return this.running;
}
return this.running;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -156,7 +156,7 @@ public class GenericMessageEndpointManager implements SmartLifecycle, Initializi
private int phase = Integer.MAX_VALUE;
private boolean running = false;
private volatile boolean running = false;
private final Object lifecycleMonitor = new Object();
@@ -318,9 +318,7 @@ public class GenericMessageEndpointManager implements SmartLifecycle, Initializi
*/
@Override
public boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return this.running;
}
return this.running;
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,15 +38,14 @@ public abstract class ConnectionManagerSupport implements SmartLifecycle {
protected final Log logger = LogFactory.getLog(getClass());
private final URI uri;
private boolean autoStartup = false;
private boolean running = false;
private int phase = Integer.MAX_VALUE;
private volatile boolean running = false;
private final Object lifecycleMonitor = new Object();
@@ -161,9 +160,7 @@ public abstract class ConnectionManagerSupport implements SmartLifecycle {
*/
@Override
public boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return this.running;
}
return this.running;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -60,8 +60,6 @@ public class JettyWebSocketClient extends AbstractWebSocketClient implements Lif
private final org.eclipse.jetty.websocket.client.WebSocketClient client;
private final Object lifecycleMonitor = new Object();
private AsyncListenableTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
@@ -99,47 +97,33 @@ public class JettyWebSocketClient extends AbstractWebSocketClient implements Lif
return this.taskExecutor;
}
@Override
public boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return this.client.isStarted();
}
}
@Override
public void start() {
synchronized (this.lifecycleMonitor) {
if (!isRunning()) {
try {
if (logger.isInfoEnabled()) {
logger.info("Starting Jetty WebSocketClient");
}
this.client.start();
}
catch (Exception ex) {
throw new IllegalStateException("Failed to start Jetty client", ex);
}
}
try {
this.client.start();
}
catch (Exception ex) {
throw new IllegalStateException("Failed to start Jetty WebSocketClient", ex);
}
}
@Override
public void stop() {
synchronized (this.lifecycleMonitor) {
if (isRunning()) {
try {
if (logger.isInfoEnabled()) {
logger.info("Stopping Jetty WebSocketClient");
}
this.client.stop();
}
catch (Exception ex) {
logger.error("Error stopping Jetty WebSocketClient", ex);
}
}
try {
this.client.stop();
}
catch (Exception ex) {
logger.error("Failed to stop Jetty WebSocketClient", ex);
}
}
@Override
public boolean isRunning() {
return this.client.isStarted();
}
@Override
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler,
String uriTemplate, Object... uriVars) {

View File

@@ -102,10 +102,10 @@ public class SubProtocolWebSocketHandler
private final Stats stats = new Stats();
private final Object lifecycleMonitor = new Object();
private volatile boolean running = false;
private final Object lifecycleMonitor = new Object();
/**
* Create a new {@code SubProtocolWebSocketHandler} for the given inbound and outbound channels.
@@ -279,9 +279,7 @@ public class SubProtocolWebSocketHandler
@Override
public final boolean isRunning() {
synchronized (this.lifecycleMonitor) {
return this.running;
}
return this.running;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -72,17 +72,16 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif
private static final Log logger = LogFactory.getLog(WebSocketStompClient.class);
private final WebSocketClient webSocketClient;
private int inboundMessageSizeLimit = 64 * 1024;
private boolean autoStartup = true;
private boolean running = false;
private int phase = Integer.MAX_VALUE;
private volatile boolean running = false;
/**
* Class constructor. Sets {@link #setDefaultHeartbeat} to "0,0" but will

View File

@@ -186,10 +186,6 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
return StringUtils.toStringArray(this.supportedProtocols);
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void start() {
@@ -219,6 +215,11 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
}
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response,

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -119,10 +118,6 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
}
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void start() {
@@ -144,6 +139,11 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
}
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void handleRequest(HttpServletRequest servletRequest, HttpServletResponse servletResponse)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -64,8 +64,7 @@ public abstract class AbstractXhrTransport implements XhrTransport {
@Override
public List<TransportType> getTransportTypes() {
return (isXhrStreamingDisabled() ?
Collections.singletonList(TransportType.XHR) :
return (isXhrStreamingDisabled() ? Collections.singletonList(TransportType.XHR) :
Arrays.asList(TransportType.XHR_STREAMING, TransportType.XHR));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@ import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
@@ -83,7 +85,7 @@ public class JettyXhrTransport extends AbstractXhrTransport implements Lifecycle
}
}
catch (Exception ex) {
throw new SockJsException("Failed to start " + this, ex);
throw new SockJsException("Failed to start JettyXhrTransport", ex);
}
}
@@ -95,7 +97,7 @@ public class JettyXhrTransport extends AbstractXhrTransport implements Lifecycle
}
}
catch (Exception ex) {
throw new SockJsException("Failed to stop " + this, ex);
throw new SockJsException("Failed to stop JettyXhrTransport", ex);
}
}
@@ -156,9 +158,9 @@ public class JettyXhrTransport extends AbstractXhrTransport implements Lifecycle
private static void addHttpHeaders(Request request, HttpHeaders headers) {
for (String name : headers.keySet()) {
for (String value : headers.get(name)) {
request.header(name, value);
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
for (String value : entry.getValue()) {
request.header(entry.getKey(), value);
}
}
}

View File

@@ -78,7 +78,7 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem
private ScheduledFuture<?> sessionCleanupTask;
private boolean running;
private volatile boolean running;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
package org.springframework.web.socket.sockjs.transport.handler;
import java.util.Map;
import javax.servlet.ServletContext;
import org.springframework.context.Lifecycle;
@@ -53,11 +52,11 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
private final HandshakeHandler handshakeHandler;
private boolean running;
private volatile boolean running;
public WebSocketTransportHandler(HandshakeHandler handshakeHandler) {
Assert.notNull(handshakeHandler, "handshakeHandler must not be null");
Assert.notNull(handshakeHandler, "HandshakeHandler must not be null");
this.handshakeHandler = handshakeHandler;
}
@@ -78,10 +77,6 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
}
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public void start() {
@@ -103,6 +98,12 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
}
}
@Override
public boolean isRunning() {
return this.running;
}
@Override
public boolean checkSessionType(SockJsSession session) {
return session instanceof WebSocketServerSockJsSession;