Search in sources :

Example 6 with AsyncRpcResult

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);
    }
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) AppResponse(org.apache.dubbo.rpc.AppResponse) RpcException(org.apache.dubbo.rpc.RpcException) ExecutorService(java.util.concurrent.ExecutorService) URL(org.apache.dubbo.common.URL) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult) Result(org.apache.dubbo.rpc.Result) AsyncRpcResult(org.apache.dubbo.rpc.AsyncRpcResult)

Aggregations

AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)6 RpcException (org.apache.dubbo.rpc.RpcException)6 AppResponse (org.apache.dubbo.rpc.AppResponse)5 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)5 ExecutorService (java.util.concurrent.ExecutorService)3 RemotingException (org.apache.dubbo.remoting.RemotingException)3 TimeoutException (org.apache.dubbo.remoting.TimeoutException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExchangeClient (org.apache.dubbo.remoting.exchange.ExchangeClient)2 CompletionException (java.util.concurrent.CompletionException)1 URL (org.apache.dubbo.common.URL)1 Result (org.apache.dubbo.rpc.Result)1 FutureAdapter (org.apache.dubbo.rpc.protocol.dubbo.FutureAdapter)1