Package com.jsgalactic.axiom.http
Interface EventSink
public interface EventSink
The destination of a server-sent event stream (see
Response.sse(com.jsgalactic.axiom.http.EventStreamBody)). It has the lifecycle
and the failure behavior of the BodyWriter it wraps: valid only while the EventStreamBody runs, blocking under backpressure, and throwing a StreamAbortedException
once the stream cannot be written. Each call sends one complete event in a single write.-
Method Summary
Modifier and TypeMethodDescriptionlongReturns the number of bytes sent so far, counted against the stream's byte cap.voidSends a comment, which clients ignore.default voidSends a comment that keeps the connection from looking idle to clients and intermediaries.static EventSinkof(BodyWriter out) Wraps a body writer as an event sink.default voidonShutdown(Runnable action) Runs an action once when shutdown begins; seeBodyWriter.onShutdown(Runnable).voidsend(ServerSentEvent event) Sends an event.default voidSends an event that has only data.default booleanReports whether the listener has started to shut down; seeBodyWriter.shutdownRequested().
-
Method Details
-
send
Sends an event.- Parameters:
event- the event- Throws:
StreamAbortedException- if the stream was abortedIOException- if the stream cannot be written for another reason
-
send
Sends an event that has only data.- Parameters:
data- text of the event; line breaks become separatedata:lines- Throws:
StreamAbortedException- if the stream was abortedIOException- if the stream cannot be written for another reason
-
comment
Sends a comment, which clients ignore. Line breaks in the text become separate comment lines, so the text cannot inject fields.- Parameters:
text- comment text- Throws:
StreamAbortedException- if the stream was abortedIOException- if the stream cannot be written for another reason
-
keepAlive
Sends a comment that keeps the connection from looking idle to clients and intermediaries. Call it at an interval shorter than their idle timeouts when events are rare. It also discovers a client that has gone away, by failing with aStreamAbortedException.- Throws:
StreamAbortedException- if the stream was abortedIOException- if the stream cannot be written for another reason
-
bytesWritten
long bytesWritten()Returns the number of bytes sent so far, counted against the stream's byte cap.- Returns:
- bytes written
-
shutdownRequested
default boolean shutdownRequested()Reports whether the listener has started to shut down; seeBodyWriter.shutdownRequested(). An event stream that sees it should send a last event and return, which ends the stream cleanly; browsers then reconnect after theretrydelay.- Returns:
- true once shutdown has begun
-
onShutdown
Runs an action once when shutdown begins; seeBodyWriter.onShutdown(Runnable).- Parameters:
action- what to run; quick and non-blocking
-
of
Wraps a body writer as an event sink.- Parameters:
out- destination of the encoded events- Returns:
- a sink writing UTF-8 encoded events to it
-