[英]abandon execution of a method after 3 seconds
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.我需要在 3 秒后放弃执行方法。
does anyone have any idea what to do?有谁知道该怎么做?
I create a object only to control this call - tryTimeout().我创建一个 object 只是为了控制这个调用 - 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.