5

Basically, what I'm facing today is the following:

  • Handle Request time out when doing Rest actions.

Seems simple written, but not as easy to code.

This is my implementation so far:

List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
interceptors.add( new NetworkInterceptor() );

tpl.setInterceptors( interceptors );

So now, after setting interceptors, I'd like to set custom timeout configurations for the template.

So I do the following: tpl.getRequestFactory().

This is returning a InterceptingClientHttpRequestFactory instead of SimpleClientHttpRequestFactory as would be returned in case no interceptors were set.

So as it is returning that InterceptingClientHttpRequestFactory instance, I can't set the Timeout.

You can check the sourcecode of Spring, the last method: http://grepcode.com/file_/repo1.maven.org/maven2/org.springframework/spring-web/3.1.1.RELEASE/org/springframework/http/client/support/InterceptingHttpAccessor.java/?v=source

So... Any tips?

Reinherd
  • 5,476
  • 7
  • 51
  • 88

3 Answers3

8

Assuming tpl is a RestTemplate, you can pass a SimpleClientHttpRequestFactory as parameter to its constructor:

    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
    interceptors.add( new NetworkInterceptor() );

    SimpleClientHttpRequestFactory s = new SimpleClientHttpRequestFactory();
    s.setReadTimeout(5000);
    s.setConnectTimeout(1000);

    RestTemplate tpl = new RestTemplate(s);//Or however you instantiated it
    tpl.setInterceptors( interceptors );

    ClientHttpRequestFactory c =  tpl.getRequestFactory();

Hope it helps.

eltabo
  • 3,749
  • 1
  • 21
  • 33
  • I've tried something similar. The problem is that once you've set any interceptor, you won't be able to change the Template, as it's fixed nw to InterceptingClientHttpRequestFactory. – Reinherd Feb 10 '14 at 16:04
  • Even if you keep a reference to your SimpleClientHttpRequestFactory? InterceptingClientHttpRequestFactory keep a reference of SimpleClientHttpRequestFactory, so I think you can modify timeouts. – eltabo Feb 10 '14 at 16:20
  • Tomorrow I will give it a try. Thanks. – Reinherd Feb 10 '14 at 22:09
  • Sure it's not work? Take a look at this gist https://gist.github.com/eltabo/8931347 Actually it's the same solution, since setting an interceptor SimpleClientHttpRequestFactory (say default one, say our factory) is wrapped with an InterceptingClientHttpRequestFactory. At the end the request it's created using the wrapped SimpleClientHttpRequestFactory. – eltabo Feb 11 '14 at 08:56
  • Yes. It is working. It was my problem, now I'm keeping the template reference as static. I've to wait 4 hours to give you the bounty. Thank you so much =) – Reinherd Feb 11 '14 at 09:16
7

i see the androidannotations document has @Rest annotations

@Rest(rootUrl="yourRootUrl",requestFactory=AppRequestFacetory.class,converters ={..},interceptors={..})
public interface RestApis extends RestClientErrorHandling{...};

AppRequestFacetory.class set TIMEOUT as belows:

@EBean
class AppRequestFactory extends SimpleClientHttpRequestFactory {

     @AfterInject
     void afterinject() {
         setReadTimeout(20*1000); //set 20s read timeout
         setConnectTimeout(20*1000); //set 20s connect timeout
     }
}

and it works . enjoy androidannotations rest api

mrljdx
  • 129
  • 1
  • 3
0

When I needed to configure Android annotations rest client I've used code like this:

    ClientHttpRequestFactory requestFactory = restClient.getRestTemplate().getRequestFactory();
    if (requestFactory instanceof SimpleClientHttpRequestFactory) {
        Log.d("HTTP", "HttpUrlConnection is used");
        ((SimpleClientHttpRequestFactory) requestFactory).setConnectTimeout(3 * 1000);
        ((SimpleClientHttpRequestFactory) requestFactory).setReadTimeout(3 * 1000);
    } else if (requestFactory instanceof HttpComponentsClientHttpRequestFactory) {
        Log.d("HTTP", "HttpClient is used");
        ((HttpComponentsClientHttpRequestFactory) requestFactory).setReadTimeout(3 * 1000);
        ((HttpComponentsClientHttpRequestFactory) requestFactory).setConnectTimeout(3 * 1000);
    }
Vadim Zin4uk
  • 1,716
  • 22
  • 18