Top Description Inners Fields Constructors Methods
io.undertow.server.handlers.sse

public Class ServerSentEventConnection

extends Object
implements Channel, Attachable
Class Inheritance
All Implemented Interfaces
io.undertow.util.Attachable, java.nio.channels.Channel, java.io.Closeable, java.lang.AutoCloseable
Imports
io.undertow.UndertowLogger, io.undertow.connector.PooledByteBuffer, io.undertow.security.api.SecurityContext, io.undertow.security.idm.Account, io.undertow.server.HttpServerExchange, io.undertow.util.Attachable, .AttachmentKey, .AttachmentList, .HeaderMap, org.xnio.ChannelExceptionHandler, .ChannelListener, .ChannelListeners, .IoUtils, .XnioExecutor, org.xnio.channels.StreamSinkChannel, java.io.IOException, java.nio.ByteBuffer, java.nio.channels.Channel, .ClosedChannelException, java.nio.charset.StandardCharsets, java.security.Principal, java.util.ArrayDeque, .ArrayList, .Deque, .HashMap, .List, .Map, .Queue, java.util.concurrent.ConcurrentLinkedDeque, .CopyOnWriteArrayList, .TimeUnit, java.util.concurrent.atomic.AtomicIntegerFieldUpdater

Represents the server side of a Server Sent Events connection. The class implements Attachable, which provides access to the underlying exchanges attachments.
Author
Stuart Douglas

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
private static class
private class

Field Summary

Modifier and TypeField and Description
private final Queue<ServerSentEventConnection.SSEData>
private final List<ChannelListener<ServerSentEventConnection>>
private final HttpServerExchange
private final Queue<ServerSentEventConnection.SSEData>
flushingMessages

Messages that have been written to the channel but flush() has failed

private volatile long
private volatile int
private static final AtomicIntegerFieldUpdater<ServerSentEventConnection>
private Map<String, String>
private PooledByteBuffer
private Map<String, Object>
private final Deque<ServerSentEventConnection.SSEData>
private volatile boolean
private final StreamSinkChannel
private XnioExecutor.Key
private final ServerSentEventConnection.SseWriteListener

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public synchronized void
addCloseTask(ChannelListener<ServerSentEventConnection>
The listener to invoke
listener
)

Adds a listener that will be invoked when the channel is closed

public <T> void
addToAttachmentList(AttachmentKey<AttachmentList<T>>
the attachment key
key
,
T
the value to add
value
)

Implements io.undertow.util.Attachable.addToAttachmentList.

Add a value to a list-typed attachment key.

public void
close()

Implements java.nio.channels.Channel.close.

Closes this channel.

private synchronized void
private void
public Account

Returns:

The account that was associated with the SSE request
getAccount
()

public <T> T
getAttachment(AttachmentKey<T>
the attachment key
key
)

Implements io.undertow.util.Attachable.getAttachment.

Get an attachment value.

public <T> List<T>
getAttachmentList(AttachmentKey<? extends List<T>>
the attachment key
key
)

Implements io.undertow.util.Attachable.getAttachmentList.

Gets a list attachment value.

public long

Returns:

The keep alive time
getKeepAliveTime
()

public String
public Principal

Returns:

The principal that was associated with the SSE request
getPrincipal
()

public Map<String, Object>
public Map<String, Deque<String>>

Returns:

the query parameters
getQueryParameters
()

public String

Returns:

the query string
getQueryString
()

public HeaderMap

Returns:

The request headers from the initial request that opened this connection
getRequestHeaders
()

public String

Returns:

The request URI from the initial request that opened this connection
getRequestURI
()

public HeaderMap

Returns:

The response headers from the initial request that opened this connection
getResponseHeaders
()

private void
public boolean
isOpen()

Implements java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

public <T> T
putAttachment(AttachmentKey<T>
the attachment key
key
,
T
the new value
value
)

Implements io.undertow.util.Attachable.putAttachment.

