Class HttpEventStreamClient


  • public class HttpEventStreamClient
    extends Object
    HTTP Client that can listen for Server-Sent Events (SSE). Implements full protocol and supports automatic reconnect.
    Since:
    2020-12-26
    Author:
    LupCode.com (Luca Vogels)
    • Constructor Detail

      • HttpEventStreamClient

        public HttpEventStreamClient​(String url,
                                     EventStreamListener... listener)
        Creates a HTTP client that listens for Server-Sent Events (SSE). Starts listening after calling start()
        Parameters:
        url - URL the client should listen at
        listener - Event stream listeners that listen for arriving events (optional)
      • HttpEventStreamClient

        public HttpEventStreamClient​(String url,
                                     Map<String,​String> headers,
                                     EventStreamListener... listener)
        Creates a HTTP client that listens for Server-Sent Events (SSE). Starts listening after calling start()
        Parameters:
        url - URL the client should listen at
        headers - HTTP headers that should be set for the request. SSE specific headers will get overwritten [Accept, Cache-Control, Last-Event-ID] (optional)
        listener - Event stream listeners that listen for arriving events (optional)
      • HttpEventStreamClient

        public HttpEventStreamClient​(String url,
                                     HttpRequestMethod method,
                                     HttpRequest.BodyPublisher requestBody,
                                     Map<String,​String> headers,
                                     EventStreamListener... listener)
        Creates a HTTP client that listens for Server-Sent Events (SSE). Starts listening after calling start()
        Parameters:
        url - URL the client should listen at
        method - HTTP method that should be used to request the event stream (default GET)
        requestBody - HTTP request body that gets sent along the request (optional)
        headers - HTTP headers that should be set for the request. SSE specific headers will get overwritten [Accept, Cache-Control, Last-Event-ID] (optional)
        listener - Event stream listeners that listen for arriving events (optional)
      • HttpEventStreamClient

        public HttpEventStreamClient​(String url,
                                     HttpRequestMethod method,
                                     HttpRequest.BodyPublisher requestBody,
                                     Map<String,​String> headers,
                                     long timeout,
                                     long retryCooldown,
                                     EventStreamListener... listener)
        Creates a HTTP client that listens for Server-Sent Events (SSE). Starts listening after calling start()
        Parameters:
        url - URL the client should listen at
        method - HTTP method that should be used to request the event stream (default GET)
        requestBody - HTTP request body that gets sent along the request (optional)
        headers - HTTP headers that should be set for the request. SSE specific headers will get overwritten [Accept, Cache-Control, Last-Event-ID] (optional)
        timeout - Timeout in milliseconds for the HTTP client before it reconnects (if negative then ignored)
        retryCooldown - Cooldown in milliseconds after connection loss before starting to reconnect (negative for no cooldown)
        listener - Event stream listeners that listen for arriving events (optional)
      • HttpEventStreamClient

        public HttpEventStreamClient​(String url,
                                     HttpRequestMethod method,
                                     HttpRequest.BodyPublisher requestBody,
                                     HttpClient.Version version,
                                     Map<String,​String> headers,
                                     long timeout,
                                     long retryCooldown,
                                     int maxReconnectsWithoutEvents,
                                     boolean resetEventIDonReconnect,
                                     HttpClient client,
                                     EventStreamListener... listener)
        Creates a HTTP client that listens for Server-Sent Events (SSE). Starts listening after calling start()
        Parameters:
        url - URL the client should listen at
        method - HTTP method that should be used to request the event stream (default GET)
        requestBody - HTTP request body that gets sent along the request (optional)
        version - Specific HTTP version that should be used to request (optional)
        headers - HTTP headers that should be set for the request. SSE specific headers will get overwritten [Accept, Cache-Control, Last-Event-ID] (optional)
        timeout - Timeout in milliseconds for the HTTP client before it reconnects (if negative then ignored)
        retryCooldown - Cooldown in milliseconds after connection loss before starting to reconnect (negative for no cooldown)
        maxReconnectsWithoutEvents - How often client can reconnect without receiving events before it stops (zero for no reconnect, negative for infinitely)
        resetEventIDonReconnect - If true then event id will be set back to zero on a reconnect (default false)
        client - HTTP client that should be used (optional)
        listener - Event stream listeners that listen for arriving events (optional)
    • Method Detail

      • getURI

        public URI getURI()
        URI this client listens for events
        Returns:
        URI that is used to listen for events
      • getURL

        public String getURL()
        URL string this client listens for events
        Returns:
        URL string that is used to listen for events
      • setURL

        public void setURL​(String url)
        Sets the URL that will be used after the next reconnect. If change should immediately take place call start() afterwards
        Parameters:
        url - URL the client should listen at
      • setURI

        public void setURI​(URI uri)
        Sets the URI that will be used after the next reconnect. If change should immediately take place call start() afterwards
        Parameters:
        uri - URI the client should listen at
      • getHttpMethod

        public HttpRequestMethod getHttpMethod()
        Returns the HTTP method type that client uses for HTTP requests
        Returns:
        HTTP request method type
      • setHttpMethod

        public void setHttpMethod​(HttpRequestMethod method)
        Sets the HTTP method type that client uses for HTTP requests
        Parameters:
        method - HTTP request method type used for HTTP requests
      • getHttpRequestBody

        public HttpRequest.BodyPublisher getHttpRequestBody()
        Returns the HTTP body used for requests (can be null)
        Returns:
        HTTP request body or null
      • setHttpRequestBody

        public void setHttpRequestBody​(HttpRequest.BodyPublisher requestBody)
        Sets a HTTP body used for requests. Only needed for certain HTTP request methods otherwise ignored.
        Parameters:
        requestBody - HTTP request body or null
      • getHttpVersion

        public HttpClient.Version getHttpVersion()
        Returns HTTP version if a specific one is set that should be used
        Returns:
        HTTP version that is forced to be used or null
      • setHttpVersion

        public void setHttpVersion​(HttpClient.Version version)
        Sets a specific HTTP version that should be used. If null then HTTP client will automatically determine appropriate version
        Parameters:
        version - HTTP version that is forced to be used or null
      • getHeaders

        public Map<String,​String> getHeaders()
        Returns HTTP headers that will be used for HTTP requests
        Returns:
        HTTP headers map
      • addHeaders

        public void addHeaders​(Map<String,​String> headers)
        Adds HTTP headers that will be used for HTTP requests (overwrites existing ones). SSE specific headers cannot be overwritten (Accept, Cache-Control, Last-Event-ID)
        Parameters:
        headers - HTTP headers that should be added
      • setHeaders

        public void setHeaders​(Map<String,​String> headers)
        Sets HTTP headers that will be used for HTTP requests (removes all existing ones). SSE specific headers cannot be overwritten (Accept, Cache-Control, Last-Event-ID)
        Parameters:
        headers - HTTP headers that should be added
      • setHeader

        public void setHeader​(String key,
                              String value)
        Sets/Removes a HTTP header that will be used for HTTP requests. SSE specific headers cannot be overwritten (Accept, Cache-Control, Last-Event-ID)
        Parameters:
        key - Key of the header (cannot be null or blank)
        value - Value that should be set (if null then key gets removed)
      • getHeader

        public String getHeader​(String key)
        Returns the value of the HTTP headers for a specific key
        Parameters:
        key - Key the value should be returned for (cannot be null or empty)
        Returns:
        Value that is set for the HTTP header or null if not set
      • removeHeader

        public String removeHeader​(String key)
        Removes a HTTP header so it gets no longer used for HTTP requests
        Parameters:
        key - Key of header that should be removed
        Returns:
        Previously set value or null if not previously set
      • removeHeaders

        public void removeHeaders​(String... keys)
        Removes multiple HTTP headers so they no longer will be used for HTTP requests
        Parameters:
        keys - Keys of the HTTP headers
      • clearHeaders

        public void clearHeaders()
        Removes all HTTP headers so no custom HTTP headers will be sent in the HTTP requests
      • getTimeout

        public long getTimeout()
        Returns the timeout in milliseconds for the HTTP client before it reconnects (if negative then ignored)
        Returns:
        Timeout in milliseconds
      • setTimeout

        public void setTimeout​(long timeout)
        Sets the timeout in milliseconds for the HTTP client before it reconnects (if negative then ignored)
        Parameters:
        timeout - Timeout in milliseconds
      • getRetryCooldown

        public long getRetryCooldown()
        Returns the cooldown in milliseconds that this client will wait before reconnecting after a connection loss
        Returns:
        Cooldown in milliseconds
      • setRetryCooldown

        public void setRetryCooldown​(long retryCooldown)
        Sets the cooldown in milliseconds that this client will wait before reconnection after a connection loss
        Parameters:
        retryCooldown - Cooldown in milliseconds (negative for no cooldown)
      • isAutoStopIfNoEventsEnabled

        public boolean isAutoStopIfNoEventsEnabled()
        Returns true if client automatically stops after a certain amount of reconnects without receiving events in between
        Returns:
        True if auto stop enabled
      • isReconnectEnabled

        public boolean isReconnectEnabled()
        Returns true if client automatically reconnects if connection is lost
        Returns:
        True if reconnect on connection loss
      • getAutoStopThreshold

        public int getAutoStopThreshold()
        Returns how often client can reconnect without receiving events in between before it automatically stops. If zero then client will not reconnect after a connection loss. If negative then auto stop is disabled and client keeps reconnecting for ever
        Returns:
        Max reconnects without events before stopping
      • setAutoStopThreshold

        public void setAutoStopThreshold​(int maxReconnectsWithoutEvents)
        Sets how often the client can reconnect without receiving events in between before it automatically stops. If zero then client will not reconnect after a connection loss. If negative then client will keep reconnecting for ever
        Parameters:
        maxReconnectsWithoutEvents - How often client can reconnect without receiving events before it stops (zero for no reconnect, negative for infinitely)
      • getReconnectsWithoutEvents

        public int getReconnectsWithoutEvents()
        Returns how often client reconnected so far without receiving any events in between. Gets reset to zero if client receives an event
        Returns:
        Reconnection count without events
      • getHttpClient

        public HttpClient getHttpClient()
        HttpClient that gets used for HTTP requests. May be null if not specified and not started yet
        Returns:
        HttpClient that is used for HTTP requests (may be null)
      • setHttpClient

        public void setHttpClient​(HttpClient client)
        Sets if a specific HttpClient should be used for HTTP requests. If null a new HttpClient instance will be created
        Parameters:
        client - HTTP client that should be used (null for new instance)
      • getLastEventID

        public long getLastEventID()
        Returns ID of the latest event
        Returns:
        ID of latest event
      • setLastEventID

        public void setLastEventID​(long id)
        Sets the event id that should be sent on next start/reconnect. May be overwritten by the server in the mean time. Call start() to force sending of new id
        Parameters:
        id - Event id that should be sent in HTTP header (Last-Event-ID)
      • isResetLastEventIDonReconnect

        public boolean isResetLastEventIDonReconnect()
        Returns if last event id gets reset to zero on reconnect
        Returns:
        True if set to zero on a reconnect
      • setResetLastEventIDonReconnect

        public void setResetLastEventIDonReconnect​(boolean reset)
        Sets if the last event it should be set to zero on a reconnect
        Parameters:
        reset - If true then last event it will be reset on a reconnect
      • getListeners

        public Set<EventStreamListener> getListeners()
        Returns a set containing all added listeners
        Returns:
        Set of all added listeners
      • removeAllListeners

        public void removeAllListeners()
        Removes all listeners so they no longer get called
      • addListener

        public void addListener​(EventStreamListener... listener)
        Adds a listener so it gets called on new events. Multiple adding of same listener will only add once
        Parameters:
        listener - Listener(s) that should be added
      • removeListener

        public void removeListener​(EventStreamListener... listener)
        Removes the listeners so they no longer get called
        Parameters:
        listener - Listeners that should be removed
      • isRunning

        public boolean isRunning()
        Returns if client is currently listening for SSE events
        Returns:
      • start

        public HttpEventStreamClient start()
        Starts listening for SSE events and immediately returns. If client looses connection then automatically reconnects. Multiple calls will not start multiple listening but calls EventStreamListener#onReconnect() on listeners
        Returns:
        This client instance
      • join

        public HttpEventStreamClient join()
        Blocks until this client has stopped listening. If not listening then returns immediately
        Returns:
        This client instance
      • stop

        public HttpEventStreamClient stop()
        Stops without reconnecting. Executes EventStreamListener#onClose() on listeners
        Returns:
        This client instance