1

我需要创建一个库,其中包含同步和异步方法。

我的图书馆的核心逻辑 -

客户将使用我们的库,他们将通过传递DataKey构建器对象来调用它。然后,我们将使用该对象构造一个 URL,并通过执行该 URL 对该 URL 进行 HTTP 客户端调用,在我们将响应作为 JSON 字符串返回后,我们将通过创建对象DataKey将该 JSON 字符串原样发送回我们的客户DataResponse.

我将有同步和异步方法。一些客户会调用该executeSynchronous方法来获得相同的功能,而一些客户会调用我们executeAsynchronous的方法,并且使用该executeAsynchronous方法,他们将调用future.get那里的代码本身。

下面是我的界面 -

public interface Client {

    // for synchronous
    public DataResponse executeSynchronous(DataKey dataKey);

    // for asynchronous
    public Future<DataResponse> executeAsynchronous(DataKey dataKey);
}

下面是我的DataResponse课-

public class DataResponse {

    private String response;
    private DataErrorEnum error;
    private DataStatusEnum status;

    // constructor here

    // and getters here
}

下面是我的DataStatusEnum课-

public enum DataStatusEnum {
    SUCCESS, ERROR;
}

下面是我的DataErrorEnum课-

public enum DataErrorEnum {
    NONE(200, "NONE", "Response is success."),
    SERVER_DOWN(3145, "Server Down", "some long message here which can give more details."),
    CLIENT_ERROR(3123, "Client Error", "some long message here which can give more details."),
    TIMEOUT_ON_CLIENT(3187, "Client Timeout", "some long message here which can give more details.");

    private final int code;
    private final String status;
    private final String description;

    // constructor and getters here
}

然后我有我DataClient的实现上述Client接口。

public class DataClient implements Client {

    private RestTemplate restTemplate = new RestTemplate();
    private ExecutorService service = Executors.newFixedThreadPool(10);

    // for synchronous call
    @Override
    public DataResponse executeSynchronous(DataKey dataKey) {
        DataResponse dataResponse = null;

        try {
            Future<String> future = executeAsynchronous(dataKey);
            dataResponse = future.get(dataKey.getTimeout(), TimeUnit.MILLISECONDS);
        } catch (TimeoutException ex) {
            PotoLogging.logErrors(ex, DataErrorEnum.TIMEOUT_ON_CLIENT, dataKey);
            dataResponse = new DataResponse(null, DataErrorEnum.TIMEOUT_ON_CLIENT, DataStatusEnum.ERROR);
        } catch (Exception ex) {
            PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, dataKey);
            dataResponse = new DataResponse(null, DataErrorEnum.CLIENT_ERROR, DataStatusEnum.ERROR);
        }

        return dataResponse;
    }

    //for asynchronous call
    @Override
    public Future<DataResponse> executeAsynchronous(DataKey dataKey) {
        Future<DataResponse> future = null;

        try {
            Task task = new Task(dataKey, restTemplate);
            future = executor.submit(task);
        } catch (Exception ex) {
            PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, dataKey);
        }

        return future;
    }
}

现在下面是我的简单类,它将执行实际任务 -

public class Task implements Callable<DataResponse> {

    private DataKey dataKey;
    private RestTemplate restTemplate;

    public Task(DataKey dataKey, RestTemplate restTemplate) {
        this.dataKey = dataKey;
        this.restTemplate = restTemplate;
    }

    @Override
    public DataResponse call() throws Exception {
        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, dataKey);
            dataResponse = new DataResponse(null, DataErrorEnum.SERVER_DOWN, DataStatusEnum.ERROR);
        } catch (Exception ex) {
            PotoLogging.logErrors(ex, DataErrorEnum.CLIENT_ERROR, dataKey);
            dataResponse = new DataResponse(null, DataErrorEnum.CLIENT_ERROR, DataStatusEnum.ERROR);
        }

        return dataResponse;
    }

    // create a URL by using dataKey object
    private String createURL() {
        String url = somecode;

        return url;
    }
}

问题陈述:-

正如我上面提到的,一些客户会调用executeSynchronous方法来获取他们在对象中传递的那个用户 ID 的数据,DataKey而一些客户会用对象调用executeAsynchronous方法,DataKey但在后一种情况下,他们会future.get在他们的代码库中做。

如果你看到我的executeSynchronous方法,我future.get在调用executeAsynchronous方法之后做,如果有的话TimeoutException,那么我正在使用PotoLogging我们公司特定的类进行日志记录,这些日志将转到这里的其他一些服务,我们用它来查看我们所有的错误日志仪表板。它主要取决于我们如何用什么名称记录它,以便我们可以在仪表板中看到这些名称。

现在的问题是我们公司内的客户也可以调用executeAsynchronous方法,但这意味着,他们将future.get在他们的代码库中执行,这也可能导致TimeoutException他们的代码,但我不能强迫他们以与我相同的方式登录。所以我的问题是——如果有人在他们的代码库中TimeoutException调用我的库的方法,我有什么办法可以得到回调,如果有的话,我可以这样记录它——executeAsynchronous

PotoLogging.logErrors(ex, DataErrorEnum.TIMEOUT_ON_CLIENT, dataKey);

我需要这样做,以便我的图书馆可以TimeoutException按照我们想要的方式登录我们公司拥有的工具。否则,我需要告诉每个客户像这样记录它,以便我们可以在仪表板中看到它。如何从异步调用中获取回调并仍然利用异步的所有功能?

做这个的最好方式是什么?

4

1 回答 1

2

Future只是一个界面。提供包装服务返回的实例的实现。使其将所有调用委托给实际Future调用,并使用适当的 try-catch 块包装这些调用。

Future<DataResponse> wrapper = new Future<DataResponse>() {
    private final Future<DataResponse> delegate = future;

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return delegate.cancel(mayInterruptIfRunning);
    }

    @Override
    public boolean isCancelled() {
        return delegate.isCancelled();
    }

    @Override
    public boolean isDone() {
        return delegate.isDone();
    }

    @Override
    public DataResponse get() throws InterruptedException, ExecutionException {
        DataResponse dataResponse = null;
        try {
            delegate.get();
        } catch (TimeoutException ex) {
            PotoLogging.logErrors(ex, DataErrorEnum.TIMEOUT_ON_CLIENT, dataKey);
            dataResponse = new DataResponse(null, DataErrorEnum.TIMEOUT_ON_CLIENT, DataStatusEnum.ERROR);
        }
        return dataResponse;
    }

    @Override
    public DataResponse get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        DataResponse dataResponse = null;
        try {
            delegate.get(timeout, unit);
        } catch (TimeoutException ex) {
            PotoLogging.logErrors(ex, DataErrorEnum.TIMEOUT_ON_CLIENT, dataKey);
            dataResponse = new DataResponse(null, DataErrorEnum.TIMEOUT_ON_CLIENT, DataStatusEnum.ERROR);
        }
        return dataResponse;
    }
};
return wrapper;
于 2015-03-12T23:19:16.923 回答