Search in sources :

Example 31 with ChannelPipelineFactory

use of org.jboss.netty.channel.ChannelPipelineFactory 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)

Aggregations

ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)31 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)29 InetSocketAddress (java.net.InetSocketAddress)12 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)11 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)10 ClientBootstrap (org.jboss.netty.bootstrap.ClientBootstrap)7 LoggingHandler (org.jboss.netty.handler.logging.LoggingHandler)7 SimpleTestServerConnection (com.linkedin.databus2.test.container.SimpleTestServerConnection)6 HttpServerCodec (org.jboss.netty.handler.codec.http.HttpServerCodec)6 Log4JLoggerFactory (org.jboss.netty.logging.Log4JLoggerFactory)6 BeforeClass (org.testng.annotations.BeforeClass)6 InvalidConfigException (com.linkedin.databus.core.util.InvalidConfigException)5 SimpleObjectCaptureHandler (com.linkedin.databus2.test.container.SimpleObjectCaptureHandler)5 IOException (java.io.IOException)5 ChannelFactory (org.jboss.netty.channel.ChannelFactory)5 DatabusHttpClientImpl (com.linkedin.databus.client.DatabusHttpClientImpl)4 DbusEventV2Factory (com.linkedin.databus.core.DbusEventV2Factory)4 HttpRequestDecoder (org.jboss.netty.handler.codec.http.HttpRequestDecoder)4 HttpResponseEncoder (org.jboss.netty.handler.codec.http.HttpResponseEncoder)4 DbusEventBuffer (com.linkedin.databus.core.DbusEventBuffer)3