Add Lifecycle to SockJsClient and Transport types
Issue: SPR-10797
This commit is contained in:
@@ -23,6 +23,7 @@ import org.eclipse.jetty.client.api.Response;
|
||||
import org.eclipse.jetty.client.util.StringContentProvider;
|
||||
import org.eclipse.jetty.http.HttpFields;
|
||||
import org.eclipse.jetty.http.HttpMethod;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -59,7 +60,7 @@ import java.util.Enumeration;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class JettyXhrTransport extends AbstractXhrTransport implements XhrTransport {
|
||||
public class JettyXhrTransport extends AbstractXhrTransport implements XhrTransport, Lifecycle {
|
||||
|
||||
private final HttpClient httpClient;
|
||||
|
||||
@@ -74,6 +75,35 @@ public class JettyXhrTransport extends AbstractXhrTransport implements XhrTransp
|
||||
return this.httpClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
try {
|
||||
if (!this.httpClient.isRunning()) {
|
||||
this.httpClient.start();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new SockJsException("Failed to start " + this, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
try {
|
||||
if (this.httpClient.isRunning()) {
|
||||
this.httpClient.stop();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new SockJsException("Failed to stop " + this, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return this.httpClient.isRunning();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ResponseEntity<String> executeInfoRequestInternal(URI infoUrl) {
|
||||
return executeRequest(infoUrl, HttpMethod.GET, getRequestHeaders(), null);
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.socket.sockjs.client;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -50,7 +51,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
* @see <a href="http://sockjs.org">http://sockjs.org</a>
|
||||
* @see org.springframework.web.socket.sockjs.client.Transport
|
||||
*/
|
||||
public class SockJsClient extends AbstractWebSocketClient {
|
||||
public class SockJsClient extends AbstractWebSocketClient implements Lifecycle {
|
||||
|
||||
private static final boolean jackson2Present = ClassUtils.isPresent(
|
||||
"com.fasterxml.jackson.databind.ObjectMapper", SockJsClient.class.getClassLoader());
|
||||
@@ -68,6 +69,8 @@ public class SockJsClient extends AbstractWebSocketClient {
|
||||
|
||||
private final Map<URI, ServerInfo> infoCache = new ConcurrentHashMap<URI, ServerInfo>();
|
||||
|
||||
private volatile boolean running = false;
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@code SockJsClient} with the given transports.
|
||||
@@ -143,6 +146,37 @@ public class SockJsClient extends AbstractWebSocketClient {
|
||||
this.taskScheduler = taskScheduler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
if (!isRunning()) {
|
||||
for (Transport transport : this.transports) {
|
||||
if (transport instanceof Lifecycle) {
|
||||
if (!((Lifecycle) transport).isRunning()) {
|
||||
((Lifecycle) transport).start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (!isRunning()) {
|
||||
for (Transport transport : this.transports) {
|
||||
if (transport instanceof Lifecycle) {
|
||||
if (((Lifecycle) transport).isRunning()) {
|
||||
((Lifecycle) transport).stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return this.running;
|
||||
}
|
||||
|
||||
public void clearServerInfoCache() {
|
||||
this.infoCache.clear();
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.socket.sockjs.client;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.context.Lifecycle;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
import org.springframework.util.concurrent.ListenableFutureCallback;
|
||||
@@ -40,12 +41,14 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class WebSocketTransport implements Transport {
|
||||
public class WebSocketTransport implements Transport, Lifecycle {
|
||||
|
||||
private static Log logger = LogFactory.getLog(WebSocketTransport.class);
|
||||
|
||||
private final WebSocketClient webSocketClient;
|
||||
|
||||
private volatile boolean running = false;
|
||||
|
||||
|
||||
public WebSocketTransport(WebSocketClient webSocketClient) {
|
||||
Assert.notNull(webSocketClient, "'webSocketClient' is required");
|
||||
@@ -60,6 +63,41 @@ public class WebSocketTransport implements Transport {
|
||||
return this.webSocketClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
if (!isRunning()) {
|
||||
if (this.webSocketClient instanceof Lifecycle) {
|
||||
((Lifecycle) this.webSocketClient).start();
|
||||
}
|
||||
else {
|
||||
this.running = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
if (isRunning()) {
|
||||
if (this.webSocketClient instanceof Lifecycle) {
|
||||
((Lifecycle) this.webSocketClient).stop();
|
||||
}
|
||||
else {
|
||||
this.running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
if (this.webSocketClient instanceof Lifecycle) {
|
||||
return ((Lifecycle) this.webSocketClient).isRunning();
|
||||
}
|
||||
else {
|
||||
return this.running;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ListenableFuture<WebSocketSession> connect(TransportRequest request, WebSocketHandler handler) {
|
||||
final SettableListenableFuture<WebSocketSession> future = new SettableListenableFuture<WebSocketSession>();
|
||||
|
||||
Reference in New Issue
Block a user