2

I am launching a long process in al web request in Spring MVC and I would like to terminate it if the thread takes a long time. Are there anyway to set a maximum time to kill the thread in a Spring controller if it is too long? Is this the best approach or are there any other better way to do it? I read here that the thread can not be killed by the app-server without the colaboration of the thread.

I found Guava TimeLimiter but not sure how to use it.

Thanks.

Community
  • 1
  • 1
Esteban S
  • 1,859
  • 5
  • 22
  • 43
  • 1
    You use it like described. `TimeLimiter` essentially does what should (or rather has to) be done anyway: Run a task asynchronously. – a better oliver Mar 10 '16 at 10:03

1 Answers1

0

Finally, I did it with a Timelimiter:

        TimeLimiter limiter = new SimpleTimeLimiter();
        limiter.callWithTimeout(new Callable<String>() {
            @Override
            public String call() throws IOException {
                return mymethod ();
            }
          }, timelimit, TimeUnit.SECONDS, false);

If the method take more than time than the timelimit, with throw an UncheckedTimeoutException exception that must be managed.

Esteban S
  • 1,859
  • 5
  • 22
  • 43