0

I need to add timeout in my code this is my code but I dont have a clue how to modify this code to add timeout.

`

 @EBean(scope = EBean.Scope.Singleton)
public class KioraRestClient {

public static final String SERVER_HOST         = KioraBuildConfig.REST_SERVER_API_URL;
public static final String VERSION_SERVER_HOST = KioraBuildConfig.REST_VERSION_SERVER_API_URL;


@AfterInject
void Initialize() {

    try {
        turnOffSslChecking();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    // this factory needs to be crated here since requires some injected dependencies
    ClientHttpRequestFactory keyPairRequestFactory = new KioraHttpRequestFactory(
            new ContentAuthUriDecorator(authPreferences)
    );

    ClientHttpRequestFactory timeoutFactory = new SimpleTimeoutRequestFactory();        


    //TODO: Extract common interface for the controllers
    LicenseController.getRestTemplate().setRequestFactory(keyPairRequestFactory);
    UserController.getRestTemplate().setRequestFactory(keyPairRequestFactory);
    ContentController.getRestTemplate().setRequestFactory(keyPairRequestFactory);

    VersionController.getRestTemplate().setRequestFactory(timeoutFactory);
    RootIPController.getRestTemplate().setRequestFactory(keyPairRequestFactory);

    PurchaseController.getRestTemplate().setRequestFactory(keyPairRequestFactory);
    HotspotController.getRestTemplate().setRequestFactory(keyPairRequestFactory);

`

I tried adding timeout like this but this timeout of 20 seccond's doesn't seem to have a effect `

HttpComponentsClientHttpRequestFactory  httpRequestFactory =  new     HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectTimeout(20 * 1000); 
 PurchaseController.getRestTemplate().setRequestFactory(httpRequestFactory);
    HotspotController.getRestTemplate().setRequestFactory(httpRequestFactory);

`

elsa
  • 396
  • 1
  • 13
  • This is not related to the rest template, only to the underlying http library. BTW, i recommend using okhttp instead of the old http-components. – WonderCsabo Jun 02 '16 at 08:28
  • What do you want to achieve with the time out set? http://stackoverflow.com/questions/3069382/what-is-the-difference-between-connection-and-read-timeout-for-sockets – Michal Foksa Jun 02 '16 at 16:05
  • I need to wait for the response and then do the rest of the things – elsa Jun 08 '16 at 06:44

1 Answers1

0

You are only setting the connect timeout not read timeout. Connect timeout defines how long it would wait before giving up trying to connect. However, read timeout defines how long it would wait for the response.

Set read timeout the same way and you will be able to see it taking effect

httpRequestFactory.setReadTimeout(20 * 1000);

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.html#setReadTimeout-int-

ritesh.garg
  • 3,725
  • 1
  • 15
  • 14