Search in sources :

Example 1 with MotanServiceException

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

the class DefaultProvider method invoke.

@Override
public Response invoke(Request request) {
    DefaultResponse response = new DefaultResponse();
    Method method = lookup(request);
    if (method == null) {
        MotanServiceException exception = new MotanServiceException("Service method not exist: " + request.getInterfaceName() + "." + request.getMethodName() + "(" + request.getParamtersDesc() + ")", MotanErrorMsgConstant.SERVICE_UNFOUND);
        response.setException(exception);
        return response;
    }
    try {
        Object value = method.invoke(proxyImpl, request.getArguments());
        response.setValue(value);
    } catch (Exception e) {
        if (e.getCause() != null) {
            LoggerUtil.error("Exception caught when method invoke: " + e.getCause());
            response.setException(new MotanBizException("provider call process error", e.getCause()));
        } else {
            response.setException(new MotanBizException("provider call process error", e));
        }
    } catch (Throwable t) {
        // 如果服务发生Error,将Error转化为Exception,防止拖垮调用方
        if (t.getCause() != null) {
            response.setException(new MotanServiceException("provider has encountered a fatal error!", t.getCause()));
        } else {
            response.setException(new MotanServiceException("provider has encountered a fatal error!", t));
        }
    }
    // 传递rpc版本和attachment信息方便不同rpc版本的codec使用。
    response.setRpcProtocolVersion(request.getRpcProtocolVersion());
    response.setAttachments(request.getAttachments());
    return response;
}
Also used : Method(java.lang.reflect.Method) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanBizException(com.weibo.api.motan.exception.MotanBizException)

Example 2 with MotanServiceException

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

the class ProviderMessageRouter method handle.

@Override
public Object handle(Channel channel, Object message) {
    if (channel == null || message == null) {
        throw new MotanFrameworkException("RequestRouter handler(channel, message) params is null");
    }
    if (!(message instanceof Request)) {
        throw new MotanFrameworkException("RequestRouter message type not support: " + message.getClass());
    }
    Request request = (Request) message;
    String serviceKey = MotanFrameworkUtil.getServiceKey(request);
    Provider<?> provider = providers.get(serviceKey);
    if (provider == null) {
        LoggerUtil.error(this.getClass().getSimpleName() + " handler Error: provider not exist serviceKey=" + serviceKey + " " + MotanFrameworkUtil.toString(request));
        MotanServiceException exception = new MotanServiceException(this.getClass().getSimpleName() + " handler Error: provider not exist serviceKey=" + serviceKey + " " + MotanFrameworkUtil.toString(request));
        DefaultResponse response = new DefaultResponse();
        response.setException(exception);
        return response;
    }
    return call(request, provider);
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Request(com.weibo.api.motan.rpc.Request) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 3 with MotanServiceException

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

the class RefererInvocationHandler method invoke.

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    if (isLocalMethod(method)) {
        if ("toString".equals(method.getName())) {
            return clustersToString();
        }
        throw new MotanServiceException("can not invoke local method:" + method.getName());
    }
    DefaultRequest request = new DefaultRequest();
    request.setRequestId(RequestIdGenerator.getRequestId());
    request.setArguments(args);
    String methodName = method.getName();
    boolean async = false;
    if (methodName.endsWith(MotanConstants.ASYNC_SUFFIX) && method.getReturnType().equals(ResponseFuture.class)) {
        methodName = MotanFrameworkUtil.removeAsyncSuffix(methodName);
        async = true;
    }
    RpcContext.getContext().putAttribute(MotanConstants.ASYNC_SUFFIX, async);
    request.setMethodName(methodName);
    request.setParamtersDesc(ReflectUtil.getMethodParamDesc(method));
    request.setInterfaceName(interfaceName);
    request.setAttachment(URLParamType.requestIdFromClient.getName(), String.valueOf(RequestIdGenerator.getRequestIdFromClient()));
    // 那么正常情况下只会使用A,如果A被开关降级,那么就会使用B,B也被降级,那么会使用C
    for (Cluster<T> cluster : clusters) {
        String protocolSwitcher = MotanConstants.PROTOCOL_SWITCHER_PREFIX + cluster.getUrl().getProtocol();
        Switcher switcher = switcherService.getSwitcher(protocolSwitcher);
        if (switcher != null && !switcher.isOn()) {
            continue;
        }
        request.setAttachment(URLParamType.version.getName(), cluster.getUrl().getVersion());
        request.setAttachment(URLParamType.clientGroup.getName(), cluster.getUrl().getGroup());
        // 带上client的application和module
        request.setAttachment(URLParamType.application.getName(), ApplicationInfo.getApplication(cluster.getUrl()).getApplication());
        request.setAttachment(URLParamType.module.getName(), ApplicationInfo.getApplication(cluster.getUrl()).getModule());
        Response response = null;
        boolean throwException = Boolean.parseBoolean(cluster.getUrl().getParameter(URLParamType.throwException.getName(), URLParamType.throwException.getValue()));
        try {
            response = cluster.call(request);
            if (async && response instanceof ResponseFuture) {
                return response;
            } else {
                return response.getValue();
            }
        } catch (RuntimeException e) {
            if (ExceptionUtil.isBizException(e)) {
                Throwable t = e.getCause();
                // 只抛出Exception,防止抛出远程的Error
                if (t != null && t instanceof Exception) {
                    throw t;
                } else {
                    String msg = t == null ? "biz exception cause is null" : ("biz exception cause is throwable error:" + t.getClass() + ", errmsg:" + t.getMessage());
                    throw new MotanServiceException(msg, MotanErrorMsgConstant.SERVICE_DEFAULT_ERROR);
                }
            } else if (!throwException) {
                LoggerUtil.warn("RefererInvocationHandler invoke false, so return default value: uri=" + cluster.getUrl().getUri() + " " + MotanFrameworkUtil.toString(request), e);
                return getDefaultReturnValue(method.getReturnType());
            } else {
                LoggerUtil.error("RefererInvocationHandler invoke Error: uri=" + cluster.getUrl().getUri() + " " + MotanFrameworkUtil.toString(request), e);
                throw e;
            }
        }
    }
    throw new MotanServiceException("Referer call Error: cluster not exist, interface=" + clz.getName() + " " + MotanFrameworkUtil.toString(request), MotanErrorMsgConstant.SERVICE_UNFOUND);
}
Also used : Response(com.weibo.api.motan.rpc.Response) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Switcher(com.weibo.api.motan.switcher.Switcher) ResponseFuture(com.weibo.api.motan.rpc.ResponseFuture) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 4 with MotanServiceException

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

