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

The following examples show how to use org.apache.dubbo.rpc.Result#hasException() . 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: LegacyBlockFilter.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    RpcContext context = RpcContext.getContext();
    String filters = (String) context.getAttachment("filters");
    if (StringUtils.isEmpty(filters)) {
        filters = "";
    }
    filters += " legacy-block-filter";
    context.setAttachment("filters", filters);

    Result result = invoker.invoke(invocation);

    logger.info("This is the default return value: " + result.getValue());

    if (result.hasException()) {
        System.out.println("LegacyBlockFilter: This will only happen when the real exception returns: " + result.getException());
        logger.warn("This will only happen when the real exception returns", result.getException());
    }

    logger.info("LegacyBlockFilter: This msg should not be blocked.");
    return result;
}
 
Example 2
Source File: Resilience4jCircuitBreakerFilter.java    From dubbo-samples with Apache License 2.0 6 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    System.out.println("**************** Enter CircuitBreaker ****************");
    long countLong = count.incrementAndGet();
    long start = 0;
    try {
        CircuitBreakerUtils.isCallPermitted(circuitBreaker);
        start = System.nanoTime();
        Result result = invoker.invoke(invocation);
        if (result.hasException()) {
            doThrowException(result.getException(), start);
            return result;
        }
        long durationInNanos = System.nanoTime() - start;
        circuitBreaker.onSuccess(durationInNanos);
        return result;
    } catch (CircuitBreakerOpenException cbo) {

        doCircuitBreakerOpenException(cbo, countLong, breakCount.incrementAndGet());
        throw cbo;
    } catch (Throwable throwable) {
        doThrowException(throwable, start);
        throw throwable;
    }
}
 
Example 3
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 4
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 5
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);
    }
}