Set an attachment value.

public <T> T
removeAttachment(AttachmentKey<T>
the attachment key
key
)

Implements io.undertow.util.Attachable.removeAttachment.

Remove an attachment, returning its previous value.

public void
send(String
The event data
data
)

Sends an event to the remote client

public void
send(String
The event data
data
,
ServerSentEventConnection.EventCallback
A callback that is notified on Success or failure
callback
)

Sends an event to the remote client

public synchronized void
send(String
The event data
data
,
String
The event name
event
,
String
The event ID
id
,
ServerSentEventConnection.EventCallback
A callback that is notified on Success or failure
callback
)

Sends an event to the remote client

public void
sendRetry(long
The retry time in milliseconds
retry
)

Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.

public synchronized void
sendRetry(long
The retry time in milliseconds
retry
,
ServerSentEventConnection.EventCallback
The callback that is notified on success or failure
callback
)

Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.

public void
setKeepAliveTime(long
The time in milliseconds between keep alive messaged
keepAliveTime
)

Sets the keep alive time in milliseconds.

public void
setParameter(String name, String value)

public void
shutdown()

execute a graceful shutdown once all data has been sent

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferedback to summary
private final Queue<ServerSentEventConnection.SSEData> buffered
closeTasksback to summary
private final List<ChannelListener<ServerSentEventConnection>> closeTasks
exchangeback to summary
private final HttpServerExchange exchange
flushingMessagesback to summary
private final Queue<ServerSentEventConnection.SSEData> flushingMessages

Messages that have been written to the channel but flush() has failed

keepAliveTimeback to summary
private volatile long keepAliveTime
openback to summary
private volatile int open
openUpdaterback to summary
private static final AtomicIntegerFieldUpdater<ServerSentEventConnection> openUpdater
parametersback to summary
private Map<String, String> parameters
pooledback to summary
private PooledByteBuffer pooled
propertiesback to summary
private Map<String, Object> properties
queueback to summary
private final Deque<ServerSentEventConnection.SSEData> queue
shutdownback to summary
private volatile boolean shutdown
sinkback to summary
private final StreamSinkChannel sink
timerKeyback to summary
private XnioExecutor.Key timerKey
writeListenerback to summary
private final ServerSentEventConnection.SseWriteListener writeListener

Constructor Detail

ServerSentEventConnectionback to summary
public ServerSentEventConnection(HttpServerExchange exchange, StreamSinkChannel sink)

Method Detail

addCloseTaskback to summary
public synchronized void addCloseTask(ChannelListener<ServerSentEventConnection> listener)

Adds a listener that will be invoked when the channel is closed

Parameters
listener:ChannelListener<ServerSentEventConnection>

The listener to invoke

addToAttachmentListback to summary
public <T> void addToAttachmentList(AttachmentKey<AttachmentList<T>> key, T value)

Implements io.undertow.util.Attachable.addToAttachmentList.

Doc from io.undertow.util.Attachable.addToAttachmentList.

Add a value to a list-typed attachment key. If the key is not mapped, add such a mapping.

Parameters
<T>
the list value type
key:AttachmentKey<AttachmentList<T>>

the attachment key

value:T

the value to add

Annotations
@Override
closeback to summary
public void close() throws IOException

Implements java.nio.channels.Channel.close.

Doc from java.nio.channels.Channel.close.

Closes this channel.

After a channel is closed, any further attempt to invoke I/O operations upon it will cause a ClosedChannelException to be thrown.

If this channel is already closed then invoking this method has no effect.

This method may be invoked at any time. If some other thread has already invoked it, however, then another invocation will block until the first invocation is complete, after which it will return without effect.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
closeback to summary
private synchronized void close(IOException e) throws IOException
fillBufferback to summary
private void fillBuffer()
getAccountback to summary
public Account getAccount()
Returns:Account

The account that was associated with the SSE request

