use of org.apache.dubbo.rpc.AsyncRpcResult in project dubbo by alibaba.
the class InjvmInvoker method doInvoke.
@Override
public Result doInvoke(Invocation invocation) throws Throwable {
Exporter<?> exporter = InjvmProtocol.getExporter(delegateExporterMap, getUrl());
if (exporter == null) {
throw new RpcException("Service [" + key + "] not found.");
}
RpcContext.getContext().setRemoteAddress(LOCALHOST_VALUE, 0);
// Solve local exposure, the server opens the token, and the client call fails.
URL serverURL = exporter.getInvoker().getUrl();
boolean serverHasToken = serverURL.hasParameter(Constants.TOKEN_KEY);
if (serverHasToken) {
invocation.setAttachment(Constants.TOKEN_KEY, serverURL.getParameter(Constants.TOKEN_KEY));
}
if (isAsync(exporter.getInvoker().getUrl(), getUrl())) {
((RpcInvocation) invocation).setInvokeMode(InvokeMode.ASYNC);
// use consumer executor
ExecutorService executor = executorRepository.createExecutorIfAbsent(getUrl());
CompletableFuture<AppResponse> appResponseFuture = CompletableFuture.supplyAsync(() -> {
Result result = exporter.getInvoker().invoke(invocation);
if (result.hasException()) {
return new AppResponse(result.getException());
} else {
return new AppResponse(result.getValue());
}
}, executor);
// save for 2.6.x compatibility, for example, TraceFilter in Zipkin uses com.alibaba.xxx.FutureAdapter
FutureContext.getContext().setCompatibleFuture(appResponseFuture);
AsyncRpcResult result = new AsyncRpcResult(appResponseFuture, invocation);
result.setExecutor(executor);
return result;
} else {
return exporter.getInvoker().invoke(invocation);
}
}
Aggregations