the class FailoverHaStrategyTest method testCallWithFalse.

public void testCallWithFalse() {
    final DefaultRequest request = new DefaultRequest();
    request.setMethodName(IWorld.class.getMethods()[0].getName());
    request.setArguments(new Object[] {});
    request.setInterfaceName(IHello.class.getSimpleName());
    request.setParamtersDesc("void");
    final Response response = mockery.mock(Response.class);
    final URL url = URL.valueOf("motan%3A%2F%2F10.209.128.244%3A8000%2Fcom.weibo.api.motan.protocol.example.IWorld%3Fprotocol%3Dmotan%26export%3Dmotan%3A8000%26application%3Dapi%26module%3Dtest%26check%3Dtrue%26refreshTimestamp%3D1373275099717%26methodconfig.world%28void%29.retries%3D1%26id%3Dmotan%26methodconfig.world%28java.lang.String%29.retries%3D1%26methodconfig.world%28java.lang.String%2Cboolean%29.retries%3D1%26nodeType%3Dservice%26group%3Dwangzhe-test-yf%26shareChannel%3Dtrue%26&");
    mockery.checking(new Expectations() {

        {
            one(loadBalance).selectToHolder(request, failoverHaStrategy.referersHolder.get());
            for (Referer<IWorld> ref : referers) {
                atLeast(0).of(ref).isAvailable();
                will(returnValue(true));
                atLeast(0).of(ref).getUrl();
                will(returnValue(url));
                atLeast(0).of(ref).destroy();
            }
            atLeast(2).of(referers.get(0)).call(request);
            will(throwException(new MotanServiceException("mock throw exception when 1th call")));
            oneOf(referers.get(1)).call(request);
            will(throwException(new MotanServiceException("mock throw exception when 2th call")));
        }
    });
    try {
        failoverHaStrategy.call(request, loadBalance);
        fail("Should throw exception before!");
        // should not run to here
        Assert.assertTrue(false);
    } catch (Exception e) {
    }
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) IWorld(com.weibo.api.motan.protocol.example.IWorld) Referer(com.weibo.api.motan.rpc.Referer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) IHello(com.weibo.api.motan.protocol.example.IHello) URL(com.weibo.api.motan.rpc.URL) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 5 with MotanServiceException

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

the class ClusterSpiTest method testCall.

public void testCall() {
    initCluster(true);
    final Request request = mockRequest();
    final Response response = mockery.mock(Response.class);
    final URL url = mockURL();
    mockery.checking(new Expectations() {

        {
            for (Referer<IHello> ref : referers) {
                atLeast(0).of(ref).call(request);
                will(returnValue(response));
                atLeast(0).of(ref).isAvailable();
                will(returnValue(true));
                atLeast(0).of(ref).getUrl();
                will(returnValue(url));
                atLeast(1).of(ref).destroy();
            }
        }
    });
    clusterSpi.call(request);
    clusterSpi.destroy();
    try {
        clusterSpi.call(request);
        fail("Should not run to here!");
    } catch (Exception e) {
        assertTrue(e instanceof MotanServiceException);
    }
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Referer(com.weibo.api.motan.rpc.Referer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL) 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