Search in sources :

Example 41 with ChannelInitializer

use of 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 42 with ChannelInitializer

use of 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 43 with ChannelInitializer

use of 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 44 with ChannelInitializer

use of 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)

Example 45 with ChannelInitializer

use of io.netty.channel.ChannelInitializer in project java by wavefrontHQ.

the class ProxyUtil method createInitializer.

/**
 * Create a {@link ChannelInitializer} with multiple dynamically created
 * {@link ChannelHandler} objects.
 *
 * @param channelHandlerSuppliers Suppliers of ChannelHandlers.
 * @param port                    port number.
 * @param idleTimeout             idle timeout in seconds.
 * @param sslContext              SSL context.
 * @return channel initializer
 */
static ChannelInitializer<SocketChannel> createInitializer(Iterable<Supplier<ChannelHandler>> channelHandlerSuppliers, int port, int idleTimeout, @Nullable SslContext sslContext) {
    String strPort = String.valueOf(port);
    ChannelHandler idleStateEventHandler = new IdleStateEventHandler(Metrics.newCounter(new TaggedMetricName("listeners", "connections.idle.closed", "port", strPort)));
    ChannelHandler connectionTracker = new ConnectionTrackingHandler(Metrics.newCounter(new TaggedMetricName("listeners", "connections.accepted", "port", strPort)), Metrics.newCounter(new TaggedMetricName("listeners", "connections.active", "port", strPort)));
    if (sslContext != null) {
        logger.info("TLS enabled on port: " + port);
    }
    return new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) {
            ChannelPipeline pipeline = ch.pipeline();
            if (sslContext != null) {
                pipeline.addLast(sslContext.newHandler(ch.alloc()));
            }
            pipeline.addFirst("idlehandler", new IdleStateHandler(idleTimeout, 0, 0));
            pipeline.addLast("idlestateeventhandler", idleStateEventHandler);
            pipeline.addLast("connectiontracker", connectionTracker);
            channelHandlerSuppliers.forEach(x -> pipeline.addLast(x.get()));
        }
    };
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) IdleStateEventHandler(com.wavefront.agent.channel.IdleStateEventHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ChannelHandler(io.netty.channel.ChannelHandler) ChannelInitializer(io.netty.channel.ChannelInitializer) TaggedMetricName(com.wavefront.common.TaggedMetricName) ChannelPipeline(io.netty.channel.ChannelPipeline) ConnectionTrackingHandler(com.wavefront.agent.channel.ConnectionTrackingHandler)

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