parent
cd10171f98
commit
4eabe29b9a
|
|
@ -25,11 +25,11 @@ import org.springframework.context.SmartLifecycle;
|
|||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* A base class for WebSocket connection managers. Provides a declarative style of
|
||||
* connecting to a WebSocket server given a URI to connect to. The connection occurs when
|
||||
* the Spring ApplicationContext is refreshed, if the {@link #autoStartup} property is set
|
||||
* to {@code true}, or if set to {@code false}, the {@link #start()} and #stop methods can
|
||||
* be invoked manually.
|
||||
* Base class for a connection manager that automates the process of connecting
|
||||
* to a WebSocket server with the Spring ApplicationContext lifecycle. Connects
|
||||
* to a WebSocket server on {@link #start()} and disconnects on {@link #stop()}.
|
||||
* If {@link #setAutoStartup(boolean)} is set to {@code true} this will be done
|
||||
* automatically when the Spring {@code ApplicationContext} is refreshed.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
|
|
@ -163,11 +163,19 @@ public abstract class ConnectionManagerSupport implements SmartLifecycle {
|
|||
return this.running;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the connection is open/{@code true} or closed/{@code false}.
|
||||
*/
|
||||
public abstract boolean isConnected();
|
||||
|
||||
/**
|
||||
* Subclasses implement this to actually establish the connection.
|
||||
*/
|
||||
protected abstract void openConnection();
|
||||
|
||||
/**
|
||||
* Subclasses implement this to close the connection.
|
||||
*/
|
||||
protected abstract void closeConnection() throws Exception;
|
||||
|
||||
protected abstract boolean isConnected();
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
|
|
@ -29,10 +29,9 @@ import org.springframework.web.socket.WebSocketSession;
|
|||
import org.springframework.web.socket.handler.LoggingWebSocketHandlerDecorator;
|
||||
|
||||
/**
|
||||
* A WebSocket connection manager that is given a URI, a {@link WebSocketClient}, and a
|
||||
* {@link WebSocketHandler}, connects to a WebSocket server through {@link #start()} and
|
||||
* {@link #stop()} methods. If {@link #setAutoStartup(boolean)} is set to {@code true}
|
||||
* this will be done automatically when the Spring ApplicationContext is refreshed.
|
||||
* WebSocket {@link ConnectionManagerSupport connection manager} that connects
|
||||
* to the server via {@link WebSocketClient} and handles the session with a
|
||||
* {@link WebSocketHandler}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
|
|
@ -46,7 +45,7 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
|
|||
@Nullable
|
||||
private WebSocketSession webSocketSession;
|
||||
|
||||
private WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
|
||||
private final WebSocketHttpHeaders headers = new WebSocketHttpHeaders();
|
||||
|
||||
|
||||
public WebSocketConnectionManager(WebSocketClient client,
|
||||
|
|
@ -58,14 +57,6 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decorate the WebSocketHandler provided to the class constructor.
|
||||
* <p>By default {@link LoggingWebSocketHandlerDecorator} is added.
|
||||
*/
|
||||
protected WebSocketHandler decorateWebSocketHandler(WebSocketHandler handler) {
|
||||
return new LoggingWebSocketHandlerDecorator(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sub-protocols to use. If configured, specified sub-protocols will be
|
||||
* requested in the handshake through the {@code Sec-WebSocket-Protocol} header. The
|
||||
|
|
@ -130,6 +121,11 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
|
|||
super.stopInternal();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
return (this.webSocketSession != null && this.webSocketSession.isOpen());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void openConnection() {
|
||||
if (logger.isInfoEnabled()) {
|
||||
|
|
@ -159,9 +155,12 @@ public class WebSocketConnectionManager extends ConnectionManagerSupport {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
return (this.webSocketSession != null && this.webSocketSession.isOpen());
|
||||
/**
|
||||
* Decorate the WebSocketHandler provided to the class constructor.
|
||||
* <p>By default {@link LoggingWebSocketHandlerDecorator} is added.
|
||||
*/
|
||||
protected WebSocketHandler decorateWebSocketHandler(WebSocketHandler handler) {
|
||||
return new LoggingWebSocketHandlerDecorator(handler);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
|
|
@ -31,11 +31,9 @@ import org.springframework.web.socket.client.ConnectionManagerSupport;
|
|||
import org.springframework.web.socket.handler.BeanCreatingHandlerProvider;
|
||||
|
||||
/**
|
||||
* A WebSocket connection manager that is given a URI, a
|
||||
* {@link javax.websocket.ClientEndpoint}-annotated endpoint, connects to a
|
||||
* WebSocket server through the {@link #start()} and {@link #stop()} methods.
|
||||
* If {@link #setAutoStartup(boolean)} is set to {@code true} this will be
|
||||
* done automatically when the Spring ApplicationContext is refreshed.
|
||||
* WebSocket {@link ConnectionManagerSupport connection manager} that connects
|
||||
* to the server via {@link WebSocketContainer} and handles the session with an
|
||||
* {@link javax.websocket.ClientEndpoint @ClientEndpoint} endpoint.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
|
|
@ -101,6 +99,12 @@ public class AnnotatedEndpointConnectionManager extends ConnectionManagerSupport
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
Session session = this.session;
|
||||
return (session != null && session.isOpen());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void openConnection() {
|
||||
this.taskExecutor.execute(() -> {
|
||||
|
|
@ -135,10 +139,4 @@ public class AnnotatedEndpointConnectionManager extends ConnectionManagerSupport
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isConnected() {
|
||||
Session session = this.session;
|
||||
return (session != null && session.isOpen());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
|
|
@ -39,10 +39,9 @@ import org.springframework.web.socket.client.ConnectionManagerSupport;
|
|||
import org.springframework.web.socket.handler.BeanCreatingHandlerProvider;
|
||||
|
||||
/**
|
||||
* A WebSocket connection manager that is given a URI, an {@link Endpoint}, connects to a
|
||||
* WebSocket server through the {@link #start()} and {@link #stop()} methods. If
|
||||
* {@link #setAutoStartup(boolean)} is set to {@code true} this will be done automatically
|
||||
* when the Spring ApplicationContext is refreshed.
|
||||
* WebSocket {@link ConnectionManagerSupport connection manager} that connects
|
||||
* to the server via {@link WebSocketContainer} and handles the session with an
|
||||
* {@link Endpoint}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
|
|
@ -133,6 +132,12 @@ public class EndpointConnectionManager extends ConnectionManagerSupport implemen
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
Session session = this.session;
|
||||
return (session != null && session.isOpen());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void openConnection() {
|
||||
this.taskExecutor.execute(() -> {
|
||||
|
|
@ -168,10 +173,4 @@ public class EndpointConnectionManager extends ConnectionManagerSupport implemen
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isConnected() {
|
||||
Session session = this.session;
|
||||
return (session != null && session.isOpen());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue