Java 3秒后放弃方法的执行
我有这个方法:Java 3秒后放弃方法的执行,java,java-8,timeout,Java,Java 8,Timeout,我有这个方法: public ResponseDTO getManh(QueryDTO manhQueryDTO) { return requestHttpService.post(ManhQueryResponseDTO.class, uri(properties.getManhattan().getUrl(), REQUEST_QUERY), manhQueryDTO); } 我需要在3秒钟后放弃方法的执行 有人知道该怎么做吗?我创建一个对象只是
public ResponseDTO getManh(QueryDTO manhQueryDTO) {
return requestHttpService.post(ManhQueryResponseDTO.class,
uri(properties.getManhattan().getUrl(), REQUEST_QUERY), manhQueryDTO);
}
我需要在3秒钟后放弃方法的执行
有人知道该怎么做吗?我创建一个对象只是为了控制这个调用-tryTimeout() 这是控制对象:
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();
}
}
private responsed to tryTimeout(){
最终持续时间超时=持续时间秒(1);
ExecutorService executor=Executors.newSingleThreadExecutor();
final Future handler=executor.submit(new Callable()){
@凌驾
public ResponseDTO call()引发异常{
返回origin.serviceToCall();
}
});
试一试{
return handler.get(timeout.toMillis(),TimeUnit.ms);
}捕获(例外e){
记录器错误(“超时”);
}最后{
executor.shutdown();
}
}
您是否在寻找超时?如果是这样,这有帮助吗?我会试试。。。你帮了大忙!TKST没有关于如何执行此操作的信息?这是否回答了您的问题?
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();
}
}