Search in sources :

Example 16 with MotanServiceException

use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.

the class SwitcherFilter method mockDefaultResponse.

/**
     * 返回的reponse需要设置exception,这样invocationhandler会在throwException为false时,构建默认值返回
     * 
     * @param request
     * @return
     */
private Response mockDefaultResponse(Request request) {
    DefaultResponse response = new DefaultResponse(null, request.getRequestId());
    response.setException(new MotanServiceException("Request false for switcher is on"));
    return response;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 17 with MotanServiceException

use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.

the class ProviderProtectedMessageRouter method reject.

private Response reject(String method, int requestCounter, int totalCounter, int maxThread) {
    DefaultResponse response = new DefaultResponse();
    MotanServiceException exception = new MotanServiceException("ThreadProtectedRequestRouter reject request: request_counter=" + requestCounter + " total_counter=" + totalCounter + " max_thread=" + maxThread, MotanErrorMsgConstant.SERVICE_REJECT);
    exception.setStackTrace(new StackTraceElement[0]);
    response.setException(exception);
    LoggerUtil.error("ThreadProtectedRequestRouter reject request: request_method=" + method + " request_counter=" + requestCounter + " =" + totalCounter + " max_thread=" + maxThread);
    return response;
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 18 with MotanServiceException

use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.

the class FailoverHaStrategy method call.

@Override
public Response call(Request request, LoadBalance<T> loadBalance) {
    List<Referer<T>> referers = selectReferers(request, loadBalance);
    if (referers.isEmpty()) {
        throw new MotanServiceException(String.format("FailoverHaStrategy No referers for request:%s, loadbalance:%s", request, loadBalance));
    }
    URL refUrl = referers.get(0).getUrl();
    // 先使用method的配置
    int tryCount = refUrl.getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.retries.getName(), URLParamType.retries.getIntValue());
    // 如果有问题,则设置为不重试
    if (tryCount < 0) {
        tryCount = 0;
    }
    for (int i = 0; i <= tryCount; i++) {
        Referer<T> refer = referers.get(i % referers.size());
        try {
            request.setRetries(i);
            return refer.call(request);
        } catch (RuntimeException e) {
            // 对于业务异常,直接抛出
            if (ExceptionUtil.isBizException(e)) {
                throw e;
            } else if (i >= tryCount) {
                throw e;
            }
            LoggerUtil.warn(String.format("FailoverHaStrategy Call false for request:%s error=%s", request, e.getMessage()));
        }
    }
    throw new MotanFrameworkException("FailoverHaStrategy.call should not come here!");
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Referer(com.weibo.api.motan.rpc.Referer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL)

Example 19 with MotanServiceException

use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.

the class YarMessageRouter method handle.

@Override
public Object handle(Channel channel, Object message) {
    YarRequest yarRequest = (YarRequest) message;
    String packagerName = yarRequest.getPackagerName();
    Provider<?> provider = providerMap.get(yarRequest.getRequestPath());
    if (provider == null) {
        throw new MotanServiceException("can not find service provider. request path:" + yarRequest.getRequestPath());
    }
    Class<?> clazz = provider.getInterface();
    Request request = YarProtocolUtil.convert(yarRequest, clazz);
    Response response = call(request, provider);
    YarResponse yarResponse = YarProtocolUtil.convert(response, packagerName);
    return yarResponse;
}
Also used : Response(com.weibo.api.motan.rpc.Response) YarResponse(com.weibo.yar.YarResponse) YarRequest(com.weibo.yar.YarRequest) YarRequest(com.weibo.yar.YarRequest) Request(com.weibo.api.motan.rpc.Request) YarResponse(com.weibo.yar.YarResponse) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 20 with MotanServiceException

use of com.weibo.api.motan.exception.MotanServiceException in project motan by weibocom.

the class YarProtocolUtil method addArguments.

/**
     * add arguments
     * 
     * @param interfaceClass
     * @param methodName
     * @param arguments
     * @return
     */
private static void addArguments(DefaultRequest request, Class<?> interfaceClass, String methodName, Object[] arguments) {
    Method targetMethod = null;
    Method[] methods = interfaceClass.getDeclaredMethods();
    for (Method m : methods) {
        // size of parameters with same method name to avoid.
        if (m.getName().equalsIgnoreCase(methodName) && m.getParameterTypes().length == arguments.length) {
            targetMethod = m;
            break;
        }
    }
    if (targetMethod == null) {
        throw new MotanServiceException("cann't find request method. method name " + methodName);
    }
    request.setParamtersDesc(ReflectUtil.getMethodParamDesc(targetMethod));
    if (arguments != null && arguments.length > 0) {
        Class<?>[] argumentClazz = targetMethod.getParameterTypes();
        request.setArguments(adaptParams(targetMethod, arguments, argumentClazz));
    }
}
Also used : Method(java.lang.reflect.Method) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Aggregations

MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)24 Response (com.weibo.api.motan.rpc.Response)11 URL (com.weibo.api.motan.rpc.URL)8 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)7 Request (com.weibo.api.motan.rpc.Request)7 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)6 Expectations (org.jmock.Expectations)6 IHello (com.weibo.api.motan.protocol.example.IHello)4 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)4 Referer (com.weibo.api.motan.rpc.Referer)4 MotanBizException (com.weibo.api.motan.exception.MotanBizException)3 TransportException (com.weibo.api.motan.transport.TransportException)3 Method (java.lang.reflect.Method)3 HashMap (java.util.HashMap)3 IWorld (com.weibo.api.motan.protocol.example.IWorld)2 RegistryService (com.weibo.api.motan.registry.RegistryService)2 ChannelFuture (org.jboss.netty.channel.ChannelFuture)2 Test (org.junit.Test)2 Cluster (com.weibo.api.motan.cluster.Cluster)1 MotanAbstractException (com.weibo.api.motan.exception.MotanAbstractException)1