Class MessageEvent
- java.lang.Object
-
- com.launchdarkly.eventsource.MessageEvent
-
- All Implemented Interfaces:
StreamEvent
public class MessageEvent extends java.lang.Object implements StreamEvent
Information about a message received from the stream.The properties of a message are defined by the SSE specification. Every message has an event name set by the "event:" field (
DEFAULT_EVENT_NAME
if not specified), a data string set by "date:" fields, an optional last event ID string (set by the "id:" field and defaulting to the same value until it is set again), and an origin URI (always the same stream URI).
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_EVENT_NAME
The default value ofgetEventName()
for all SSE messages that did not have anevent
field.
-
Constructor Summary
Constructors Constructor Description MessageEvent(java.lang.String data)
Simple constructor with event data only, using the default event name.MessageEvent(java.lang.String eventName, java.io.Reader dataReader, java.lang.String lastEventId, java.net.URI origin)
Constructs a new instance with lazy-loading behavior.MessageEvent(java.lang.String eventName, java.lang.String data)
Constructs a new instance.MessageEvent(java.lang.String eventName, java.lang.String data, java.lang.String lastEventId, java.net.URI origin)
Constructs a new instance.MessageEvent(java.lang.String data, java.lang.String lastEventId, java.net.URI origin)
Constructs a new instance with the default event name.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Indicates that you are finished with this message.boolean
equals(java.lang.Object o)
java.lang.String
getData()
Returns the event data as a string.java.io.Reader
getDataReader()
Returns a single-useReader
for consuming the event data.java.lang.String
getEventName()
Returns the event name.java.lang.String
getLastEventId()
Returns the event ID, if any.java.net.URI
getOrigin()
Returns the endpoint of the stream that generated the event.int
hashCode()
boolean
isStreamingData()
Returnstrue
if this event was dispatched with streaming data behavior rather than pre-read data.java.lang.String
toString()
-
-
-
Field Detail
-
DEFAULT_EVENT_NAME
public static final java.lang.String DEFAULT_EVENT_NAME
The default value ofgetEventName()
for all SSE messages that did not have anevent
field. This constant is defined in the SSE specification.- Since:
- 2.6.0
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MessageEvent
public MessageEvent(java.lang.String data)
Simple constructor with event data only, using the default event name.This constructor assumes that the event data has been fully read into memory as a String.
- Parameters:
data
- the event data; if null, will be changed to an empty string
-
MessageEvent
public MessageEvent(java.lang.String data, java.lang.String lastEventId, java.net.URI origin)
Constructs a new instance with the default event name.This constructor assumes that the event data has been fully read into memory as a String.
- Parameters:
data
- the event data; if null, will be changed to an empty stringlastEventId
- the event ID, or null if noneorigin
- the stream endpoint
-
MessageEvent
public MessageEvent(java.lang.String eventName, java.lang.String data, java.lang.String lastEventId, java.net.URI origin)
Constructs a new instance.This constructor assumes that the event data has been fully read into memory as a String.
- Parameters:
eventName
- the event name; if null,DEFAULT_EVENT_NAME
is useddata
- the event data; if null, will be changed to an empty stringlastEventId
- the event ID, or null if noneorigin
- the stream endpoint- Since:
- 2.6.0
-
MessageEvent
public MessageEvent(java.lang.String eventName, java.io.Reader dataReader, java.lang.String lastEventId, java.net.URI origin)
Constructs a new instance with lazy-loading behavior.This constructor takes a
Reader
instead of a String for the event data. This is an optimization that sometimes allows events to be processed without large buffers. The caller must be careful about using this model because the behavior of the reader is not idempotent; seegetDataReader()
.- Parameters:
eventName
- an object that will provide the event name if requesteddataReader
- aReader
for consuming the event datalastEventId
- an object that will provide the last event ID if requestedorigin
- the stream endpoint- Since:
- 2.6.0
- See Also:
getDataReader()
-
MessageEvent
public MessageEvent(java.lang.String eventName, java.lang.String data)
Constructs a new instance.- Parameters:
eventName
- the event namedata
- the event data, if any
-
-
Method Detail
-
getEventName
public java.lang.String getEventName()
Returns the event name. This is the value of theevent
field in the SSE message, or, if there was none, the constantDEFAULT_EVENT_NAME
.- Returns:
- the event name
- Since:
- 2.6.0
-
getData
public java.lang.String getData()
Returns the event data as a string.The format of event data is described in the SSE specification. Every event has at least one line with a
data
ordata:
prefix. After removing the prefix, multiple lines are concatenated with a separator of'\n'
.If you have set the
EventSource.Builder.streamEventData(boolean)
option totrue
to enable streaming delivery of event data to your handler without buffering the entire event, you should usegetDataReader()
instead ofgetData()
. CallinggetData()
in this mode would defeat the purpose by causing all of the data to be read at once. However, if you do this,getData()
memoizes the result so that calling it repeatedly does not try to read the stream again.The method will never return
null
; every event has data, even if the data is empty (zero length).- Returns:
- the data string
-
getDataReader
public java.io.Reader getDataReader()
Returns a single-useReader
for consuming the event data.This is meant to be used if you have set the option
EventSource.Builder.streamEventData(boolean)
totrue
, indicating that you want to consume the data with a streaming approach, rather than buffering the full event in memory. In this mode,getDataReader()
returns aReader
that consumes the event data while it is being received. The format is the same as described ingetData()
(for instance, if the data was sent in multiple lines, the lines are separated by'\n'
). Because thisReader
is connected directly to the HTTP response stream, it is only valid until you read the next message.See
EventSource.Builder.streamEventData(boolean)
for more details and important limitations of the streaming mode.If you have not set that option, then the returned
Reader
simply provides the same already-buffered data that would be available fromgetData()
.This
Reader
can be used only once; if you have already consumed the data, further calls togetDataReader()
will return the same instance that will not read any more.The method will never return
null
; every event has data, even if the data is empty (zero length).If the stream connection is closed before a complete SSE message has been received (that is, before the usual blank line that would terminate a message), then instead of a normal EOF, the Reader will throw a
StreamClosedWithIncompleteMessageException
. If this happens, the application should generally discard the entireMessageEvent
and not try to process it further.- Returns:
- a reader for the event data
- Since:
- 2.6.0
-
getLastEventId
public java.lang.String getLastEventId()
Returns the event ID, if any.- Returns:
- the event ID or null
-
getOrigin
public java.net.URI getOrigin()
Returns the endpoint of the stream that generated the event.- Returns:
- the stream URI
-
isStreamingData
public boolean isStreamingData()
Returnstrue
if this event was dispatched with streaming data behavior rather than pre-read data.This is only the case if you have set the
EventSource.Builder.streamEventData(boolean)
option totrue
.- Returns:
- true if the event has streaming data
- Since:
- 2.6.0
-
close
public void close()
Indicates that you are finished with this message.This method is only relevant if you are using streaming data mode. It tells EventSource that you will not be using the
getDataReader()
stream any more and that any remaining data for this event should be skipped.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-