4

I wish to set my Spring Boot server timeout, say to 15 seconds. To be clear: I wish that if my server fails to respond within 15 seconds it will return an error response (something like what happens in Heroku, only there it is 30 seconds). How can I do that?

My problem actually originates from the fact that this service is a gateway and it calls another service, which sometimes takes a long time to response, and I can't control that service's timeout. Since I'm using an SDK files to call that server, I can't control the outgoing calls. Is there a way I can globally set a timeout for all outgoing calls?

Thank you

user2339344
  • 951
  • 2
  • 12
  • 22
  • 1
    Solves http://stackoverflow.com/questions/34852236/spring-boot-rest-api-request-timeout your first problem? – GandalfIX May 17 '16 at 17:59
  • I've tried that but it doesn't really work. Application crashes on an exception calling new `HttpComponentsClientHttpRequestFactory()`. I have found this http://stackoverflow.com/questions/13837012/spring-resttemplate-timeout, where on the 3rd answer refers to *Spring Boot* as I need, and now the program runs, but it doesn't stop requests after the given milliseconds. Have I missed some configuration? Any further examples / code / help will be appreciated. – user2339344 May 19 '16 at 19:33

1 Answers1

3

You can wrap your library into a extra thread and then directly join on that thread with timeout. Example see http://www.journaldev.com/1024/java-thread-join-example-with-explanation

GandalfIX
  • 261
  • 1
  • 3
  • 8
  • Well, that's a nice trick, but it's a trick. It's not an infrastructural or framework way of doing this. Especially that my service has lots of APIs it introduces, and each such call should be wrapped to run within a thread or such. I thought of that, but I'm looking for something more fundamental. – user2339344 May 19 '16 at 19:29
  • @user2339344 Actually, that is a perfectly fundamental solution. It kicks in and does the job cleanly. Maybe add some `ExecutorService` logic for thread pooling or some fancy `CompletableFuture` usage but that's it. Messing with that SDK will most probably bring more problems. – bekce Jan 18 '17 at 19:58