I am considering a generic universal / revocable interface for asynchronous requests / responses. The requirements are as follows: he must:
- Asynchronous Call Support
- Cancel
- Be shared
- Support Request / Response
- Support for either returning in the current thread or processing the response in another response
So here is my first hit:
interface AsyncOperation<INPUT, OUTPUT> {
Future<OUTPUT> execute(INPUT in, AsyncCallback<OUTPUT> callback);
}
interface AsyncCallback<OUTPUT> {
void done(OUTPUT output);
}
Using:
operation.execute("Test", new AsyncCallback<String> {
public void done(String output) {
}
});
Future<String> future = operation.execute("Test", null);
try {
String result = future.get(1000);
} catch(TimeoutException ex) {
future.cancel();
}
disadvantages
- It's complicated.
- It supports only one query parameter - not too worried about this
- The only "ready" means that the exceptions must be passed through the "made" ones, this can be solved by having onSuccess and onException (and onFinally?) In AsyncCallback, but this will make it even more detailed
Google Protocol :
void [methodname](RpcController controller,
[RequestClass] request, RpcCallback<[ResponseClass]> callback);
?