Search in sources :

Example 6 with MotanServiceException

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

the class RefererInvocationHandlerTest method testInvokeException.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInvokeException() throws Throwable {
    final Cluster cluster = mockery.mock(Cluster.class);
    final URL u = new URL("motan", "local", 80, "test");
    u.addParameter(URLParamType.nodeType.getName(), MotanConstants.NODE_TYPE_REFERER);
    mockery.checking(new Expectations() {

        {
            one(cluster).call(with(any(Request.class)));
            will(throwException(new MotanBizException("just test", new StackOverflowError())));
            allowing(cluster).getUrl();
            will(returnValue(u));
        }
    });
    List<Cluster> clus = new ArrayList<Cluster>();
    clus.add(cluster);
    RefererInvocationHandler handler = new RefererInvocationHandler(String.class, clus);
    Method[] methods = String.class.getMethods();
    try {
        handler.invoke(null, methods[1], null);
    } catch (Exception e) {
        assertTrue(e instanceof MotanServiceException);
        assertTrue(e.getMessage().contains("StackOverflowError"));
    }
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) ArrayList(java.util.ArrayList) Cluster(com.weibo.api.motan.cluster.Cluster) Method(java.lang.reflect.Method) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanBizException(com.weibo.api.motan.exception.MotanBizException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) Test(org.junit.Test)

Example 7 with MotanServiceException

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

the class AbstractInterfaceConfig method getLocalHostAddress.

protected String getLocalHostAddress(List<URL> registryURLs) {
    String localAddress = null;
    Map<String, Integer> regHostPorts = new HashMap<String, Integer>();
    for (URL ru : registryURLs) {
        if (StringUtils.isNotBlank(ru.getHost()) && ru.getPort() > 0) {
            regHostPorts.put(ru.getHost(), ru.getPort());
        }
    }
    InetAddress address = NetUtils.getLocalAddress(regHostPorts);
    if (address != null) {
        localAddress = address.getHostAddress();
    }
    if (NetUtils.isValidLocalHost(localAddress)) {
        return localAddress;
    }
    throw new MotanServiceException("Please config local server hostname with intranet IP first!", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
Also used : InetAddress(java.net.InetAddress) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) URL(com.weibo.api.motan.rpc.URL)

Example 8 with MotanServiceException

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

the class ConfigUtil method parseExport.

/**
     * export fomart: protocol1:port1,protocol2:port2
     * 
     * @param export
     * @return
     */
@SuppressWarnings("unchecked")
public static Map<String, Integer> parseExport(String export) {
    if (StringUtils.isBlank(export)) {
        return Collections.emptyMap();
    }
    Map<String, Integer> pps = new HashMap<String, Integer>();
    String[] protocolAndPorts = MotanConstants.COMMA_SPLIT_PATTERN.split(export);
    for (String pp : protocolAndPorts) {
        if (StringUtils.isBlank(pp)) {
            continue;
        }
        String[] ppDetail = pp.split(":");
        if (ppDetail.length == 2) {
            pps.put(ppDetail[0], Integer.parseInt(ppDetail[1]));
        } else if (ppDetail.length == 1) {
            if (MotanConstants.PROTOCOL_INJVM.equals(ppDetail[0])) {
                pps.put(ppDetail[0], MotanConstants.DEFAULT_INT_VALUE);
            } else {
                int port = MathUtil.parseInt(ppDetail[0], 0);
                if (port <= 0) {
                    throw new MotanServiceException("Export is malformed :" + export);
                } else {
                    pps.put(MotanConstants.PROTOCOL_MOTAN, port);
                }
            }
        } else {
            throw new MotanServiceException("Export is malformed :" + export);
        }
    }
    return pps;
}
Also used : HashMap(java.util.HashMap) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 9 with MotanServiceException

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

the class ActiveLimitFilter method filter.

@Override
public Response filter(Caller<?> caller, Request request) {
    int maxAcvitivyCount = caller.getUrl().getIntParameter(URLParamType.actives.getName(), URLParamType.actives.getIntValue());
    if (maxAcvitivyCount > 0) {
        int activeCount = RpcStats.getServiceStat(caller.getUrl()).getActiveCount();
        if (activeCount >= maxAcvitivyCount) {
            throw new MotanServiceException(String.format("Request(%s) active count exceed the limit (%s), referer:%s", request, maxAcvitivyCount, caller.getUrl()), MotanErrorMsgConstant.SERVICE_REJECT);
        }
    }
    long startTime = System.currentTimeMillis();
    RpcStats.beforeCall(caller.getUrl(), request);
    try {
        Response rs = caller.call(request);
        RpcStats.afterCall(caller.getUrl(), request, true, System.currentTimeMillis() - startTime);
        return rs;
    } catch (RuntimeException re) {
        RpcStats.afterCall(caller.getUrl(), request, false, System.currentTimeMillis() - startTime);
        throw re;
    }
}
Also used : Response(com.weibo.api.motan.rpc.Response) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException)

Example 10 with MotanServiceException

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

the class NettyChannel method request.

@Override
public Response request(Request request) throws TransportException {
    int timeout = nettyClient.getUrl().getMethodParameter(request.getMethodName(), request.getParamtersDesc(), URLParamType.requestTimeout.getName(), URLParamType.requestTimeout.getIntValue());
    if (timeout <= 0) {
        throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
    NettyResponseFuture response = new NettyResponseFuture(request, timeout, this.nettyClient);
    this.nettyClient.registerCallback(request.getRequestId(), response);
    ChannelFuture writeFuture = this.channel.write(request);
    boolean result = writeFuture.awaitUninterruptibly(timeout, TimeUnit.MILLISECONDS);
    if (result && writeFuture.isSuccess()) {
        response.addListener(new FutureListener() {

            @Override
            public void operationComplete(Future future) throws Exception {
                if (future.isSuccess() || (future.isDone() && ExceptionUtil.isBizException(future.getException()))) {
                    // 成功的调用 
                    nettyClient.resetErrorCount();
                } else {
                    // 失败的调用 
                    nettyClient.incrErrorCount();
                }
            }
        });
        return response;
    }
    writeFuture.cancel();
    response = this.nettyClient.removeCallback(request.getRequestId());
    if (response != null) {
        response.cancel();
    }
    // 失败的调用 
    nettyClient.incrErrorCount();
    if (writeFuture.getCause() != null) {
        throw new MotanServiceException("NettyChannel send request to server Error: url=" + nettyClient.getUrl().getUri() + " local=" + localAddress + " " + MotanFrameworkUtil.toString(request), writeFuture.getCause());
    } else {
        throw new MotanServiceException("NettyChannel send request to server Timeout: url=" + nettyClient.getUrl().getUri() + " local=" + localAddress + " " + MotanFrameworkUtil.toString(request));
    }
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) FutureListener(com.weibo.api.motan.rpc.FutureListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) Future(com.weibo.api.motan.rpc.Future) ChannelFuture(org.jboss.netty.channel.ChannelFuture) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) TransportException(com.weibo.api.motan.transport.TransportException)

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