Class JettyWebSocketClient
- java.lang.Object
- org.springframework.web.socket.client.AbstractWebSocketClient
- org.springframework.web.socket.client.jetty.JettyWebSocketClient
- All Implemented Interfaces:
Lifecycle
,WebSocketClient
public class JettyWebSocketClient extends AbstractWebSocketClient implements Lifecycle
Initiates WebSocket requests to a WebSocket server programmatically through the Jetty WebSocket API.As of 4.1 this class implements
Lifecycle
rather thanSmartLifecycle
. UseWebSocketConnectionManager
instead to auto-start a WebSocket connection.- Since:
- 4.0
- Author:
- Rossen Stoyanchev
Field Summary
Fields inherited from class org.springframework.web.socket.client.AbstractWebSocketClient
logger
Constructor Summary
Constructors Constructor Description JettyWebSocketClient()
Default constructor that creates an instance ofWebSocketClient
.JettyWebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient client)
Constructor that accepts an existingWebSocketClient
instance.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ListenableFuture<WebSocketSession>
doHandshake(WebSocketHandler webSocketHandler, String uriTemplate, Object... uriVars)
ListenableFuture<WebSocketSession>
doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String,Object> attributes)
Perform the actual handshake to establish a connection to the server.AsyncListenableTaskExecutor
getTaskExecutor()
Return the configuredTaskExecutor
.protected Principal
getUser()
Return the user to make available throughWebSocketSession.getPrincipal()
.boolean
isRunning()
Check whether this component is currently running.void
setTaskExecutor(AsyncListenableTaskExecutor taskExecutor)
Set anAsyncListenableTaskExecutor
to use when opening connections.void
start()
Start this component.void
stop()
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.Methods inherited from class org.springframework.web.socket.client.AbstractWebSocketClient
assertUri, doHandshake
Constructor Detail
JettyWebSocketClient
public JettyWebSocketClient()
Default constructor that creates an instance ofWebSocketClient
.
JettyWebSocketClient
public JettyWebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient client)
Constructor that accepts an existingWebSocketClient
instance.
Method Detail
setTaskExecutor
public void setTaskExecutor(@Nullable AsyncListenableTaskExecutor taskExecutor)
Set anAsyncListenableTaskExecutor
to use when opening connections. If this property is set tonull
, calls to any of thedoHandshake
methods will block until the connection is established.By default an instance of
SimpleAsyncTaskExecutor
is used.
getTaskExecutor
@Nullable public AsyncListenableTaskExecutor getTaskExecutor()
Return the configuredTaskExecutor
.
start
public void start()
Description copied from interface:Lifecycle
Start this component.Should not throw an exception if the component is already running.
In the case of a container, this will propagate the start signal to all components that apply.
- Specified by:
start
in interfaceLifecycle
- See Also:
SmartLifecycle.isAutoStartup()
stop
public void stop()
Description copied from interface:Lifecycle
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementingSmartLifecycle
and itsstop(Runnable)
variant when asynchronous stop behavior is necessary.Note that this stop notification is not guaranteed to come before destruction: On regular shutdown,
Lifecycle
beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.Should not throw an exception if the component is not running (not started yet).
In the case of a container, this will propagate the stop signal to all components that apply.
- Specified by:
stop
in interfaceLifecycle
- See Also:
SmartLifecycle.stop(Runnable)
,DisposableBean.destroy()
isRunning
public boolean isRunning()
Description copied from interface:Lifecycle
Check whether this component is currently running.In the case of a container, this will return
true
only if all components that apply are currently running.
doHandshake
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler, String uriTemplate, Object... uriVars)
- Specified by:
doHandshake
in interfaceWebSocketClient
- Overrides:
doHandshake
in classAbstractWebSocketClient
doHandshakeInternal
public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String,Object> attributes)
Description copied from class:AbstractWebSocketClient
Perform the actual handshake to establish a connection to the server.- Specified by:
doHandshakeInternal
in classAbstractWebSocketClient
- Parameters:
wsHandler
- the client-side handler for WebSocket messagesheaders
- the HTTP headers to use for the handshake, with unwanted (forbidden) headers filtered out (nevernull
)uri
- the target URI for the handshake (nevernull
)protocols
- requested sub-protocols, or an empty listextensions
- requested WebSocket extensions, or an empty listattributes
- the attributes to associate with the WebSocketSession, i.e. viaWebSocketSession.getAttributes()
; currently always an empty map.- Returns:
- the established WebSocket session wrapped in a ListenableFuture.
getUser
@Nullable protected Principal getUser()
Return the user to make available throughWebSocketSession.getPrincipal()
. By default this method returnsnull