0

I am building a spring boot micrcoservice where I will be frequently calling a webservice.

The SLA, service level agreement between my service provider and my client is 15 seconds..but I can call the webservice as much as I want during these 15 seconds..

Is there a way I can make sure that my requests and response are not taking longer than 15 seconds?

Is there a way to do this with Java/ spring boot?

Stephan L
  • 402
  • 3
  • 14
ErEcTuS
  • 777
  • 1
  • 14
  • 33
  • If your service's SLA is 15 seconds, then you probably want your outbound requests to be timed out in less than 15 seconds, so that you have time to build and return a timeout response to your client and remain within your SLA. – Graham Lea Nov 22 '19 at 09:50

1 Answers1

0

I have found that Spring Restemplate have a timeout feature. Any better solutions would be welcome :)

@Configuration
public class Config {

    @Value("${custom.rest.connection.connect-timeout}")
    private int connectionTimeout;

    @Value("${custom.rest.connection.read-timeout}")
    private int readTimeout;

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {

        return restTemplateBuilder
                .setConnectTimeout(Duration.ofSeconds(connectionTimeout))
                .setReadTimeout(Duration.ofSeconds(readTimeout))
                .build();
    }
}

and on application.config

#connection
custom.rest.connection.connection-request-timeout=10000
custom.rest.connection.connect-timeout=10000
custom.rest.connection.read-timeout=10000
ErEcTuS
  • 777
  • 1
  • 14
  • 33
  • Have you tried `@Transactional(timeout = 15)`? Documentation about this can be found [here](https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html#transaction-declarative-attransactional-settings). Also [example](https://stackoverflow.com/a/52290279/4156237) :P – Tuan Hoang Nov 22 '19 at 03:53
  • This falls in the Circuit Breaker Pattern, Hystrix timeout of timeoutInMilliseconds should fit the scenario. – brijesh Nov 22 '19 at 05:02
  • It looks like you've set a timeout of 10,000 seconds, which is not what you planned. :) – Graham Lea Nov 22 '19 at 09:57
  • Note that, with a connection timeout of 10 seconds and a read timeout of 10 seconds, you could potentially wait a total of 20 seconds for a response. – Graham Lea Nov 22 '19 at 09:57
  • 1
    Also, it's important to know that read timeout is not a timeout for the server to complete sending a response, but to *start* sending a response. If there's a lot of data or the connection is slow, your request could still take longer than the specified timeout without ever timing out. – Graham Lea Nov 22 '19 at 09:57
  • @btreport, indeed Circuit Breaker Pattern, Hystrix seems like it was built for this type of situation – ErEcTuS Nov 22 '19 at 10:38