Search in sources :

Example 21 with MotanFrameworkException

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

the class NettyClient method initClientBootstrap.

/**
	 * 初始化 netty clientBootstrap
	 */
private void initClientBootstrap() {
    bootstrap = new ClientBootstrap(channelFactory);
    bootstrap.setOption("keepAlive", true);
    bootstrap.setOption("tcpNoDelay", true);
    // 实际上,极端情况下,connectTimeout会达到500ms,因为netty nio的实现中,是依赖BossThread来控制超时,
    // 如果为了严格意义的timeout,那么需要应用端进行控制。
    int timeout = getUrl().getIntParameter(URLParamType.connectTimeout.getName(), URLParamType.connectTimeout.getIntValue());
    if (timeout <= 0) {
        throw new MotanFrameworkException("NettyClient init Error: timeout(" + timeout + ") <= 0 is forbid.", MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
    }
    bootstrap.setOption("connectTimeoutMillis", timeout);
    // 最大响应包限制
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() {
            ChannelPipeline pipeline = Channels.pipeline();
            pipeline.addLast("decoder", new NettyDecoder(codec, NettyClient.this, maxContentLength));
            pipeline.addLast("encoder", new NettyEncoder(codec, NettyClient.this));
            pipeline.addLast("handler", new NettyChannelHandler(NettyClient.this, new MessageHandler() {

                @Override
                public Object handle(Channel channel, Object message) {
                    Response response = (Response) message;
                    NettyResponseFuture responseFuture = NettyClient.this.removeCallback(response.getRequestId());
                    if (responseFuture == null) {
                        LoggerUtil.warn("NettyClient has response from server, but resonseFuture not exist,  requestId={}", response.getRequestId());
                        return null;
                    }
                    if (response.getException() != null) {
                        responseFuture.onFailure(response);
                    } else {
                        responseFuture.onSuccess(response);
                    }
                    return null;
                }
            }));
            return pipeline;
        }
    });
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MessageHandler(com.weibo.api.motan.transport.MessageHandler) Channel(com.weibo.api.motan.transport.Channel) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory)

Example 22 with MotanFrameworkException

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

the class NettyDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception {
    if (buffer.readableBytes() <= MotanConstants.NETTY_HEADER) {
        return null;
    }
    buffer.markReaderIndex();
    short type = buffer.readShort();
    if (type != MotanConstants.NETTY_MAGIC_TYPE) {
        buffer.resetReaderIndex();
        throw new MotanFrameworkException("NettyDecoder transport header not support, type: " + type);
    }
    byte messageType = (byte) buffer.readShort();
    long requestId = buffer.readLong();
    int dataLength = buffer.readInt();
    // FIXME 如果dataLength过大,可能导致问题
    if (buffer.readableBytes() < dataLength) {
        buffer.resetReaderIndex();
        return null;
    }
    if (maxContentLength > 0 && dataLength > maxContentLength) {
        LoggerUtil.warn("NettyDecoder transport data content length over of limit, size: {}  > {}. remote={} local={}", dataLength, maxContentLength, ctx.getChannel().getRemoteAddress(), ctx.getChannel().getLocalAddress());
        Exception e = new MotanServiceException("NettyDecoder transport data content length over of limit, size: " + dataLength + " > " + maxContentLength);
        if (messageType == MotanConstants.FLAG_REQUEST) {
            Response response = buildExceptionResponse(requestId, e);
            channel.write(response);
            throw e;
        } else {
            throw e;
        }
    }
    byte[] data = new byte[dataLength];
    buffer.readBytes(data);
    try {
        String remoteIp = getRemoteIp(channel);
        return codec.decode(client, remoteIp, data);
    } catch (Exception e) {
        if (messageType == MotanConstants.FLAG_REQUEST) {
            Response resonse = buildExceptionResponse(requestId, e);
            channel.write(resonse);
            return null;
        } else {
            Response resonse = buildExceptionResponse(requestId, e);
            return resonse;
        }
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) Response(com.weibo.api.motan.rpc.Response) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 23 with MotanFrameworkException

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

the class ConsulClientWrapper method init.

@PostConstruct
void init() {
    try {
        String[] arr = registryUrl.split(":");
        String host = arr[0];
        int port = Integer.parseInt(arr[1]);
        consulClient = new ConsulClient(host, port);
    } catch (Exception e) {
        throw new MotanFrameworkException("Fail to connect consul, cause: " + e.getMessage());
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ConsulClient(com.ecwid.consul.v1.ConsulClient) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) PostConstruct(javax.annotation.PostConstruct)

Example 24 with MotanFrameworkException

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

the class ZookeeperRegistry method doUnregister.

@Override
protected void doUnregister(URL url) {
    try {
        serverLock.lock();
        removeNode(url, ZkNodeType.AVAILABLE_SERVER);
        removeNode(url, ZkNodeType.UNAVAILABLE_SERVER);
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to unregister %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        serverLock.unlock();
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Example 25 with MotanFrameworkException

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

the class ZookeeperRegistry method unsubscribeService.

@Override
protected void unsubscribeService(URL url, ServiceListener serviceListener) {
    try {
        clientLock.lock();
        Map<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners != null) {
            IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
            if (zkChildListener != null) {
                zkClient.unsubscribeChildChanges(ZkUtils.toNodeTypePath(url, ZkNodeType.CLIENT), zkChildListener);
                childChangeListeners.remove(serviceListener);
            }
        }
    } catch (Throwable e) {
        throw new MotanFrameworkException(String.format("Failed to unsubscribe service %s to zookeeper(%s), cause: %s", url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : ServiceListener(com.weibo.api.motan.registry.support.command.ServiceListener) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener)

Aggregations

MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)41 URL (com.weibo.api.motan.rpc.URL)7 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)6 ArrayList (java.util.ArrayList)6 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)5 IOException (java.io.IOException)5 Method (java.lang.reflect.Method)4 Registry (com.weibo.api.motan.registry.Registry)3 Response (com.weibo.api.motan.rpc.Response)3 HeartbeatFactory (com.weibo.api.motan.transport.HeartbeatFactory)3 ObjectInput (java.io.ObjectInput)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Serialization (com.weibo.api.motan.codec.Serialization)2 ConfigHandler (com.weibo.api.motan.config.handler.ConfigHandler)2 CommandListener (com.weibo.api.motan.registry.support.command.CommandListener)2 ServiceListener (com.weibo.api.motan.registry.support.command.ServiceListener)2 TransportException (com.weibo.api.motan.transport.TransportException)2 Map (java.util.Map)2 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)2