Search in sources :

Example 86 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project jdepth by Crab2died.

the class EchoClient method connect.

private void connect(String host, int port) throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, Boolean.TRUE).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
                ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter)).addLast(new StringDecoder()).addLast(new EchoClientHandler());
            }
        });
        ChannelFuture future = bootstrap.connect(host, port).sync();
        future.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) Bootstrap(io.netty.bootstrap.Bootstrap) StringDecoder(io.netty.handler.codec.string.StringDecoder) ByteBuf(io.netty.buffer.ByteBuf) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 87 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project jdepth by Crab2died.

the class EchoClient method connect.

private void connect(String host, int port) throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, Boolean.TRUE).handler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(// 定长设置
                new FixedLengthFrameDecoder(15)).addLast(new StringDecoder()).addLast(new EchoClientHandler());
            }
        });
        ChannelFuture future = bootstrap.connect(host, port).sync();
        future.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) FixedLengthFrameDecoder(io.netty.handler.codec.FixedLengthFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 88 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project x-pipe by ctripcorp.

the class PsyncLatencyTest method startGetLatency.

private void startGetLatency() {
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    Bootstrap b = new Bootstrap();
    b.group(eventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            p.addLast(new LoggingHandler(LogLevel.DEBUG));
            p.addLast(new NettySimpleMessageHandler());
            p.addLast(new ReceiveMessageHandler(runId, offset));
        }
    });
    b.connect(dest);
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) NettySimpleMessageHandler(com.ctrip.xpipe.netty.NettySimpleMessageHandler) ReceiveMessageHandler(com.ctrip.xpipe.redis.keeper.simple.latency.netty.ReceiveMessageHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 89 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project motan by weibocom.

the class NettyClient method open.

@Override
public synchronized boolean open() {
    if (isAvailable()) {
        return true;
    }
    bootstrap = new Bootstrap();
    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.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    // 最大响应包限制
    final int maxContentLength = url.getIntParameter(URLParamType.maxContentLength.getName(), URLParamType.maxContentLength.getIntValue());
    bootstrap.group(nioEventLoopGroup).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("decoder", new NettyDecoder(codec, NettyClient.this, maxContentLength));
            pipeline.addLast("encoder", new NettyEncoder());
            pipeline.addLast("handler", new NettyChannelHandler(NettyClient.this, new MessageHandler() {

                @Override
                public Object handle(Channel channel, Object message) {
                    Response response = (Response) message;
                    ResponseFuture responseFuture = NettyClient.this.removeCallback(response.getRequestId());
                    if (responseFuture == null) {
                        LoggerUtil.warn("NettyClient has response from server, but responseFuture not exist, requestId={}", response.getRequestId());
                        return null;
                    }
                    if (response.getException() != null) {
                        responseFuture.onFailure(response);
                    } else {
                        responseFuture.onSuccess(response);
                    }
                    return null;
                }
            }));
        }
    });
    // 初始化连接池
    initPool();
    LoggerUtil.info("NettyClient finish Open: url={}", url);
    // 注册统计回调
    StatsUtil.registryStatisticCallback(this);
    // 设置可用状态
    state = ChannelState.ALIVE;
    return true;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanAbstractException(com.weibo.api.motan.exception.MotanAbstractException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ChannelPipeline(io.netty.channel.ChannelPipeline) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 90 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project cassandra by apache.

the class SimpleClient method establishConnection.

@VisibleForTesting
void establishConnection() throws IOException {
    // Configure the client.
    bootstrap = new Bootstrap().group(new NioEventLoopGroup(new NamedThreadFactory("SimpleClient-nioEventLoopGroup"))).channel(io.netty.channel.socket.nio.NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
    // Configure the pipeline factory.
    if (encryptionOptions.isEnabled()) {
        bootstrap.handler(new SecureInitializer(largeMessageThreshold));
    } else {
        bootstrap.handler(new Initializer(largeMessageThreshold));
    }
    ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
    // Wait until the connection attempt succeeds or fails.
    channel = future.awaitUninterruptibly().channel();
    if (!future.isSuccess()) {
        bootstrap.group().shutdownGracefully();
        throw new IOException("Connection Error", future.cause());
    }
}
Also used : NamedThreadFactory(org.apache.cassandra.concurrent.NamedThreadFactory) InetSocketAddress(java.net.InetSocketAddress) io.netty.channel(io.netty.channel) Bootstrap(io.netty.bootstrap.Bootstrap) IOException(java.io.IOException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)429 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)207 Channel (io.netty.channel.Channel)194 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)189 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)169 ChannelFuture (io.netty.channel.ChannelFuture)152 EventLoopGroup (io.netty.channel.EventLoopGroup)139 SocketChannel (io.netty.channel.socket.SocketChannel)123 InetSocketAddress (java.net.InetSocketAddress)119 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)113 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)108 Test (org.junit.jupiter.api.Test)89 ChannelPipeline (io.netty.channel.ChannelPipeline)76 LocalChannel (io.netty.channel.local.LocalChannel)74 LocalServerChannel (io.netty.channel.local.LocalServerChannel)71 LocalAddress (io.netty.channel.local.LocalAddress)66 CountDownLatch (java.util.concurrent.CountDownLatch)62 IOException (java.io.IOException)60 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)56 ChannelInitializer (io.netty.channel.ChannelInitializer)45