public class RetryHttpClient extends Object implements HttpClient
HttpClient
implementation that retries contacting a remote URL and
allows for persistant connections. For each retry, this client waits an
additional second before attempting a new connection. Note that if a
failure occurs reading the server response, retries may or may not be made
only for non-POST
and PUT
methods. Refer to
readResponse
on this is handled.
Also, this client handles most redirects, on either the same host or
different host. By default, note that redirects will only be followed for
non-POST
and PUT
methods.
Refer to setFollowRedirects
on this.
HTTPS (HTTP over SSL) relies on the javax.net.ssl
library, and
may require com.sun.net.ssl.internal.ssl.Provider
to be
installed using the java.security.Security.addProvider
method.
Many of the protected
methods may be overriden
to supply specific functionality. For example, by default
the underlying HttpClient is the BasicHttpClient
,
this can be easily changed.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_MAX_TRIES
Default number of tries.
|
Constructor and Description |
---|
RetryHttpClient(URL url)
Constructs a
RetryHttpClient that retries 3 times. |
RetryHttpClient(URL url,
int maxTries)
Constructs a
RetryHttpClient that retries a number of times. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the wrapped
HttpClient . |
OutputStream |
getOutputStream()
Returns a stream for writing data to, if data is to be sent to the
server.
|
protected HttpClient |
makeHttpClient(URL url)
Returns a newly constructed HTTP client for a given URL.
|
protected Socket |
makeSocket(URL url)
Returns a newly constructed socket for a given URL.
|
protected Socket |
makeSSLSocket(URL url)
Returns a newly constructed SSL socket for a given URL.
|
ClientResponse |
readResponse()
Reads the response data from the HTTP server.
|
protected void |
retrySendRequest(IOException e,
int failures)
Callback that indicates the connection failed and will be retried.
|
void |
setFollowRedirects(boolean followRedirects)
Sets whether HTTP redirects should be followed.
|
void |
setSkipContinues(boolean skipContinues)
Sets whether HTTP continue responses should be skipped.
|
protected void |
setSocketOptions(Socket socket)
Sets the socket options to use.
|
String |
toString()
Returns debug information.
|
void |
writeRequest(ClientRequest request)
Attempts to send an HTTP request, and may retry to send a certain
number of times.
|
public static final int DEFAULT_MAX_TRIES
public RetryHttpClient(URL url, int maxTries)
RetryHttpClient
that retries a number of times.maxTries
- must be greater than zeropublic RetryHttpClient(URL url)
RetryHttpClient
that retries 3 times.protected void retrySendRequest(IOException e, int failures)
failures
- number of failures (counting from 0)protected Socket makeSocket(URL url) throws IOException
IOException
protected Socket makeSSLSocket(URL url) throws IOException
IOException
protected void setSocketOptions(Socket socket) throws IOException
IOException
protected HttpClient makeHttpClient(URL url) throws IOException
makeSocket(java.net.URL)
to construct a network socket
for HTTP, makeSSLSocket(java.net.URL)
for HTTPS.
Calls setSocketOptions(java.net.Socket)
to set socket options.IOException
public void writeRequest(ClientRequest request) throws IOException
makeHttpClient(java.net.URL)
to construct a new client.writeRequest
in interface HttpClient
IOException
public OutputStream getOutputStream()
getOutputStream
in interface HttpClient
public ClientResponse readResponse() throws IOException
If the response data cannot be read, then the connection will be
closed, and writeRequest
will be called
up to the maximum of retries as previously set.
The original request will not retried if the request method was
PUT
or POST
and ClientRequest.getDataPoster()
returns null
.
If a redirection response was read, it is followed only if redirection is enabled.
readResponse
in interface HttpClient
HttpException
- if the server returned an invalid HTTP
responseIOException
public void close() throws IOException
HttpClient
.
The underlying client may not be re-used.close
in interface HttpClient
IOException
public void setFollowRedirects(boolean followRedirects)
public void setSkipContinues(boolean skipContinues)
Copyright © 2012. All Rights Reserved.