Java Code Examples for com.alibaba.dubbo.rpc.Result#hasException()

The following examples show how to use com.alibaba.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: CacheFilter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.CACHE_KEY))) {
        Cache cache = cacheFactory.getCache(invoker.getUrl().addParameter(Constants.METHOD_KEY, invocation.getMethodName()));
        if (cache != null) {
            String key = StringUtils.toArgumentString(invocation.getArguments());
            if (cache != null && key != null) {
                Object value = cache.get(key);
                if (value != null) {
                    return new RpcResult(value);
                }
                Result result = invoker.invoke(invocation);
                if (! result.hasException()) {
                    cache.put(key, result.getValue());
                }
                return result;
            }
        }
    }
    return invoker.invoke(invocation);
}
 
Example 2
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
/**
 * finish tracer under async
 * @param result
 * @param sofaTracerSpan
 * @param invocation
 */
public static void doFinishTracerUnderAsync(Result result, SofaTracerSpan sofaTracerSpan,
                                            Invocation invocation) {
    DubboConsumerSofaTracer dubboConsumerSofaTracer = DubboConsumerSofaTracer
        .getDubboConsumerSofaTracerSingleton();
    // to build tracer instance
    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);
}
 
Example 3
Source File: CacheFilter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.CACHE_KEY))) {
        Cache cache = cacheFactory.getCache(invoker.getUrl().addParameter(Constants.METHOD_KEY, invocation.getMethodName()));
        if (cache != null) {
            String key = StringUtils.toArgumentString(invocation.getArguments());
            if (cache != null && key != null) {
                Object value = cache.get(key);
                if (value != null) {
                    return new RpcResult(value);
                }
                Result result = invoker.invoke(invocation);
                if (! result.hasException()) {
                    cache.put(key, result.getValue());
                }
                return result;
            }
        }
    }
    return invoker.invoke(invocation);
}
 
Example 4
Source File: CacheFilter.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.CACHE_KEY))) {
        Cache cache = cacheFactory.getCache(invoker.getUrl().addParameter(Constants.METHOD_KEY, invocation.getMethodName()));
        if (cache != null) {
            String key = StringUtils.toArgumentString(invocation.getArguments());
            Object value = cache.get(key);
            if (value != null) {
                return new RpcResult(value);
            }
            Result result = invoker.invoke(invocation);
            if (! result.hasException()) {
                cache.put(key, result.getValue());
            }
            return result;
        }
    }
    return invoker.invoke(invocation);
}
 
Example 5
Source File: CacheFilter.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.CACHE_KEY))) {
        Cache cache = cacheFactory.getCache(invoker.getUrl().addParameter(Constants.METHOD_KEY, invocation.getMethodName()));
        if (cache != null) {
            String key = StringUtils.toArgumentString(invocation.getArguments());
            if (cache != null && key != null) {
                Object value = cache.get(key);
                if (value != null) {
                    return new RpcResult(value);
                }
                Result result = invoker.invoke(invocation);
                if (! result.hasException()) {
                    cache.put(key, result.getValue());
                }
                return result;
            }
        }
    }
    return invoker.invoke(invocation);
}
 
Example 6
Source File: CacheFilter.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (cacheFactory != null && ConfigUtils.isNotEmpty(invoker.getUrl().getMethodParameter(invocation.getMethodName(), Constants.CACHE_KEY))) {
        Cache cache = cacheFactory.getCache(invoker.getUrl(), invocation);
        if (cache != null) {
            String key = StringUtils.toArgumentString(invocation.getArguments());
            Object value = cache.get(key);
            if (value != null) {
                return new RpcResult(value);
            }
            Result result = invoker.invoke(invocation);
            if (!result.hasException() && result.getValue() != null) {
                cache.put(key, result.getValue());
            }
            return result;
        }
    }
    return invoker.invoke(invocation);
}
 
Example 7
Source File: FutureFilter.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
private void syncCallback(final Invoker<?> invoker, final Invocation invocation, final Result result) {
    if (result.hasException()) {
        fireThrowCallback(invoker, invocation, result.getException());
    } else {
        fireReturnCallback(invoker, invocation, result.getValue());
    }
}
 
Example 8
Source File: CacheFilter.java    From dubbo-ext with Apache License 2.0 5 votes vote down vote up
public Result invoke(Invoker<?> invoker, Invocation invo)
    throws RpcException {
    Result result;
    long start = System.currentTimeMillis();
    String method = invo.getMethodName();
    StringBuilder key = new StringBuilder(120);
    key.append(invo.getInvoker().getInterface().getName()).append(".").append(method);
    
    for (String name : methodPrefix) {
        if (method.startsWith(name)) {
            key.append("[");
            Object[] args = invo.getArguments();
            for (int i = 0; i < args.length; i++) {
                key.append(args[i]).append(",");
            }
            key.append("]");
            String cacheKey = key.toString();
            Object value = cache.get(cacheKey);
            if (value != null) {
                logger.info("cache hit:{},time:{}", cacheKey, (System.currentTimeMillis() - start));
                return new RpcResult(value);
            }
            result = invoker.invoke(invo);
            if (!result.hasException()) {
                cache.put(cacheKey, result.getValue());
            }
            logger.info("call server:{} time:{}", cacheKey, (System.currentTimeMillis() - start));
            return result;
        }
    }
    result = invoker.invoke(invo);
    logger.info("call server:{} time:{}", key, (System.currentTimeMillis() - start));
    return result;
}
 
