Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Handling timeout with RestTemplate. #902

Description

@sergicastellsague

Hello,

I've been looking a way to handle timeouts with the Rest template.

What I did, was the following:

ClientHttpRequestFactory factory = tpl.getRequestFactory();

            if ( factory instanceof SimpleClientHttpRequestFactory)
            {
                ((SimpleClientHttpRequestFactory) factory).setConnectTimeout( 5 * 1000 );
                ((SimpleClientHttpRequestFactory) factory).setReadTimeout( 5 * 1000 );
            }
            else if ( factory instanceof HttpComponentsClientHttpRequestFactory)
            {
                ((HttpComponentsClientHttpRequestFactory) factory).setReadTimeout( 5 * 1000);
                ((HttpComponentsClientHttpRequestFactory) factory).setConnectTimeout( 5 * 1000);
            }

            tpl.setRequestFactory( factory );

However, I'm not sure what is triggered or where can I catch a timeout and do something with it.

My implementation, ATM is with an interceptor (https://github.com/excilys/androidannotations/wiki/Rest%20API#interceptors). There it checks if device has internet connection, caches the response, etc. so it would be great to implement something to handle the TimeOut there aswell.

As an example, this is what is happening when my device has connection and everything works flawlessly:

SLog.d( "Before" );

ClientHttpResponse response = requestExecution.execute( request, bytes );

SLog.d( "After" );

And the log output is the following:

01-30 07:49:50.896 1308-1370/com.example. D/SLog﹕ Before
01-30 07:49:51.904 1308-1370/com.example D/SLog﹕ After

But if I set an unexisting gateway, so the request gets lost, the "After" text is never shown.

What I was trying to do, is to check the Headers from response object generated by the "execute" method of RequestExecution. And check if the response was a TIMEOUT. I can handle 500 responses, for example, but never TIMEOUT responses.

What I was thinking about, is that maybe ClientHttpResponse object generated by "execute" method, is always filled up with information from server and is never filled with information if the server doesn't return any information / can't reach server.

Any tip about how / where can I implement code to handle timeout? Note that if it's in the interceptor, it would be so great.

Thanks.

Activity

  1. sergicastellsague commented on Feb 4, 2014

    @sergicastellsague
    Author

    Oh... I found out the issue:

    ClientHttpRequestFactory factory = tpl.getRequestFactory();
    
                if ( factory instanceof SimpleClientHttpRequestFactory)
                {
                    ((SimpleClientHttpRequestFactory) factory).setConnectTimeout( 5 * 1000 );
                    ((SimpleClientHttpRequestFactory) factory).setReadTimeout( 5 * 1000 );
                }
                else if ( factory instanceof HttpComponentsClientHttpRequestFactory)
                {
                    ((HttpComponentsClientHttpRequestFactory) factory).setReadTimeout( 5 * 1000);
                    ((HttpComponentsClientHttpRequestFactory) factory).setConnectTimeout( 5 * 1000);
                }
    
                tpl.setRequestFactory( factory );
    

    This is not working, because as I've set interceptors, RequestFactory is org.springframework.http.client.InterceptingClientHttpRequestFactory which doesn't have timeout methods.

    Is there any way I can set timeout for this request factory?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions