Class HttpEventStreamClient
java.lang.Object
io.quarkiverse.githubapp.runtime.sse.HttpEventStreamClient
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)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassEvent that gets received by theHttpEventStreamClientprotected class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected HttpClientprotected final AtomicBooleanprotected longprotected HashSet<EventStreamListener>protected intprotected HttpRequestMethodprotected final AtomicIntegerprotected HttpRequest.BodyPublisherprotected booleanprotected longprotected CompletableFuture<HttpResponse<Void>>protected longprotected URIprotected HttpClient.Version -
Constructor Summary
ConstructorsConstructorDescriptionHttpEventStreamClient(String url, EventStreamListener... listener) Creates a HTTP client that listens for Server-Sent Events (SSE).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).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).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).HttpEventStreamClient(String url, Map<String, String> headers, EventStreamListener... listener) Creates a HTTP client that listens for Server-Sent Events (SSE). -
Method Summary
Modifier and TypeMethodDescriptionvoidaddHeaders(Map<String, String> headers) Adds HTTP headers that will be used for HTTP requests (overwrites existing ones).voidaddListener(EventStreamListener... listener) Adds a listener so it gets called on new events.voidRemoves all HTTP headers so no custom HTTP headers will be sent in the HTTP requestsintReturns how often client can reconnect without receiving events in between before it automatically stops.Returns the value of the HTTP headers for a specific keyReturns HTTP headers that will be used for HTTP requestsHttpClientthat gets used for HTTP requests.Returns the HTTP method type that client uses for HTTP requestsReturns the HTTP body used for requests (can be null)Returns HTTP version if a specific one is set that should be usedlongReturns ID of the latest eventReturns a set containing all added listenersintReturns how often client reconnected so far without receiving any events in between.longReturns the cooldown in milliseconds that this client will wait before reconnecting after a connection losslongReturns the timeout in milliseconds for the HTTP client before it reconnects (if negative then ignored)getURI()URI this client listens for eventsgetURL()URL string this client listens for eventsbooleanReturns true if client automatically stops after a certain amount of reconnects without receiving events in betweenbooleanReturns true if client automatically reconnects if connection is lostbooleanReturns if last event id gets reset to zero on reconnectbooleanReturns if client is currently listening for SSE eventsjoin()Blocks until this client has stopped listening.voidRemoves all listeners so they no longer get calledremoveHeader(String key) Removes a HTTP header so it gets no longer used for HTTP requestsvoidremoveHeaders(String... keys) Removes multiple HTTP headers so they no longer will be used for HTTP requestsvoidremoveListener(EventStreamListener... listener) Removes the listeners so they no longer get calledvoidsetAutoStopThreshold(int maxReconnectsWithoutEvents) Sets how often the client can reconnect without receiving events in between before it automatically stops.voidSets/Removes a HTTP header that will be used for HTTP requests.voidsetHeaders(Map<String, String> headers) Sets HTTP headers that will be used for HTTP requests (removes all existing ones).voidsetHttpClient(HttpClient client) Sets if a specificHttpClientshould be used for HTTP requests.voidsetHttpMethod(HttpRequestMethod method) Sets the HTTP method type that client uses for HTTP requestsvoidsetHttpRequestBody(HttpRequest.BodyPublisher requestBody) Sets a HTTP body used for requests.voidsetHttpVersion(HttpClient.Version version) Sets a specific HTTP version that should be used.voidsetLastEventID(long id) Sets the event id that should be sent on next start/reconnect.voidsetResetLastEventIDonReconnect(boolean reset) Sets if the last event it should be set to zero on a reconnectvoidsetRetryCooldown(long retryCooldown) Sets the cooldown in milliseconds that this client will wait before reconnection after a connection lossvoidsetTimeout(long timeout) Sets the timeout in milliseconds for the HTTP client before it reconnects (if negative then ignored)voidSets the URI that will be used after the next reconnect.voidSets the URL that will be used after the next reconnect.start()Starts listening for SSE events and immediately returns.stop()Stops without reconnecting.
-
Field Details
-
uri
-
method
-
requestBody
-
version
-
headers
-
timeout
protected long timeout -
retryCooldown
protected long retryCooldown -
maxReconnectsWithoutEvents
protected int maxReconnectsWithoutEvents -
hasReceivedEvents
-
reconnectWithoutEvents
-
client
-
lastEventID
protected long lastEventID -
resetEventIDonReconnect
protected boolean resetEventIDonReconnect -
listeners
-
internalListeners
-
running
-
-
Constructor Details
-
HttpEventStreamClient
Creates a HTTP client that listens for Server-Sent Events (SSE). Starts listening after callingstart()- Parameters:
url- URL the client should listen atlistener- 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 callingstart()- Parameters:
url- URL the client should listen atheaders- 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 callingstart()- Parameters:
url- URL the client should listen atmethod- 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 callingstart()- Parameters:
url- URL the client should listen atmethod- 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 callingstart()- Parameters:
url- URL the client should listen atmethod- 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 Details
-
getURI
URI this client listens for events- Returns:
- URI that is used to listen for events
-
getURL
URL string this client listens for events- Returns:
- URL string that is used to listen for events
-
setURL
Sets the URL that will be used after the next reconnect. If change should immediately take place callstart()afterwards- Parameters:
url- URL the client should listen at
-
setURI
Sets the URI that will be used after the next reconnect. If change should immediately take place callstart()afterwards- Parameters:
uri- URI the client should listen at
-
getHttpMethod
Returns the HTTP method type that client uses for HTTP requests- Returns:
- HTTP request method type
-
setHttpMethod
Sets the HTTP method type that client uses for HTTP requests- Parameters:
method- HTTP request method type used for HTTP requests
-
getHttpRequestBody
Returns the HTTP body used for requests (can be null)- Returns:
- HTTP request body or null
-
setHttpRequestBody
Sets a HTTP body used for requests. Only needed for certain HTTP request methods otherwise ignored.- Parameters:
requestBody- HTTP request body or null
-
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
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
Returns HTTP headers that will be used for HTTP requests- Returns:
- HTTP headers map
-
addHeaders
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
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
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
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
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
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
HttpClientthat gets used for HTTP requests. May be null if not specified and not started yet- Returns:
HttpClientthat is used for HTTP requests (may be null)
-
setHttpClient
Sets if a specificHttpClientshould be used for HTTP requests. If null a newHttpClientinstance 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. Callstart()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
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
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
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
Starts listening for SSE events and immediately returns. If client loses connection then automatically reconnects. Multiple calls will not start multiple listening but callsEventStreamListener.onReconnect(HttpEventStreamClient, HttpResponse, boolean, long)on listeners- Returns:
- This client instance
-
join
Blocks until this client has stopped listening. If not listening then returns immediately- Returns:
- This client instance
-
stop
Stops without reconnecting. ExecutesEventStreamListener.onClose(HttpEventStreamClient, HttpResponse)on listeners- Returns:
- This client instance
-