Class SseEvent

java.lang.Object
org.apache.juneau.marshall.sse.SseEvent

@BeanType(properties="event,data,id,retry") public class SseEvent extends Object
Represents a single Server-Sent Event (SSE) frame on the wire.

Each event maps to a sequence of field: value lines terminated by a blank line in the text/event-stream wire format:

event: progress data: {"step":4,"total":10} id: 42 retry: 5000

The payload field data is a pre-serialized String — the SSE serializer does not compose with a delegate marshaller. Callers that need a typed payload should serialize it themselves (e.g. via Json.of(x)) and assign the resulting string to setData(...). The serializer will split that string on \n into multiple data: lines per spec.

Example: build and serialize an event:

SseEvent event = new SseEvent() .setEvent("progress") .setData(Json.of(Map.of("step", 4, "total", 10))) .setId("42") .setRetry(5000L); String wire = Sse.of(event);

Notes:
  • This class is not thread safe.
See Also:
  • Field Details

  • Constructor Details

    • SseEvent

      public SseEvent()
      Constructor.
    • SseEvent

      public SseEvent(String event, String data)
      Constructor.
      Parameters:
      event - The event-type name. Can be null (dispatches as "message").
      data - The payload string. Can be null (no data: lines emitted).
  • Method Details

    • getEvent

      public String getEvent()
      Returns the event-type name.
      Returns:
      The event-type name, or null if not set.
    • setEvent

      public SseEvent setEvent(String value)
      Sets the event-type name.
      Parameters:
      value - The event-type name. Can be null to unset.
      Returns:
      This object.
    • getData

      public String getData()
      Returns the data payload (pre-serialized string).
      Returns:
      The data payload, or null if not set.
    • setData

      public SseEvent setData(String value)
      Sets the data payload.
      Parameters:
      value - The pre-serialized payload string. Can be null to unset.
      Returns:
      This object.
    • getId

      public String getId()
      Returns the last-event-id value.
      Returns:
      The id, or null if not set.
    • setId

      public SseEvent setId(String value)
      Sets the last-event-id value.
      Parameters:
      value - The id. Can be null to unset.
      Returns:
      This object.
    • getRetry

      public Long getRetry()
      Returns the reconnect-delay hint in milliseconds.
      Returns:
      The retry value, or null if not set.
    • setRetry

      public SseEvent setRetry(Long value)
      Sets the reconnect-delay hint in milliseconds.
      Parameters:
      value - The retry value. Can be null to unset.
      Returns:
      This object.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object