getAttachmentback to summary
public <T> T getAttachment(AttachmentKey<T> key)

Implements io.undertow.util.Attachable.getAttachment.

Doc from io.undertow.util.Attachable.getAttachment.

Get an attachment value. If no attachment exists for this key, null is returned.

Parameters
<T>
the value type
key:AttachmentKey<T>

the attachment key

Returns:T

the value, or null if there is none

Annotations
@Override
getAttachmentListback to summary
public <T> List<T> getAttachmentList(AttachmentKey<? extends List<T>> key)

Implements io.undertow.util.Attachable.getAttachmentList.

Doc from io.undertow.util.Attachable.getAttachmentList.

Gets a list attachment value. If not attachment exists for this key an empty list is returned

Parameters
<T>
the value type
key:AttachmentKey<? extends List<T>>

the attachment key

Returns:List<T>

the value, or an empty list if there is none

Annotations
@Override
getKeepAliveTimeback to summary
public long getKeepAliveTime()
Returns:long

The keep alive time

getParameterback to summary
public String getParameter(String name)
getPrincipalback to summary
public Principal getPrincipal()
Returns:Principal

The principal that was associated with the SSE request

getPropertiesback to summary
public Map<String, Object> getProperties()
getQueryParametersback to summary
public Map<String, Deque<String>> getQueryParameters()
Returns:Map<String, Deque<String>>

the query parameters

getQueryStringback to summary
public String getQueryString()
Returns:String

the query string

getRequestHeadersback to summary
public HeaderMap getRequestHeaders()
Returns:HeaderMap

The request headers from the initial request that opened this connection

getRequestURIback to summary
public String getRequestURI()
Returns:String

The request URI from the initial request that opened this connection

getResponseHeadersback to summary
public HeaderMap getResponseHeaders()
Returns:HeaderMap

The response headers from the initial request that opened this connection

handleExceptionback to summary
private void handleException(IOException e)
isOpenback to summary
public boolean isOpen()

Implements java.nio.channels.Channel.isOpen.

Doc from java.nio.channels.Channel.isOpen.

Tells whether or not this channel is open.

Returns:boolean

true if, and only if, this channel is open

Annotations
@Override
putAttachmentback to summary
public <T> T putAttachment(AttachmentKey<T> key, T value)

Implements io.undertow.util.Attachable.putAttachment.

Doc from io.undertow.util.Attachable.putAttachment.

Set an attachment value. If an attachment for this key was already set, return the original value. If the value being set is null, the attachment key is removed.

Parameters
<T>
the value type
key:AttachmentKey<T>

the attachment key

value:T

the new value

Returns:T

the old value, or null if there was none

Annotations
@Override
removeAttachmentback to summary
public <T> T removeAttachment(AttachmentKey<T> key)

Implements io.undertow.util.Attachable.removeAttachment.

Doc from io.undertow.util.Attachable.removeAttachment.

Remove an attachment, returning its previous value.

Parameters
<T>
the value type
key:AttachmentKey<T>

the attachment key

Returns:T

the old value, or null if there was none

Annotations
@Override
sendback to summary
public void send(String data)

Sends an event to the remote client

Parameters
data:String

The event data

sendback to summary
public void send(String data, ServerSentEventConnection.EventCallback callback)

Sends an event to the remote client

Parameters
data:String

The event data

callback:ServerSentEventConnection.EventCallback

A callback that is notified on Success or failure

sendback to summary
public synchronized void send(String data, String event, String id, ServerSentEventConnection.EventCallback callback)

Sends an event to the remote client

Parameters
data:String

The event data

event:String

The event name

id:String

The event ID

callback:ServerSentEventConnection.EventCallback

A callback that is notified on Success or failure

sendRetryback to summary
public void sendRetry(long retry)

Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.

Parameters
retry:long

The retry time in milliseconds

sendRetryback to summary
public synchronized void sendRetry(long retry, ServerSentEventConnection.EventCallback callback)

