1

I have a JAX-RS API that does a long duration work and the API is being called via ajax call by the client. The client is getting 503 status - Service Unavailable after 50 seconds.

How can I increase this timeout value. I tried increasing the connection timeout in tomcat (which is hosting API). I also tried adding timeout in ajax call but that also didn't work.

Vinod
  • 1,076
  • 2
  • 16
  • 33
  • You need to set timeout values in the client. There are different ways to set the timeouts depending on implementation and version of the client. Which client are you using? Can you share the code? – Aditya Abhas Dec 30 '18 at 12:50

1 Answers1

0

You could use the Suspended annotation and create a TimeoutHandler .

Not sure if you need to increase the timeout in tomcat using this example.

    public class Resource {

        private Executor executor = Executors.newSingleThreadExecutor();

        @GET
        public void asyncGet(@Suspended final AsyncResponse asyncResponse) {

            asyncResponse.setTimeoutHandler(new TimeoutHandler() {
                @Override
                public void handleTimeout(AsyncResponse asyncResponse) {
                    asyncResponse.resume("Processing timeout.");
                    executor.shutdown();
                }
            });

            asyncResponse.setTimeout(60, TimeUnit.SECONDS);
            executor.submit(() -> {
                String result = someService.expensiveOperation();
                asyncResponse.resume(result);
                executor.shutdown();
            });
        }
    }

Jersey documentation here

thomas77
  • 1,100
  • 13
  • 27
  • I tried TimeoutHandler and set timeout to 5 minutes but still client received 503 - service unavailable after 50 seconds. – Vinod Jan 02 '19 at 04:55