Search in sources :

Example 46 with ChannelPipeline

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline in project alien4cloud by alien4cloud.

the class StompConnection method init.

@SneakyThrows({ InterruptedException.class, URISyntaxException.class })
private void init() {
    if (this.stompChannel != null) {
        throw new IllegalStateException("The stomp connection has already been started");
    }
    String wsUrl = "ws://" + host + ":" + port + endPoint + "/websocket";
    if (log.isDebugEnabled()) {
        log.debug("Web socket url {}", wsUrl);
    }
    String loginUrl = null;
    if (user != null && password != null && loginPath != null) {
        loginUrl = "http://" + host + ":" + port + loginPath;
        if (log.isDebugEnabled()) {
            log.debug("Authentication url {}", loginUrl);
        }
    }
    this.eventLoopGroup = new NioEventLoopGroup();
    this.stompClientHandler = new StompClientHandler();
    DefaultHttpHeaders handshakeHeaders = new DefaultHttpHeaders();
    if (this.headers != null) {
        for (Map.Entry<String, String> header : this.headers.entrySet()) {
            handshakeHeaders.add(header.getKey(), header.getValue());
        }
    }
    final WebSocketClientHandler webSocketHandler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(new URI(wsUrl), WebSocketVersion.V13, null, false, handshakeHeaders), host, user, password, loginUrl);
    Bootstrap b = new Bootstrap();
    b.group(eventLoopGroup).channel(NioSocketChannel.class);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(HttpClientCodec.class.getName(), new HttpClientCodec());
            pipeline.addLast(HttpObjectAggregator.class.getName(), new HttpObjectAggregator(8192));
            pipeline.addLast(WebSocketClientCompressionHandler.class.getName(), new WebSocketClientCompressionHandler());
            pipeline.addLast(WebSocketClientHandler.class.getName(), webSocketHandler);
            pipeline.addLast(StompSubframeDecoder.class.getName(), new StompSubframeDecoder());
            pipeline.addLast(StompSubframeEncoder.class.getName(), new StompSubframeEncoder());
            pipeline.addLast(StompSubframeAggregator.class.getName(), new StompSubframeAggregator(1048576));
            pipeline.addLast(StompClientHandler.class.getName(), stompClientHandler);
        }
    });
    this.stompChannel = b.connect(host, port).sync().channel();
    this.stompClientHandler.connectFuture(this.stompChannel.newPromise());
    webSocketHandler.handshakeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            stompClientHandler.beginStomp(stompChannel);
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) WebSocketClientCompressionHandler(io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler) StompSubframeAggregator(io.netty.handler.codec.stomp.StompSubframeAggregator) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) StompSubframeEncoder(io.netty.handler.codec.stomp.StompSubframeEncoder) URI(java.net.URI) ChannelFutureListener(io.netty.channel.ChannelFutureListener) URISyntaxException(java.net.URISyntaxException) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) StompSubframeDecoder(io.netty.handler.codec.stomp.StompSubframeDecoder) Bootstrap(io.netty.bootstrap.Bootstrap) Map(java.util.Map) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SneakyThrows(lombok.SneakyThrows)

Example 47 with ChannelPipeline

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline in project duangframework by tcrct.

the class RpcChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    // 将 RPC 请求进行解码(为了处理请求)
    p.addLast(new NettyDecoder());
    // // 将 RPC 响应进行编码(为了返回响应)
    p.addLast(new NettyEncoder());
    // 目的是支持异步大文件传输
    p.addLast(new ChunkedWriteHandler());
    p.addLast(new IdleStateHandler(60, 0, 0));
    // 真正处理RPC业务逻辑的地方
    p.addLast(new NettyServiceHandler());
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) NettyEncoder(com.duangframework.rpc.common.NettyEncoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) NettyDecoder(com.duangframework.rpc.common.NettyDecoder) NettyServiceHandler(com.duangframework.rpc.handler.NettyServiceHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 48 with ChannelPipeline

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline in project duangframework by tcrct.

the class ClientChannelInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline p = ch.pipeline();
    // 将 RPC 请求进行解码(为了处理请求)
    p.addLast(new NettyDecoder());
    // // 将 RPC 响应进行编码(为了返回响应)
    p.addLast(new NettyEncoder());
    // 目的是支持异步大文件传输
    p.addLast(new ChunkedWriteHandler());
    p.addLast(new IdleStateHandler(60, 0, 0));
    // 真正处理RPC业务逻辑的地方
    p.addLast(new NettyClientHandler());
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) NettyEncoder(com.duangframework.rpc.common.NettyEncoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) NettyDecoder(com.duangframework.rpc.common.NettyDecoder) NettyClientHandler(com.duangframework.rpc.handler.NettyClientHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 49 with ChannelPipeline

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

the class NettyKeyedPoolClientFactory method doStart.

@Override
protected void doStart() throws Exception {
    eventLoopGroup = new NioEventLoopGroup(eventLoopThreads, XpipeThreadFactory.create("NettyKeyedPoolClientFactory"));
    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());
            p.addLast(new NettySimpleMessageHandler());
            p.addLast(new NettyClientHandler());
        }
    });
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) LoggingHandler(io.netty.handler.logging.LoggingHandler) NettySimpleMessageHandler(com.ctrip.xpipe.netty.NettySimpleMessageHandler) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 50 with ChannelPipeline

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelPipeline 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)

Aggregations

ChannelPipeline (io.netty.channel.ChannelPipeline)370 SocketChannel (io.netty.channel.socket.SocketChannel)107 Channel (io.netty.channel.Channel)90 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)90 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)80 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)78 Bootstrap (io.netty.bootstrap.Bootstrap)77 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)69 ChannelFuture (io.netty.channel.ChannelFuture)68 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)62 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)62 EventLoopGroup (io.netty.channel.EventLoopGroup)54 SslHandler (io.netty.handler.ssl.SslHandler)52 InetSocketAddress (java.net.InetSocketAddress)49 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)43 LoggingHandler (io.netty.handler.logging.LoggingHandler)37 IOException (java.io.IOException)37 StringDecoder (io.netty.handler.codec.string.StringDecoder)36 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)36 StringEncoder (io.netty.handler.codec.string.StringEncoder)33