Java Code Examples for org.apache.dubbo.rpc.Result#getException()

The following examples show how to use org.apache.dubbo.rpc.Result#getException() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
@Override
public Result onResponse(Result result, Invoker<?> invoker, Invocation invocation) {
    String spanKey = getTracerSpanMapKey(invoker);
    try {
        // only the asynchronous callback to print
        boolean isAsync = RpcUtils.isAsync(invoker.getUrl(), invocation);
        if (!isAsync) {
            return result;
        }
        if (TracerSpanMap.containsKey(spanKey)) {
            SofaTracerSpan sofaTracerSpan = TracerSpanMap.get(spanKey);
            // to build tracer instance
            if (dubboConsumerSofaTracer == null) {
                this.dubboConsumerSofaTracer = DubboConsumerSofaTracer
                    .getDubboConsumerSofaTracerSingleton();
            }
            String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
            if (result.hasException()) {
                if (result.getException() instanceof RpcException) {
                    resultCode = Integer.toString(((RpcException) result.getException())
                        .getCode());
                    sofaTracerSpan.setTag(CommonSpanTags.RESULT_CODE, resultCode);
                } else {
                    resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
                }
            }
            // add elapsed time
            appendElapsedTimeTags(invocation, sofaTracerSpan, result, true);
            dubboConsumerSofaTracer.clientReceiveTagFinish(sofaTracerSpan, resultCode);
        }
    } finally {
        if (TracerSpanMap.containsKey(spanKey)) {
            TracerSpanMap.remove(spanKey);
        }
    }
    return result;
}
 
Example 2
Source File: DubboInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    Object ret) throws Throwable {
    Result result = (Result) ret;
    if (result != null && result.getException() != null) {
        dealException(result.getException());
    }

    ContextManager.stopSpan();
    return ret;
}
 
Example 3
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
/**
 * rpc client handler
 * @param invoker
 * @param invocation
 * @return
 */
private Result doServerFilter(Invoker<?> invoker, Invocation invocation) {
    if (dubboProviderSofaTracer == null) {
        this.dubboProviderSofaTracer = DubboProviderSofaTracer
            .getDubboProviderSofaTracerSingleton();
    }
    SofaTracerSpan sofaTracerSpan = serverReceived(invocation);
    appendRpcServerSpanTags(invoker, sofaTracerSpan);
    Result result;
    Throwable exception = null;
    try {
        result = invoker.invoke(invocation);
        if (result == null) {
            return null;
        } else {
            appendElapsedTimeTags(invocation, sofaTracerSpan, result, false);
        }
        if (result.hasException()) {
            exception = result.getException();
        }
        return result;
    } catch (RpcException e) {
        exception = e;
        throw e;
    } catch (Throwable t) {
        exception = t;
        throw new RpcException(t);
    } finally {
        String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
        if (exception != null) {
            if (exception instanceof RpcException) {
                sofaTracerSpan.setTag(Tags.ERROR.getKey(), exception.getMessage());
                RpcException rpcException = (RpcException) exception;
                if (rpcException.isBiz()) {
                    resultCode = String.valueOf(rpcException.getCode());
                }
            } else {
                resultCode = SofaTracerConstant.RESULT_CODE_ERROR;
            }
        }
        dubboProviderSofaTracer.serverSend(resultCode);
    }
}
 
Example 4
Source File: DubboHmilyTransactionFilter.java    From hmily with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(final Invoker<?> invoker, final Invocation invocation) throws RpcException {
    String methodName = invocation.getMethodName();
    Class clazz = invoker.getInterface();
    Class[] args = invocation.getParameterTypes();
    final Object[] arguments = invocation.getArguments();
    Method method = null;
    Hmily hmily = null;
    try {
        converterParamsClass(args, arguments);
        method = clazz.getMethod(methodName, args);
        hmily = method.getAnnotation(Hmily.class);
    } catch (Exception ex) {
        LOGGER.error("hmily find method error {} ", ex);
    }
    if (Objects.nonNull(hmily)) {
        try {
            final HmilyTransactionContext hmilyTransactionContext = HmilyTransactionContextLocal.getInstance().get();
            if (Objects.nonNull(hmilyTransactionContext)) {
                RpcMediator.getInstance().transmit(RpcContext.getContext()::setAttachment, hmilyTransactionContext);
                final Result result = invoker.invoke(invocation);
                //if result has not exception
                if (!result.hasException()) {
                    final HmilyParticipant hmilyParticipant = buildParticipant(hmilyTransactionContext, hmily, method, clazz, arguments, args);
                    if (hmilyTransactionContext.getRole() == HmilyRoleEnum.INLINE.getCode()) {
                        hmilyTransactionExecutor.registerByNested(hmilyTransactionContext.getTransId(),
                                hmilyParticipant);
                    } else {
                        hmilyTransactionExecutor.enlistParticipant(hmilyParticipant);
                    }
                } else {
                    throw new HmilyRuntimeException("rpc invoke exception{}", result.getException());
                }
                return result;
            }
            return invoker.invoke(invocation);
        } catch (RpcException e) {
            e.printStackTrace();
            throw e;
        }
    } else {
        return invoker.invoke(invocation);
    }
}
 
Example 5
Source File: TracingFilter.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
  if (!isInit) return invoker.invoke(invocation);
  TraceContext invocationContext = currentTraceContext.get();

  RpcContext rpcContext = RpcContext.getContext();
  Kind kind = rpcContext.isProviderSide() ? Kind.SERVER : Kind.CLIENT;
  Span span;
  DubboRequest request;
  if (kind.equals(Kind.CLIENT)) {
    // When A service invoke B service, then B service then invoke C service, the parentId of the
    // C service span is A when read from invocation.getAttachments(). This is because
    // AbstractInvoker adds attachments via RpcContext.getContext(), not the invocation.
    // See org.apache.dubbo.rpc.protocol.AbstractInvoker(line 141) from v2.7.3
    Map<String, String> attachments = RpcContext.getContext().getAttachments();
    DubboClientRequest clientRequest = new DubboClientRequest(invoker, invocation, attachments);
    request = clientRequest;
    span = clientHandler.handleSendWithParent(clientRequest, invocationContext);
  } else {
    DubboServerRequest serverRequest = new DubboServerRequest(invoker, invocation);
    request = serverRequest;
    span = serverHandler.handleReceive(serverRequest);
  }

  boolean isSynchronous = true;
  Scope scope = currentTraceContext.newScope(span.context());
  Result result = null;
  Throwable error = null;
  try {
    result = invoker.invoke(invocation);
    error = result.getException();
    CompletableFuture<Object> future = rpcContext.getCompletableFuture();
    if (future != null) {
      isSynchronous = false;
      // NOTE: We don't currently instrument CompletableFuture, so callbacks will not see the
      // invocation context unless they use an executor instrumented by CurrentTraceContext
      // If we later instrument this, take care to use the correct context depending on RPC kind!
      future.whenComplete(FinishSpan.create(this, request, result, span));
    }
    return result;
  } catch (Throwable e) {
    propagateIfFatal(e);
    error = e;
    throw e;
  } finally {
    if (isSynchronous) FinishSpan.finish(this, request, result, error, span);
    scope.close();
  }
}