Class HttpConnectStrategy
- java.lang.Object
-
- com.launchdarkly.eventsource.ConnectStrategy
-
- com.launchdarkly.eventsource.HttpConnectStrategy
-
public class HttpConnectStrategy extends ConnectStrategy
Allows configuration of HTTP request behavior forEventSource.EventSource uses this as its default implementation of how to connect to a stream. The underlying implementation is OkHttp.
If you do not need to specify any options other than the stream URI, then you do not need to reference this class directly; you can just call one of the
Builder(URI).To configure additional options, obtain an
HttpConnectStrategyinstance by callingConnectStrategy.http(URI), and then call any of the methods of this class to specify your options. The class is immutable, so each of these methods returns a new modified instance, and an EventSource created with this configuration will not be affected by any subsequent changes you make.Once you have configured all desired options, pass the object to the
Builder(ConnectStrategy)constructor:EventSource es = new EventSource.Builder( ConnectStrategy.http() .addHeader("Authorization", "xyz") .connectTimeout(3, TimeUnit.SECONDS) ) .build();- Since:
- 4.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceHttpConnectStrategy.ClientConfigurerAn interface for use withclientBuilderActions(ClientConfigurer).static interfaceHttpConnectStrategy.RequestTransformerAn interface for use withrequestTransformer(RequestTransformer).
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_CONNECT_TIMEOUT_MILLISThe default value forconnectTimeout(long, TimeUnit): 10 seconds.static longDEFAULT_READ_TIMEOUT_MILLISThe default value forreadTimeout(long, TimeUnit): 5 minutes.static longDEFAULT_WRITE_TIMEOUT_MILLISThe default value forwriteTimeout(long, TimeUnit): 5 seconds.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpConnectStrategyclientBuilderActions(HttpConnectStrategy.ClientConfigurer configurer)Specifies any type of configuration actions you want to perform on the OkHttpClient builder.HttpConnectStrategyconnectTimeout(long connectTimeout, java.util.concurrent.TimeUnit timeUnit)Specifies the connection timeout.com.launchdarkly.eventsource.HttpConnectStrategy.ClientcreateClient(LDLogger logger)Creates a client instance.HttpConnectStrategyheader(java.lang.String name, java.lang.String value)Sets a custom header to be included in each request.HttpConnectStrategyheaders(okhttp3.Headers headers)Sets custom headers to be included in each request.HttpConnectStrategyhttpClient(okhttp3.OkHttpClient httpClient)Set a custom HTTP client that will be used to make the EventSource connection.HttpConnectStrategymethodAndBody(java.lang.String method, okhttp3.RequestBody body)Specifies the request method and body to be used for HTTP requests.HttpConnectStrategyproxy(java.lang.String proxyHost, int proxyPort)Specifies a web proxy to be used for all HTTP connections.HttpConnectStrategyproxy(java.net.Proxy proxy)Specifies a web proxy to be used for all HTTP connections.HttpConnectStrategyproxyAuthenticator(okhttp3.Authenticator proxyAuthenticator)Specifies a web proxy authenticator to be used for all HTTP connections.HttpConnectStrategyreadTimeout(long readTimeout, java.util.concurrent.TimeUnit timeUnit)Sets the read timeout.HttpConnectStrategyrequestTransformer(HttpConnectStrategy.RequestTransformer requestTransformer)Specifies an object that will be used to customize outgoing requests.HttpConnectStrategyuri(java.net.URI uri)Specifies a different stream URI.HttpConnectStrategywriteTimeout(long writeTimeout, java.util.concurrent.TimeUnit timeUnit)Sets the write timeout.-
Methods inherited from class com.launchdarkly.eventsource.ConnectStrategy
http, http, http
-
-
-
-
Field Detail
-
DEFAULT_CONNECT_TIMEOUT_MILLIS
public static final long DEFAULT_CONNECT_TIMEOUT_MILLIS
The default value forconnectTimeout(long, TimeUnit): 10 seconds.- See Also:
- Constant Field Values
-
DEFAULT_WRITE_TIMEOUT_MILLIS
public static final long DEFAULT_WRITE_TIMEOUT_MILLIS
The default value forwriteTimeout(long, TimeUnit): 5 seconds.- See Also:
- Constant Field Values
-
DEFAULT_READ_TIMEOUT_MILLIS
public static final long DEFAULT_READ_TIMEOUT_MILLIS
The default value forreadTimeout(long, TimeUnit): 5 minutes.- See Also:
- Constant Field Values
-
-
Method Detail
-
createClient
public com.launchdarkly.eventsource.HttpConnectStrategy.Client createClient(LDLogger logger)
Description copied from class:ConnectStrategyCreates a client instance.This is called once when an EventSource is created. The EventSource retains the returned Client and uses it to perform all subsequent connection attempts.
- Specified by:
createClientin classConnectStrategy- Parameters:
logger- the logger belonging to EventSource- Returns:
- a
ConnectStrategy.Clientinstance
-
clientBuilderActions
public HttpConnectStrategy clientBuilderActions(HttpConnectStrategy.ClientConfigurer configurer)
Specifies any type of configuration actions you want to perform on the OkHttpClient builder.HttpConnectStrategy.ClientConfigureris an interface with a single method, so you can use a lambda instead. EventSource will call this method when it creates an HTTP client. If you call this method multiple times, or use it in combination with other client configuration methods, the actions are performed in the same order as the calls.EventSource es = new EventSource.Builder( ConnectStrategy.http().clientBuilderActions(clientBuilder -> { clientBuilder.sslSocketFactory(mySocketFactory, myTrustManager); });- Parameters:
configurer- a ClientConfigurer (or lambda) that will act on the HTTP client builder- Returns:
- a new HttpConnectStrategy instance with this property modified
-
connectTimeout
public HttpConnectStrategy connectTimeout(long connectTimeout, java.util.concurrent.TimeUnit timeUnit)
Specifies the connection timeout.- Parameters:
connectTimeout- the connection timeout, in whatever time unit is specified bytimeUnittimeUnit- the time unit, orTimeUnit.MILLISECONDSif null- Returns:
- a new HttpConnectStrategy instance with this property modified
- See Also:
DEFAULT_CONNECT_TIMEOUT_MILLIS,readTimeout(long, TimeUnit),writeTimeout(long, TimeUnit)
-
header
public HttpConnectStrategy header(java.lang.String name, java.lang.String value)
Sets a custom header to be included in each request.Any existing headers with the same name are overwritten.
- Parameters:
name- the header namevalue- the header value- Returns:
- a new HttpConnectStrategy instance with this property modified
-
headers
public HttpConnectStrategy headers(okhttp3.Headers headers)
Sets custom headers to be included in each request.Any existing headers with the same name are overwritten.
- Parameters:
headers- headers to be sent with the HTTP request- Returns:
- a new HttpConnectStrategy instance with this property modified
-
httpClient
public HttpConnectStrategy httpClient(okhttp3.OkHttpClient httpClient)
Set a custom HTTP client that will be used to make the EventSource connection.If you use this method, all other methods that correspond to client configuration properties (timeouts, proxy, etc.) will be ignored, and this client instance will be used exactly as is. Closing the EventSource will not cause this client to be closed; you are responsible for its lifecycle.
- Parameters:
httpClient- the HTTP client, or null to allow EventSource to create a client- Returns:
- a new HttpConnectStrategy instance with this property modified
-
methodAndBody
public HttpConnectStrategy methodAndBody(java.lang.String method, okhttp3.RequestBody body)
Specifies the request method and body to be used for HTTP requests. The default is to use the GET method with no request body. A non-null body is only valid for some request methods, such as POST.- Parameters:
method- the HTTP methodbody- the request body or null- Returns:
- a new HttpConnectStrategy instance with these properties modified
-
proxy
public HttpConnectStrategy proxy(java.net.Proxy proxy)
Specifies a web proxy to be used for all HTTP connections.- Parameters:
proxy- the proxy- Returns:
- a new HttpConnectStrategy instance with this property modified
-
proxy
public HttpConnectStrategy proxy(java.lang.String proxyHost, int proxyPort)
Specifies a web proxy to be used for all HTTP connections.- Parameters:
proxyHost- the proxy hostnameproxyPort- the proxy port- Returns:
- a new HttpConnectStrategy instance with this property modified
-
proxyAuthenticator
public HttpConnectStrategy proxyAuthenticator(okhttp3.Authenticator proxyAuthenticator)
Specifies a web proxy authenticator to be used for all HTTP connections.- Parameters:
proxyAuthenticator- the proxy authenticator- Returns:
- a new HttpConnectStrategy instance with this property modified
-
readTimeout
public HttpConnectStrategy readTimeout(long readTimeout, java.util.concurrent.TimeUnit timeUnit)
Sets the read timeout. If a read timeout happens, theEventSourcewill restart the connection.- Parameters:
readTimeout- the read timeout, in whatever time unit is specified bytimeUnittimeUnit- the time unit, orTimeUnit.MILLISECONDSif null- Returns:
- a new HttpConnectStrategy instance with this property modified
- See Also:
DEFAULT_READ_TIMEOUT_MILLIS,connectTimeout(long, TimeUnit),writeTimeout(long, TimeUnit)
-
requestTransformer
public HttpConnectStrategy requestTransformer(HttpConnectStrategy.RequestTransformer requestTransformer)
Specifies an object that will be used to customize outgoing requests.Use this if you need to set request properties other than the ones that are already supported by
HttpConnectStrategymethods, or if you need to determine the request properties dynamically rather than setting them to fixed values initially.EventSource es = new EventSource.Builder( ConnectStrategy.http().requestTransformer(request -> { return request.newBuilder().tag("hello").build(); }).build();If you call this method multiple times, the transformations are applied in the same order as the calls.
- Parameters:
requestTransformer- the transformer object- Returns:
- a new HttpConnectStrategy instance with this property modified
-
uri
public HttpConnectStrategy uri(java.net.URI uri)
Specifies a different stream URI.- Parameters:
uri- the stream URI- Returns:
- a new HttpConnectStrategy instance with this property modified
-
writeTimeout
public HttpConnectStrategy writeTimeout(long writeTimeout, java.util.concurrent.TimeUnit timeUnit)
Sets the write timeout. If a write timeout happens, theEventSourcewill restart the connection.- Parameters:
writeTimeout- the write timeout, in whatever time unit is specified bytimeUnittimeUnit- the time unit, orTimeUnit.MILLISECONDSif null- Returns:
- a new HttpConnectStrategy instance with this property modified
- See Also:
DEFAULT_READ_TIMEOUT_MILLIS,connectTimeout(long, TimeUnit),readTimeout(long, TimeUnit)
-
-