Class EventSource
- java.lang.Object
-
- com.launchdarkly.eventsource.EventSource
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class EventSource extends java.lang.Object implements java.io.CloseableA client for the Server-Sent Events (SSE) protocol.Instances are always configured and constructed with
EventSource.Builder. The client is created in an inactive state and will not connect until you callstart().Note that although
EventSourceis named after the JavaScript API that is described in the SSE specification, its behavior is not identical to standard web browser implementations of EventSource, specifically in terms of failure/reconnection behavior: it will automatically retry (with a backoff delay) for some error conditions where a browser will not retry. It also supports request configuration options (such as request headers and method) that the browser EventSource does not support. However, its interpretation of the stream data is fully conformant with the SSE specification, unless you use the opt-in modeEventSource.Builder.streamEventData(boolean)which allows for greater efficiency in some use cases but has some behavioral constraints.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classEventSource.BuilderBuilder forEventSource.static interfaceEventSource.RequestTransformerInterface for an object that can modify the network request that the EventSource will make.
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_BACKOFF_RESET_THRESHOLD_MILLISThe default value forEventSource.Builder.backoffResetThreshold(long, TimeUnit): 60 seconds.static longDEFAULT_CONNECT_TIMEOUT_MILLISThe default value forEventSource.Builder.connectTimeout(long, TimeUnit): 10 seconds.static longDEFAULT_MAX_RECONNECT_TIME_MILLISThe default value forEventSource.Builder.maxReconnectTime(long, TimeUnit): 30 seconds.static intDEFAULT_READ_BUFFER_SIZEThe default value forEventSource.Builder.readBufferSize(int).static longDEFAULT_READ_TIMEOUT_MILLISThe default value forEventSource.Builder.readTimeout(long, TimeUnit): 5 minutes.static longDEFAULT_RECONNECT_TIME_MILLISThe default value forEventSource.Builder.reconnectTime(long, TimeUnit): 1 second.static longDEFAULT_WRITE_TIMEOUT_MILLISThe default value forEventSource.Builder.writeTimeout(long, TimeUnit): 5 seconds.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanawaitClosed(long timeout, java.util.concurrent.TimeUnit timeUnit)Block until all underlying threads have terminated and resources have been released.voidclose()Drops the current stream connection (if any) and permanently shuts down the EventSource.okhttp3.HttpUrlgetHttpUrl()Returns the current stream endpoint as an OkHttp HttpUrl.java.lang.StringgetLastEventId()Returns the ID value, if any, of the last known event.ReadyStategetState()Returns an enum indicating the current status of the connection.java.net.URIgetUri()Returns the current stream endpoint as a java.net.URI.voidrestart()Drops the current stream connection (if any) and attempts to reconnect.voidstart()Attempts to connect to the remote event source if not already connected.
-
-
-
Field Detail
-
DEFAULT_RECONNECT_TIME_MILLIS
public static final long DEFAULT_RECONNECT_TIME_MILLIS
The default value forEventSource.Builder.reconnectTime(long, TimeUnit): 1 second.- See Also:
- Constant Field Values
-
DEFAULT_MAX_RECONNECT_TIME_MILLIS
public static final long DEFAULT_MAX_RECONNECT_TIME_MILLIS
The default value forEventSource.Builder.maxReconnectTime(long, TimeUnit): 30 seconds.- See Also:
- Constant Field Values
-
DEFAULT_CONNECT_TIMEOUT_MILLIS
public static final long DEFAULT_CONNECT_TIMEOUT_MILLIS
The default value forEventSource.Builder.connectTimeout(long, TimeUnit): 10 seconds.- See Also:
- Constant Field Values
-
DEFAULT_WRITE_TIMEOUT_MILLIS
public static final long DEFAULT_WRITE_TIMEOUT_MILLIS
The default value forEventSource.Builder.writeTimeout(long, TimeUnit): 5 seconds.- See Also:
- Constant Field Values
-
DEFAULT_READ_TIMEOUT_MILLIS
public static final long DEFAULT_READ_TIMEOUT_MILLIS
The default value forEventSource.Builder.readTimeout(long, TimeUnit): 5 minutes.- See Also:
- Constant Field Values
-
DEFAULT_BACKOFF_RESET_THRESHOLD_MILLIS
public static final long DEFAULT_BACKOFF_RESET_THRESHOLD_MILLIS
The default value forEventSource.Builder.backoffResetThreshold(long, TimeUnit): 60 seconds.- See Also:
- Constant Field Values
-
DEFAULT_READ_BUFFER_SIZE
public static final int DEFAULT_READ_BUFFER_SIZE
The default value forEventSource.Builder.readBufferSize(int).- See Also:
- Constant Field Values
-
-
Method Detail
-
start
public void start()
Attempts to connect to the remote event source if not already connected. This method returns immediately; the connection happens on a worker thread.
-
restart
public void restart()
Drops the current stream connection (if any) and attempts to reconnect.This method returns immediately after dropping the current connection; the reconnection happens on a worker thread.
If a connection attempt is already in progress but has not yet connected, or if
close()has previously been called, this method has no effect. Ifstart()has never been called, it is the same as callingstart().
-
getState
public ReadyState getState()
Returns an enum indicating the current status of the connection.- Returns:
- a
ReadyStatevalue
-
close
public void close()
Drops the current stream connection (if any) and permanently shuts down the EventSource.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
awaitClosed
public boolean awaitClosed(long timeout, java.util.concurrent.TimeUnit timeUnit) throws java.lang.InterruptedExceptionBlock until all underlying threads have terminated and resources have been released.- Parameters:
timeout- maximum time to wait for everything to shut down, in whatever time unit is specified bytimeUnittimeUnit- the time unit, orTimeUnit.MILLISECONDSif null- Returns:
trueif all thread pools terminated within the specified timeout,falseotherwise.- Throws:
java.lang.InterruptedException- if this thread is interrupted while blocking
-
getLastEventId
public java.lang.String getLastEventId()
Returns the ID value, if any, of the last known event.This can be set initially with
EventSource.Builder.lastEventId(String), and is updated whenever an event is received that has an ID. Whether event IDs are supported depends on the server; it may ignore this value.- Returns:
- the last known event ID, or null
- Since:
- 2.0.0
- See Also:
EventSource.Builder.lastEventId(String)
-
getHttpUrl
public okhttp3.HttpUrl getHttpUrl()
Returns the current stream endpoint as an OkHttp HttpUrl.- Returns:
- the endpoint URL
- Since:
- 1.9.0
- See Also:
getUri()
-
getUri
public java.net.URI getUri()
Returns the current stream endpoint as a java.net.URI.- Returns:
- the endpoint URI
- See Also:
getHttpUrl()
-
-