Sends the 'retry' message to the client, instructing it how long to wait before attempting a reconnect.

Parameters
retry:long

The retry time in milliseconds

callback:ServerSentEventConnection.EventCallback

The callback that is notified on success or failure

setKeepAliveTimeback to summary
public void setKeepAliveTime(long keepAliveTime)

Sets the keep alive time in milliseconds. If this is larger than zero a ':' message will be sent this often (assuming there is no activity) to keep the connection alive. The spec recommends a value of 15000 (15 seconds).

Parameters
keepAliveTime:long

The time in milliseconds between keep alive messaged

setParameterback to summary
public void setParameter(String name, String value)
shutdownback to summary
public void shutdown()

execute a graceful shutdown once all data has been sent

io.undertow.server.handlers.sse back to summary

public Interface ServerSentEventConnection.EventCallback


Method Summary

Modifier and TypeMethod and Description
public void
done(ServerSentEventConnection
The connection
connection
,
String
The message data
data
,
String
The message event
event
,
String
The message id
id
)

Notification that is called when a message is sucessfully sent

public void
failed(ServerSentEventConnection
The connection
connection
,
String
The message data
data
,
String
The message event
event
,
String
The message id
id
,
IOException
The exception
e
)

Notification that is called when a message send fails.

Method Detail

doneback to summary
public void done(ServerSentEventConnection connection, String data, String event, String id)

Notification that is called when a message is sucessfully sent

Parameters
connection:ServerSentEventConnection

The connection

data:String

The message data

event:String

The message event

id:String

The message id

failedback to summary
public void failed(ServerSentEventConnection connection, String data, String event, String id, IOException e)

Notification that is called when a message send fails.

Parameters
connection:ServerSentEventConnection

The connection

data:String

The message data

event:String

The message event

id:String

The message id

e:IOException

The exception

io.undertow.server.handlers.sse back to summary

private Class ServerSentEventConnection.SSEData

extends Object
Class Inheritance
  • java.lang.Object
  • io.undertow.server.handlers.sse.ServerSentEventConnection.SSEData

Field Summary

Modifier and TypeField and Description
pack-priv final ServerSentEventConnection.EventCallback
pack-priv final String
private int
pack-priv final String
pack-priv final String
private byte[]
private int
pack-priv final long

Constructor Summary

AccessConstructor and Description
private
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

callbackback to summary
pack-priv final ServerSentEventConnection.EventCallback callback
databack to summary
pack-priv final String data
endBufferPositionback to summary
private int endBufferPosition
eventback to summary
pack-priv final String event
idback to summary
pack-priv final String id
leftOverDataback to summary
private byte[] leftOverData
leftOverDataOffsetback to summary
private int leftOverDataOffset
retryback to summary
pack-priv final long retry

Constructor Detail

SSEDataback to summary
private SSEData(String event, String data, String id, ServerSentEventConnection.EventCallback callback)
SSEDataback to summary
private SSEData(long retry, ServerSentEventConnection.EventCallback callback)
io.undertow.server.handlers.sse back to summary

private Class ServerSentEventConnection.SseWriteListener

extends Object
implements ChannelListener<StreamSinkChannel>
Class Inheritance
  • java.lang.Object
  • io.undertow.server.handlers.sse.ServerSentEventConnection.SseWriteListener
All Implemented Interfaces
org.xnio.ChannelListener, java.util.EventListener

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
handleEvent(StreamSinkChannel
the channel event
channel
)

Implements org.xnio.ChannelListener.handleEvent.

Handle the event on this channel.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

SseWriteListenerback to summary
private SseWriteListener()

Method Detail

handleEventback to summary
public void handleEvent(StreamSinkChannel channel)

Implements org.xnio.ChannelListener.handleEvent.

Doc from org.xnio.ChannelListener.handleEvent.

Handle the event on this channel.

Parameters
channel:StreamSinkChannel

the channel event

Annotations
@Override