定時任務超時處理方法
阿新 • • 發佈:2019-01-25
final ExecutorService exec = Executors.newFixedThreadPool(1);
Integer status=0;
Callable<Integer> call = new Callable<Integer>() {
public Integer call() throws Exception {
//開始執行任務
Integer type=Integer.valueOf(HTTPSRequestUtil.httpsGetSyncRequest(url));
return type;
}
};
try {
Future<Integer> future = exec.submit(call);
Integer obj = future.get(1000 * 5, TimeUnit.MILLISECONDS); //任務處理超時時間設為 1 秒
status=obj;
System.out.println("任務成功返回:" + status);
} catch (TimeoutException ex) {
status=10;
System.out.println("處理超時啦...."+ status);
} catch (Exception e) {
status=10;
System.out.println("處理失敗."+status);
}
// 關閉執行緒池
exec.shutdown();
return status;
Integer status=0;
Callable<Integer> call = new Callable<Integer>() {
public Integer call() throws Exception {
//開始執行任務
Integer type=Integer.valueOf(HTTPSRequestUtil.httpsGetSyncRequest(url));
return type;
}
};
try {
Future<Integer> future = exec.submit(call);
Integer obj = future.get(1000 * 5, TimeUnit.MILLISECONDS); //任務處理超時時間設為 1 秒
status=obj;
System.out.println("任務成功返回:" + status);
} catch (TimeoutException ex) {
status=10;
System.out.println("處理超時啦...."+ status);
} catch (Exception e) {
status=10;
System.out.println("處理失敗."+status);
}
// 關閉執行緒池
exec.shutdown();
return status;