All notable changes to the LaunchDarkly EventSource implementation for Java will be documented in this file. This project adheres to Semantic Versioning.
- Bumping Guava version to incorporate CVE fixes.
This release is identical to 4.0.1. It exists for Semantic Versioning compliance: a new API was added (the StreamClosedWithIncompleteMessageException
class) since 4.0.0, therefore the next version number should have been 4.1.0 rather than 4.0.1.
StreamClosedWithIncompleteMessageException
(see below).
- When using streaming data mode (see
EventSource.Builder.streamEventData
), if the stream connection was lost before theMessageEvent
was fully read-- that is, before encountering a blank line-- theReader
returned byMessageEvent.getDataReader()
was treating this as a regular EOF condition. That was incorrect; the SSE specification says that in such a case, the incomplete message is invalid and its contents should not be used. Therefore, in this case reading from theReader
will throw aStreamClosedWithIncompleteMessageException
. The caller should handle this by simply throwing away theMessageEvent
.
This release revises the implementation of EventSource
so that it does not create its own worker threads. The methods for starting and reading a stream now perform synchronous I/O, and the caller is responsible for choosing what thread to start the stream and read the stream on. This makes the core implementation simpler and more efficient, and reduces possibilities for deadlock. If you want to continue using the old asynchronous push model, see the com.launchdarkly.eventsource.background
package.
The configuration API has also been revised to separate out the HTTP functionality and the backoff/retry logic into subcomponents which can be customized or replaced.
EventSource
methodsreadMessage()
,readAnyEvents()
,messages()
, andanyEvents()
for synchronously reading events.EventSource.Builder(ConnectStrategy)
constructor and theConnectStrategy
andHttpConnectStrategy
classes, for customizing connection parameters.EventSource.Builder(URI)
,(URL)
, and(HttpUrl)
constructors, as shortcuts for minimal HTTP configuration.EventSource.Builder.errorStrategy
and theErrorStrategy
class, for customizing error recovery behavior.EventSource.Builder.retryDelayStrategy
and theRetryDelayStrategy
andDefaultRetryDelayStrategy
classes, for customizing or replacing backoff/jitter behavior.StreamEvent
,CommentEvent
,StartedEvent
, andFaultEvent
classes, representing types of information other thanMessageEvent
that you can read synchronously from the stream.- The
com.launchdarkly.eventsource.background
package, containing theBackgroundEventSource
wrapper for emulating the old push model.
EventSource.Builder.restart
is renamed tointerrupt
, to clarify that the restarting of the stream is not done immediately but only happens if you continue trying to read events afterinterrupt()
.EventSource.Builder.backoffResetThreshold
is renamed toretryDelayResetThreshold
, to clarify that it applies to whatever kind ofRetryDelayStrategy
is being used even if that is not the default backoff strategy.EventSource.Builder.reconnectTime
is renamed toretryDelay
, to clarify that it is describing a duration rather than a moment in time.UnsuccessfulResponseException
is renamed toStreamHttpErrorException
, to clarify that it is specifically referring to an HTTP response with an error status.- The implementation of
streamEventData
mode is now simpler and no longer usesPipedInputStream
andPipedOutputStream
internally.
ConnectionHandler
andEventSource.Builder.connectionHandler()
: replaced byErrorStrategy
.EventHandler
and theEventSource.Builder(URI, EventHandler)
constructor: replaced byBackgroundEventHandler
if you are usingBackgroundEventSource
.EventSource.Builder
methodsconnectTimeout
,readTimeout
,writeTimeout
,method
,body
,headers
,proxy
,proxyAuthenticator
, andrequestTransformer
: replaced by methods inHttpConnectStrategy
.EventSource.Builder.maxReconnect
: replaced byDefaultRetryDelayStrategy.maxDelay
.EventSource.Builder.maxEventTasksInFlight
: replaced by an equivalent option inBackgroundEventSource
.
This release expands compatibility of the library with Android by removing Java 8 API usages that are not always available in Android. In effect, it restores the broader compatibility of the 1.x version while preserving the other API/functionality improvements that were added in 2.x. It also removes the previous dependency on SLF4J.
- Every
EventSource
andEventSource
method that previously took ajava.time.Duration
parameter now takes(long, TimeUnit)
parameters instead. This is to allow the library to be used from Android code on older platform versions that do not support thejava.time
types. It undoes an API change that was made in the 2.0.0 release. - SLF4J is no longer a dependency. Logging is done entirely through the com.launchdarkly.logging facade; see
EventSource.Builder.logger
. - Previously, if no logging destination was specified, the default behavior was to send log output to SLF4J. Now, the default behavior is to do no logging. If you still want to use SLF4J, do this (for "log.name", substitute whatever you want the logger name to be in SLF4J):
// import com.launchdarkly.logging.*;
// EventSource.Builder builder =
// new EventSource.Builder(myHandler, streamUri);
builder.logger(
LDLogger.withAdapter(LDSLF4J.adapter(), "log.name")
);
Logger
interface,EventSource.Builder.logger(Logger)
, andEventSource.Builder.loggerBaseName
. See note above on logging.
- Changed jitter logic that used
java.util.Random
to usejava.security.SecureRandom
. Even though in this case it is not being used for any cryptographic purpose, but only to produce a pseudo-random delay, static analysis tools may still report every use ofjava.util.Random
as a security risk by default. The purpose of this change is simply to avoid such warnings; it has no practical effect on the behavior of the library.
- Changed jitter logic that used
java.util.Random
to usejava.security.SecureRandom
. Even though in this case it is not being used for any cryptographic purpose, but only to produce a pseudo-random delay, static analysis tools may still report every use ofjava.util.Random
as a security risk by default. The purpose of this change is simply to avoid such warnings; it has no practical effect on the behavior of the library.
The main purpose of this release is to introduce a new logging facade, com.launchdarkly.logging
, to streamline how logging works in LaunchDarkly Java and Android code. Previously, okhttp-eventsource
used SLF4J for logging by default, but could be made to send output to a Logger
interface of its own; the LaunchDarkly Java SDK used only SLF4J, so developers needed to provide an SLF4J configuration externally; and the LaunchDarkly Android SDK used Timber, but still brought in SLF4J as a transitive dependency of okhttp-eventsource
. In this release, the default behavior is still to use SLF4J, but the logging facade can also be configured programmatically to do simple console logging without SLF4J, or to forward output to another framework such as java.util.logging
, or other destinations. In a future major version release, the default behavior will be changed so that okhttp-eventsource
does not require SLF4J as a dependency.
- An overload of
EventSource.Builder.logger()
that takes acom.launchdarkly.logging.LDLogger
instead of acom.launchdarkly.eventsource.Logger
.
- The overload of
EventSource.Builder.logger()
that takes acom.launchdarkly.eventsource.Logger
. EventSource.Builder.loggerBaseName()
: this method was only relevant to the default behavior of using SLF4J, providing a way to customize what the logger name would be for SLF4J. But when using the new framework, the logger name is built into theLDLogger
instance. For example (having imported thecom.launchdarkly.logging
package):builder.logger(LDLogger.withAdapter(LDSLF4J.adapter(), "my.desired.logger.name"))
- Updated OkHttp dependency to v4.9.3 to get recent fixes, including a security fix.
- The 2.5.0 and 2.6.0 releases mistakenly showed
kotlin-stdlib
as a compile-time dependency inpom.xml
. While this library does use the Kotlin runtime (because the underlying OkHttp client uses Kotlin), that is a transitive dependency and is not needed at compile time.
EventSource.Builder.streamEventData
andEventSource.Builder.expectFields
, for enabling a new event parsing mode in which event data can be consumed directly from the stream without holding it all in memory. This may be useful in applications where individual events are extremely large.MessageEvent.getDataReader
andMessageEvent.isStreamingData
, for use with the new mode described above.MessageEvent.getEventName
, providing access to the event name from the event object; previously the event name was only available as a parameter ofEventHandler.onMessage
.
- Miscellaneous improvements in memory usage and performance when parsing the stream, even in the default mode.
EventSource.Builder.maxTasksInFlight
allows setting a limit on how many asynchronous event handler calls can be queued for dispatch at any given time. (Thanks, thomaslee!)EventSource.awaitClosed
provides confirmation that all asynchronous event handler calls have been completed after stopping theEventSource
. (Thanks, thomaslee!)
- The build has been updated to use Gradle 7.
- The CI build now includes testing in Java 17.
This release fixes a number of SSE spec compliance issues which do not affect usage in the LaunchDarkly Java and Android SDKs, but could be relevant in other use cases.
EventSource.Builder.readBufferSize
- The implementation of stream parsing has been changed. Previously, we were using
BufferedSource
from theokio
library, but that API did not support\r
line terminators (see below). Now we use our own implementation, which is simpler thanBufferedSource
and is optimized for reading text lines in UTF-8. - The CI build now incorporates the cross-platform contract tests defined in https://github.com/launchdarkly/sse-contract-tests to ensure consistent test coverage across different LaunchDarkly SSE implementations.
- The stream parser did not support a
\r
character by itself as a line terminator. The SSE specification requires that\r
,\n
, and\r\n
are all valid. - If an event's
id:
field contains a null character, the whole field should be ignored. - The parser was incorrectly trimming spaces from lines that did not contain a colon, so for instance
data[space]
was being treated as an emptydata
field, when it is really an invalid field name that should be ignored.
- Fixed a bug that caused the connection error handler to be called twice instead of once, with only the second return value being used. The second call would always pass an
EOFException
instead of the original error. The result was that any connection error handler logic that needed to distinguish between different kinds of errors would not work as intended.
- Worker threads might not be shut down after closing the EventSource, if the stream had previously been stopped due to a ConnectionErrorHandler returning
SHUTDOWN
. Now, the threads are stopped as soon as the stream is shut down for any reason. (#51) - Fixed a race condition that could cause
onClosed()
not to be called in some circumstances where it should be called.
EventSource.Builder.logger()
and theLogger
interface allow a custom logging implementation to be used instead of SLF4J.EventSource.Builder.loggerBaseName()
allows customization of the logger name when using the default SLF4J implementation.- Greatly improved unit test coverage; the CI build will now enforce coverage goals (see
CONTRIBUTING.md
).
- Explicitly closing the stream could cause a misleading log line saying that the connection error handler had shut it down.
- Explicitly closing the stream could also cause an unnecessary backoff delay (with a log line about waiting X amount of time) before the stream task actually shut down.
- Fixed a bug that could cause the randomized jitter not to be applied to reconnection delays if the reconnect interval (in milliseconds) was a power of two.
EventSource.Builder.threadPriority()
specifies thatEventSource
should create its worker threads with a specific priority.
EventSource
methodrestart()
(also added in 1.11.0).
- Updated OkHttp version to 4.5.0.
- A build problem caused the 2.0.0 release to have an incorrect
pom.xml
that did not list any dependencies.
EventSource.Builder.lastEventId
(replacesEventSource.setLastEventId
).
- This library now uses OkHttp 4.x and requires Java 8 or above.
- Configuration methods that previously specified a duration in milliseconds now use the
Duration
class.
- In
EventSource
:setHttpUrl
,setLastEventId
,setMaxReconnectTime
,setReconnectionTime
,setUri
(these can only be set in the builder).
- Restored compatibility with Java 7. CI builds now verify that the library can be compiled and tested in Java 7 rather than just building with a target JVM setting of 1.7.
- Bumped OkHttp version to 3.12.12 to avoid a crash on Java 8u252.
- Explicitly closing the stream could also cause an unnecessary backoff delay (with a log line about waiting X amount of time) before the stream task actually shut down.
- Fixed a bug that could cause the randomized jitter not to be applied to reconnection delays if the reconnect interval (in milliseconds) was a power of two.
- New
EventSource
methodrestart()
allows the caller to force a stream connection retry even if no I/O error has happened, using the same backoff behavior that would be used for errors.
- Updated OkHttp version to 3.12.10 (the latest version that still supports Java 7).
- Fixed trailing period in logger name. (#34)
- If you provide your own value for the
Accept
header usingEventSource.Builder.headers()
, it should not also send the defaultAccept: text/event-stream
, but replace it. (#38)
EventSource.Builder.clientBuilderActions()
allows you to modify the OkHttp client options in any way, such as customizing TLS behavior or any other methods supported byOkHttpClient.Builder
.- The CI tests now include end-to-end tests against an embedded HTTP server.
- Removed JSR305 annotations such as
@Nullable
. JSR305 is unsupported and obsolete, and can cause problems in Java 9.
- The
requestTransformer
option allows you to customize the outgoing request in any way you like. (#28) - It is now possible to specify the endpoint as either a
URI
or an OkHttpHttpUrl
. (#29) - The
backoffResetThresholdMs
option controls the new backoff behavior described below. - Added Javadoc comments for all public members.
- The exponential backoff behavior when a stream connection fails has changed as follows. Previously, the backoff delay would increase for each attempt if the connection could not be made at all, or if a read timeout happened; but if a connection was made and then an error (other than a timeout) occurred, the delay would be reset to the minimum value. Now, the delay is only reset if a stream connection is made and remains open for at least
backoffResetThresholdMs
milliseconds. The default for this value is one minute (60000).
- Added
maxReconnectTimeMs(long)
method toEventSource.Builder
to override the default maximum reconnect time of 30 seconds
- Fixed EventSource logger name to match convention
- Ensure background threads are daemon threads
- Don't log IOExceptions when the stream is already being shut down
- Added the ability to connect to an SSE resource using any HTTP method (defaults to GET), and specify a request body.
- Add new handler interface for stopping connection retries after an error
- Ensure that connections are closed completely (#24)
- Ensure that we reconnect with backoff in case of a read timeout