简体   繁体   中英

How to handle read timeout exception in jersey client 1.8

I am using jersey 1.8

to call an external service. Here is my code.

 try{
     ClientResponse response = webResource.header(HttpHeaders.AUTHORIZATION, encodedHashString).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, formData);
 }catch(ClientHandlerException che){
   //handelling code here
 {

when read timeout exception occurs it gives ClientHandlerException and underlying exception is SocketTimeoutException . But question here is I can not just say that since its ClientHandlerException it is a timeout exception because this exception can happen for other client related errors. what can be the exact code to handle it, I need to do some handeling if its a read timeout exception.

Try something like this:

try {
    ClientResponse response = webResource.header(HttpHeaders.AUTHORIZATION, encodedHashString).type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(ClientResponse.class, formData);
} catch(ClientHandlerException ex) {
    handleClientHandlerException(ex);
}

private void handleClientHandlerException(ClientHandlerException ex) throws ClientHandlerException {
    if (ex.getCause() instanceof SocketTimeoutException) {
        // handelling SocketTimeoutException code here
    }
    throw ex;
}

In handleClientHandlerException you also can try something like ExceptionUtils#getRootCause from apache commons lang if cause isn't SocketTimeoutException to get the root cause.

您可以使用来自番石榴的Throwables.getRootCause方法!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM