Search in sources :

Example 41 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project graylog2-server by Graylog2.

the class NettyTransport method getChannelInitializer.

protected ChannelInitializer<? extends Channel> getChannelInitializer(final LinkedHashMap<String, Callable<? extends ChannelHandler>> handlerList) {
    return new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) throws Exception {
            final ChannelPipeline p = ch.pipeline();
            Map.Entry<String, Callable<? extends ChannelHandler>> postentry = null;
            for (final Map.Entry<String, Callable<? extends ChannelHandler>> entry : handlerList.entrySet()) {
                // Handle exceptions at the top of the (bottom-up evaluated) pipeline
                if (entry.getKey().equals("exception-logger")) {
                    postentry = entry;
                } else {
                    p.addLast(entry.getKey(), entry.getValue().call());
                }
            }
            if (postentry != null) {
                p.addLast(postentry.getKey(), postentry.getValue().call());
            }
        }
    };
}
Also used : Channel(io.netty.channel.Channel) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelHandler(io.netty.channel.ChannelHandler) ExceptionLoggingChannelHandler(org.graylog2.inputs.transports.netty.ExceptionLoggingChannelHandler) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ChannelPipeline(io.netty.channel.ChannelPipeline) Callable(java.util.concurrent.Callable)

Example 42 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.

the class Client method run.

public void run() throws Exception {
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        // (2)
        Bootstrap b = new Bootstrap();
        b.group(workerGroup).channel(// (3)
        NioSocketChannel.class).handler(new // (4)
        ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new KryoDecoderHandler());
                ch.pipeline().addLast(new KryoEncoderHandler());
                ch.pipeline().addLast(handler);
            }
        }).option(ChannelOption.SO_KEEPALIVE, // (6)
        true);
        // Bind and start to accept incoming connections.
        // (7)
        ChannelFuture f = b.connect("localhost", port).sync();
        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to gracefully
        // shut down your server.
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 43 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project chuidiang-ejemplos by chuidiang.

the class Client method run.

public void run() throws Exception {
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        // (2)
        Bootstrap b = new Bootstrap();
        b.group(workerGroup).channel(// (3)
        NioSocketChannel.class).handler(new // (4)
        ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new FrameExtractor());
                ch.pipeline().addLast(new FrameMaker());
                ch.pipeline().addLast(new StringDecoder());
                ch.pipeline().addLast(new StringEncoder());
                ch.pipeline().addLast(clientHandler);
            }
        }).option(ChannelOption.SO_KEEPALIVE, // (6)
        true);
        // Bind and start to accept incoming connections.
        // (7)
        ChannelFuture f = b.connect("localhost", port).sync();
        // Wait until the server socket is closed.
        // In this example, this does not happen, but you can do that to gracefully
        // shut down your server.
        f.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 44 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project ambry by linkedin.

the class NettyServerTest method getNettyServer.

// helpers
// general
/**
 * Gets an instance of {@link NettyServer}.
 * @param properties the in-memory {@link Properties} to use.
 * @return an instance of {@link NettyServer}.
 * @throws InstantiationException
 * @throws IOException
 */
private NettyServer getNettyServer(Properties properties) throws InstantiationException, IOException {
    if (properties == null) {
        // dud properties. should pick up defaults
        properties = new Properties();
    }
    VerifiableProperties verifiableProperties = new VerifiableProperties(properties);
    final NettyConfig nettyConfig = new NettyConfig(verifiableProperties);
    final PerformanceConfig performanceConfig = new PerformanceConfig(verifiableProperties);
    Map<Integer, ChannelInitializer<SocketChannel>> channelInitializers = new HashMap<>();
    channelInitializers.put(nettyConfig.nettyServerPort, new FrontendNettyChannelInitializer(nettyConfig, performanceConfig, NETTY_METRICS, CONNECTION_STATS_HANDLER, REQUEST_HANDLER, PUBLIC_ACCESS_LOGGER, REST_SERVER_STATE, null, null));
    channelInitializers.put(nettyConfig.nettyServerSSLPort, new FrontendNettyChannelInitializer(nettyConfig, performanceConfig, NETTY_METRICS, CONNECTION_STATS_HANDLER, REQUEST_HANDLER, PUBLIC_ACCESS_LOGGER, REST_SERVER_STATE, SSL_FACTORY, null));
    return new NettyServer(nettyConfig, NETTY_METRICS, channelInitializers);
}
Also used : VerifiableProperties(com.github.ambry.config.VerifiableProperties) HashMap(java.util.HashMap) PerformanceConfig(com.github.ambry.config.PerformanceConfig) ChannelInitializer(io.netty.channel.ChannelInitializer) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) NettyConfig(com.github.ambry.config.NettyConfig)

Example 45 with ChannelInitializer

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelInitializer in project weicoder by wdcode.

the class WebSocketServer method handler.

@Override
protected ChannelHandler handler() {
    return new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(final SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new HttpServerCodec());
            ch.pipeline().addLast(new HttpObjectAggregator(1024 * 1024));
            ch.pipeline().addLast(new WebSocketHandler("websocket"));
            ch.config().setAllocator(PooledByteBufAllocator.DEFAULT);
        }
    };
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) WebSocketHandler(com.weicoder.netty.handler.WebSocketHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelInitializer(io.netty.channel.ChannelInitializer)

Aggregations

ChannelInitializer (io.netty.channel.ChannelInitializer)85 Channel (io.netty.channel.Channel)58 Bootstrap (io.netty.bootstrap.Bootstrap)42 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)39 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)36 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)32 InetSocketAddress (java.net.InetSocketAddress)32 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)31 ChannelFuture (io.netty.channel.ChannelFuture)29 ChannelPipeline (io.netty.channel.ChannelPipeline)26 EventLoopGroup (io.netty.channel.EventLoopGroup)26 LocalServerChannel (io.netty.channel.local.LocalServerChannel)21 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)21 LocalChannel (io.netty.channel.local.LocalChannel)20 SocketChannel (io.netty.channel.socket.SocketChannel)18 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)17 SslHandler (io.netty.handler.ssl.SslHandler)17 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)14 CountDownLatch (java.util.concurrent.CountDownLatch)12 ChannelHandler (io.netty.channel.ChannelHandler)11