Example 9
Source File: FutureFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void syncCallback(final Invoker<?> invoker, final Invocation invocation, final Result result) {
    if (result.hasException()) {
        fireThrowCallback(invoker, invocation, result.getException());
    } else {
        fireReturnCallback(invoker, invocation, result.getValue());
    }
}
 
Example 10
Source File: FutureFilter.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
private void syncCallback(final Invoker<?> invoker, final Invocation invocation, final Result result) {
    if (result.hasException()) {
        fireThrowCallback(invoker, invocation, result.getException());
    } else {
        fireReturnCallback(invoker, invocation, result.getValue());
    }
}
 
Example 11
Source File: DubboClientParser.java    From brave-instrumentation-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public void response(DubboAdapter adapter, Result rpcResult, SpanCustomizer customizer) {
  if (!rpcResult.hasException()) {
    customizer.tag("consumer.result", "true");
  } else {
    customizer.tag("consumer.exception", rpcResult.getException().getMessage());
  }
}
 
Example 12
Source File: DubboServerParser.java    From brave-instrumentation-dubbo with Apache License 2.0 5 votes vote down vote up
@Override
public void response(DubboAdapter adapter, Result rpcResult, SpanCustomizer customizer) {
  if (!rpcResult.hasException()) {
    customizer.tag("provider.result", "true");
  } else {
    customizer.tag("provider.exception", rpcResult.getException().getMessage());
  }
}
 
Example 13
Source File: Utils.java    From spring-boot-starter-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * 对服务端的异常进行编码
 * 
 * @param relust
 * @return
 */
protected final static Result encoderException(Result relust) {
	if (relust != null && relust.hasException()) {
		Throwable throwable = relust.getException();
		if (exceptions.contains(throwable.getClass())) {
			log.debug("{}异常被转化为Exception输出,用来通用异常处理....",throwable.getClass());
			return new RpcResult(new Exception(TRANSFORM_EXCEPTION_MESSAGE, throwable));
		}
	}
	return relust;
}
 
Example 14
Source File: Utils.java    From spring-boot-starter-dubbo with Apache License 2.0 5 votes vote down vote up
protected final static Result decodeException(Result relust) {
	if (relust != null && relust.hasException()) {
		Throwable throwable = relust.getException();
		if (throwable.getClass().equals(Exception.class) && TRANSFORM_EXCEPTION_MESSAGE.equals(throwable.getMessage())) {
			throwable = throwable.getCause();
			if (throwable != null) {
				log.debug("被包装的异常{},解开包装...", throwable.getClass());
				return new RpcResult(throwable);
			}
		}
	}
	return relust;
}
 
Example 15
Source File: DubboController.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Object create(Request req, Response res) throws JsonProcessingException {
	Map<String, String> headerMap = new HashMap<String, String>();
	for (String each : req.getHeaderNames()) {
		headerMap.put(each, req.getHeader(each));
	}
	String serviceName = headerMap.remove(RestExpressProtocol.SERVICE_KEY);
	String methodName = headerMap.remove(RestExpressProtocol.METHOD_KEY);
	InvokerBean<?> invokerBean = SERVICE_MAPPING.get(serviceName);
	Result dubboResult = invokerBean.invoke(methodName, req, headerMap);
	if (dubboResult.hasException()) {
		return MAPPER.writeValueAsString(dubboResult.getException());
	}
	return MAPPER.writeValueAsString(dubboResult.getValue());
}
 
Example 16
Source File: FutureFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void syncCallback(final Invoker<?> invoker, final Invocation invocation, final Result result) {
    if (result.hasException()) {
        fireThrowCallback(invoker, invocation, result.getException());
    } else {
        fireReturnCallback(invoker, invocation, result.getValue());
    }
}
 
Example 17
Source File: FutureFilter.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void syncCallback(final Invoker<?> invoker, final Invocation invocation, final Result result) {
    if (result.hasException()) {
        fireThrowCallback(invoker, invocation, result.getException());
    } else {
        fireReturnCallback(invoker, invocation, result.getValue());
    }
}
 
Example 18
Source File: CustomExceptionFilter.java    From BigDataPlatform with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException, ValidateException {
    Result result = invoker.invoke(invocation);
    if (!result.hasException()) {
        return result;
    }

    Throwable e = result.getException();
    if (e instanceof ValidateException) {
        throw new ValidateException(e.getMessage());
    } else {
        e.printStackTrace();
        throw new RpcException(wrapperExceptionMessage(e));
    }
}
 
Example 19
Source File: FutureFilter.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
private void syncCallback(final Invoker<?> invoker, final Invocation invocation, final Result result) {
    if (result.hasException()) {
        fireThrowCallback(invoker, invocation, result.getException());
    } else {
        fireReturnCallback(invoker, invocation, result.getValue());
    }
}
 
Example 20
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);
    }
}