4

Considering the following code:

@RestController
@RequestMapping("/timeout")
public class TestController {

    @Autowired
    private TestService service;

    @GetMapping("/max10secs")
    public String max10secs() {
        //In some cases it can take more than 10 seconds
        return service.call();
    }
}

@Service
public class TestService {

    public String call() {
        //some business logic here
        return response;
    }
}

What I want to accomplish is that if the method call from the TestService takes more than 10 seconds I want to cancel it and generate a response with a HttpStatus.REQUEST_TIMEOUT code.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Julio Villane
  • 994
  • 16
  • 28

2 Answers2

6

What I managed to do, but I don't know if there are any conceptual or practical flaws is what it follows...

First, the configuration of spring-async

@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {

    @Bean(name = "threadPoolTaskExecutor")
    public Executor threadPoolTaskExecutor() {

        ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
        pool.setCorePoolSize(10);
        pool.setMaxPoolSize(10);
        pool.setWaitForTasksToCompleteOnShutdown(true);
        return pool;
    }

    @Override
    public Executor getAsyncExecutor() {
        return new SimpleAsyncTaskExecutor();
    }
}

And next, the Controller and Service modifications:

@RestController
@RequestMapping("/timeout")
public class TestController {

    @Autowired
    private TestService service;

    @GetMapping("/max10secs")
    public String max10secs() throws InterruptedException, ExecutionException {
        Future<String> futureResponse = service.call();
        try {
            //gives 10 seconds to finish the methods execution
            return futureResponse.get(10, TimeUnit.SECONDS);
        } catch (TimeoutException te) {
            //in case it takes longer we cancel the request and check if the method is not done
            if (futureResponse.cancel(true) || !futureResponse.isDone())
                throw new TestTimeoutException();
            else {
                return futureResponse.get();
            }
        }
    }
}

@Service
public class TestService {

    @Async("threadPoolTaskExecutor")
    public Future<String> call() {
        try{
            //some business logic here
            return new AsyncResult<>(response);
        } catch (Exception e) {
            //some cancel/rollback logic when the request is cancelled
            return null;
        }
    }
}

And finally generate the TestTimeoutException:

@ResponseStatus(value = HttpStatus.REQUEST_TIMEOUT, reason = "too much time")
public class TestTimeoutException extends RuntimeException{ }
Julio Villane
  • 994
  • 16
  • 28
  • This is a nice approach. But i would prefer to solve this using a circuit breaker like Hystrix. In hystrix, you can specify the timeout value. It is fairly simple to integrate Hystrix – pvpkiran Aug 30 '18 at 13:56
2

There is another solution via DeferredResult.

TestController.java

@RestController
@RequestMapping("/timeout")
public class TestController
{

    @Autowired
    private TestService service;


    @GetMapping("/max10secs")
    public DeferredResult<String> max10secs()
    {
        //In some cases it can take more than 10 seconds
        return service.call();
    }
}

TestService.java

@Service
public class TestService
{

    public DeferredResult<String> call()
    {
        DeferredResult<String> result = new DeferredResult(10000L);
        //some business logic here
        result.onTimeout(()->{
           // do whatever you want there
        });
        result.setResult("test");
        return result;
    }
}

This way, controller will return actual result only when you call result.setResult("test");.

As you can see, in case of timeout (value for timeout is defined in constructor of DeferredResult object in milliseconds) there will be a callback executed where you can throw any exception, or return another object(HttpStatus.REQUEST_TIMEOUT in your case).

You can read about the DeferredResult in Spring here.

Pavel Gordon
  • 192
  • 2
  • 11