如何用回调实现异步调用?

时间:2015-03-12 23:10:01

标签: java asynchronous callback synchronous futuretask

我需要创建一个库,在其中我将使用同步和异步方法。

我图书馆的核心逻辑 -

客户将使用我们的库,他们将通过传递DataKey构建器对象来调用它。然后,我们将使用该DataKey对象构造一个URL,并通过执行它来对该URL进行HTTP客户端调用,然后在我们将响应作为JSON字符串返回之后,我们将该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方法获取他们在DataKey对象中传递的用户ID的数据,有些客户会调用executeAsynchronous方法{ {1}}对象,但在后一种情况下,他们会在代码库中执行DataKey

如果您看到我的future.get方法,则在调用executeSynchronous方法后我正在future.get,如果有executeAsynchronous,那么我正在使用{{1}进行记录} class在我们公司中是特定的,并且日志将转到我们用于查看仪表板上所有错误日志的其他服务。它主要取决于我们如何使用名称记录它,以便我们可以在仪表板中看到这些名称。

现在问题是我们公司内的客户也可以调用TimeoutException方法,但这意味着,他们会在代码库中执行PotoLogging,这也会导致代码中出现executeAsynchronous但我不能强迫他们以与我一样的方式记录。所以我的问题是 - 如果有任何future.get,我有什么方法可以获得回调,这样我就可以像这样记录它,如果有人在他们的代码库中调用我的库的TimeoutException方法 - < / p>

TimeoutException

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

这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分: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;