-1

I've this method:

public ResponseDTO getManh(QueryDTO manhQueryDTO) {
  return requestHttpService.post(ManhQueryResponseDTO.class,
                    uri(properties.getManhattan().getUrl(), REQUEST_QUERY), manhQueryDTO);
}

I need abandon execution of a method after 3 seconds.

does anyone have any idea what to do?

Marvin
  • 13,325
  • 3
  • 51
  • 57

1 Answers1

0

I create a object only to control this call - tryTimeout().

    ResponseDTO responseDTO = tryTimeout();

this is the control obj:

  private ResponseDTO tryTimeout() {

    final Duration timeout = Duration.ofSeconds(1);
    ExecutorService executor = Executors.newSingleThreadExecutor();

    final Future<ResponseDTO> handler = executor.submit(new Callable() {
        @Override
        public ResponseDTO call() throws Exception {
            return origin.serviceToCall();
        }
    });

    try {
        return handler.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
    } catch (Exception e) {

        LOGGER.error("TIMEOUT");

    } finally {
        executor.shutdown();
    }

}