我需要创建一个库,其中包含同步和异步方法。
executeSynchronous()
- 等到我有结果,返回结果。executeAsynchronous()
- 立即返回一个 Future,如果需要,可以在其他事情完成后处理它。
我的图书馆的核心逻辑
客户将使用我们的库,他们将通过传递DataKey
构建器对象来调用它。然后,我们将使用该对象构造一个 URL,并通过执行该 URL 对该 URL 进行 HTTP 客户端调用,在我们将响应作为 JSON 字符串返回后,我们将通过创建对象DataKey
将该 JSON 字符串原样发送回我们的客户DataResponse
. 有些客户会打电话executeSynchronous()
,有些客户可能会打电话executeAsynchronous()
,这就是为什么我需要在我的库中分别提供两种方法。
界面:
public interface Client {
// for synchronous
public DataResponse executeSynchronous(DataKey key);
// for asynchronous
public Future<DataResponse> executeAsynchronous(DataKey key);
}
然后我有我DataClient
的实现上述Client
接口:
public class DataClient implements Client {
private RestTemplate restTemplate = new RestTemplate();
private ExecutorService executor = Executors.newFixedThreadPool(10);
// for synchronous call
@Override
public DataResponse executeSynchronous(DataKey key) {
DataResponse dataResponse = null;
Future<DataResponse> future = null;
try {
future = executeAsynchronous(key);
dataResponse = future.get(key.getTimeout(), TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
PotoLogging.logErrors(ex, DataErrorEnum.TIMEOUT_ON_CLIENT, key);
dataResponse = new DataResponse(null, DataErrorEnum.TIMEOUT_ON_CLIENT, DataStatusEnum.ERROR);
// does this look right the way I am doing it?
future.cancel(true); // terminating tasks that have timed out.
} catch (Exception ex) {
PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, key);
dataResponse = new DataResponse(null, DataErrorEnum.CLIENT_ERROR, DataStatusEnum.ERROR);
}
return dataResponse;
}
//for asynchronous call
@Override
public Future<DataResponse> executeAsynchronous(DataKey key) {
Future<DataResponse> future = null;
try {
Task task = new Task(key, restTemplate);
future = executor.submit(task);
} catch (Exception ex) {
PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, key);
}
return future;
}
}
将执行实际任务的简单类:
public class Task implements Callable<DataResponse> {
private DataKey key;
private RestTemplate restTemplate;
public Task(DataKey key, RestTemplate restTemplate) {
this.key = key;
this.restTemplate = restTemplate;
}
@Override
public DataResponse call() {
DataResponse dataResponse = null;
String response = null;
try {
String url = createURL();
response = restTemplate.getForObject(url, String.class);
// it is a successful response
dataResponse = new DataResponse(response, DataErrorEnum.NONE, DataStatusEnum.SUCCESS);
} catch (RestClientException ex) {
PotoLogging.logErrors(ex, DataErrorEnum.SERVER_DOWN, key);
dataResponse = new DataResponse(null, DataErrorEnum.SERVER_DOWN, DataStatusEnum.ERROR);
} catch (Exception ex) {
PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, key);
dataResponse = new DataResponse(null, DataErrorEnum.CLIENT_ERROR, DataStatusEnum.ERROR);
}
return dataResponse;
}
// create a URL by using key object
private String createURL() {
String url = somecode;
return url;
}
}
问题陈述:-
当我开始研究这个解决方案时,我并没有终止已经超时的任务。我正在向客户端报告超时,但任务继续在线程池中运行(可能会长时间占用我有限的 10 个线程之一)。cancel
所以我在网上做了一些研究,我发现我可以通过使用on取消那些超时的任务future
,如下所示 -
future.cancel(true);
但我想确定一下,我在executeSynchronous
取消已超时任务的方法中所做的方式是否正确?
由于我正在调用which 如果任务仍在队列cancel()
中Future
,它将停止运行,所以我不确定我在做什么是正确的还是不正确的?这样做的正确方法是什么?
如果有更好的方法,那么任何人都可以提供一个例子吗?