Example usage for io.netty.channel ChannelInitializer ChannelInitializer

List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer

Introduction

In this page you can find the example usage for io.netty.channel ChannelInitializer ChannelInitializer.

Prototype

ChannelInitializer

Source Link

Usage

From source file:com.spotify.ffwd.riemann.RiemannTCPProtocolClient.java

License:Apache License

@Override
public ChannelInitializer<Channel> initializer() {
    return new ChannelInitializer<Channel>() {
        @Override//from w w w  . j  ava  2  s.c o  m
        protected void initChannel(Channel ch) throws Exception {
            final LengthFieldBasedFrameDecoder lengthPrefix = new LengthFieldBasedFrameDecoder(MAX_LENGTH, 0,
                    4);
            ch.pipeline().addLast(lengthPrefix, receiver, sender);
        }
    };
}

From source file:com.spotify.ffwd.riemann.RiemannTCPProtocolServer.java

License:Apache License

@Override
public final ChannelInitializer<Channel> initializer() {
    return new ChannelInitializer<Channel>() {
        @Override/*from  w  w  w  .  j a va  2 s.  c  om*/
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(frameDecoder.get(), decoder, responder, unpacker, handler);
        }
    };
}

From source file:com.spotify.ffwd.riemann.RiemannUDPProtocolServer.java

License:Apache License

@Override
public final ChannelInitializer<Channel> initializer() {
    return new ChannelInitializer<Channel>() {
        @Override// w  w  w  .  jav a  2s  .  c  o m
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(datagramDecoder, messageDecoder, unpacker, handler);
        }
    };
}

From source file:com.spotify.ffwd.template.TemplateOutputProtocolClient.java

License:Apache License

@Override
public ChannelInitializer<Channel> initializer() {
    return new ChannelInitializer<Channel>() {
        @Override//from  ww w  . ja va  2s  . c om
        protected void initChannel(final Channel ch) throws Exception {
            ch.pipeline().addLast(stringEncoder, outputEncoder);
        }
    };
}

From source file:com.spotify.folsom.client.DefaultRawMemcacheClient.java

License:Apache License

public static ListenableFuture<RawMemcacheClient> connect(final HostAndPort address,
        final int outstandingRequestLimit, final boolean binary, final Executor executor,
        final long timeoutMillis) {

    final ChannelInboundHandler decoder;
    if (binary) {
        decoder = new BinaryMemcacheDecoder();
    } else {//  w  ww.j ava 2  s  .  c  om
        decoder = new AsciiMemcacheDecoder();
    }

    final ChannelHandler initializer = new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ch.pipeline().addLast(new TcpTuningHandler(), decoder,

                    // Downstream
                    new MemcacheEncoder());
        }
    };

    final SettableFuture<RawMemcacheClient> clientFuture = SettableFuture.create();

    final Bootstrap bootstrap = new Bootstrap().group(EVENT_LOOP_GROUP).handler(initializer)
            .channel(NioSocketChannel.class)
            .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, SimpleSizeEstimator.INSTANCE);

    final ChannelFuture connectFuture = bootstrap
            .connect(new InetSocketAddress(address.getHostText(), address.getPort()));

    connectFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                // Create client
                final RawMemcacheClient client = new DefaultRawMemcacheClient(address, future.channel(),
                        outstandingRequestLimit, executor, timeoutMillis);
                clientFuture.set(client);
            } else {
                clientFuture.setException(future.cause());
            }
        }
    });

    return onExecutor(clientFuture, executor);
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMQIntegrationTest.java

License:Apache License

@Before
public void setup() throws InterruptedException {
    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();
    serverBootstrap = new ServerBootstrap();

    serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {

                @Override/* ww  w  .  ja va  2 s.  com*/
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ZMTP20Codec(new ZMTPSession(ZMTPConnectionType.Addressed, 1024,
                            identity.getBytes(), ZMTPSocketType.REQ), false));
                    ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelActive(final ChannelHandlerContext ctx) throws Exception {
                            super.channelActive(ctx);
                            channelsConnected.add(ctx.channel());
                        }

                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            incomingMessages.put((ZMTPIncomingMessage) msg);
                        }
                    });
                }
            });

    serverChannel = serverBootstrap.bind(0).sync().channel();
    serverAddress = (InetSocketAddress) serverChannel.localAddress();
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPTestConnector.java

License:Apache License

public boolean connectAndReceive(final String ip, final int port, final int serverType) {
    context = ZMQ.context(1);//w  w  w. j av  a 2 s.  c  om
    serverSocket = context.socket(serverType);

    preConnect(serverSocket);

    serverSocket.bind("tcp://" + ip + ":" + port);

    EventLoopGroup group = new NioEventLoopGroup();
    // Configure the client.
    final Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    final ZMTPSession session = new ZMTPSession(ZMTPConnectionType.Addressed,
                            "client".getBytes());
                    ChannelPipeline pl = ch.pipeline();
                    pl.addLast(new ZMTP10Codec(session));
                    pl.addLast(new ChannelInboundHandlerAdapter() {
                        @Override
                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                            if (onMessage((ZMTPIncomingMessage) msg)) {
                                receivedMessage = true;
                                ctx.channel().close();
                            }
                        }
                    });
                }
            });

    // Start the connection attempt.
    final ChannelFuture future = bootstrap.connect(new InetSocketAddress(ip, port));

    future.awaitUninterruptibly();

    afterConnect(serverSocket, future);

    // Wait until the connection is closed or the connection attempt fails.
    future.channel().closeFuture().awaitUninterruptibly();

    // Shut down thread pools to exit.
    group.shutdownGracefully();

    serverSocket.close();
    context.term();

    return receivedMessage;
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java

License:Apache License

public static void main(final String... args) throws InterruptedException {
    final ProgressMeter meter = new ProgressMeter("requests", true);

    // Codecs//  www.ja  v a2s .  co m
    final ZMTPCodec serverCodec = ZMTPCodec.builder().socketType(ROUTER).encoder(ReplyEncoder.class)
            .decoder(RequestDecoder.class).build();

    final ZMTPCodec clientCodec = ZMTPCodec.builder().socketType(DEALER).encoder(RequestEncoder.class)
            .decoder(ReplyDecoder.class).build();

    // Server
    final Executor serverExecutor = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
            UNCAUGHT_EXCEPTION_HANDLER, true);
    final ServerBootstrap serverBootstrap = new ServerBootstrap()
            .group(new NioEventLoopGroup(1), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE)
            .childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(serverCodec);
                    ch.pipeline().addLast(new ServerRequestTracker());
                    ch.pipeline().addLast(new ServerHandler(serverExecutor));
                }
            });
    final Channel server = serverBootstrap.bind(ANY_PORT).awaitUninterruptibly().channel();

    // Client
    final Executor clientExecutor = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
            UNCAUGHT_EXCEPTION_HANDLER, true);
    final SocketAddress address = server.localAddress();
    final Bootstrap clientBootstrap = new Bootstrap().group(new NioEventLoopGroup())
            .channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE)
            .handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(clientCodec);
                    ch.pipeline().addLast(new ClientRequestTracker());
                    ch.pipeline().addLast(new ClientHandler(meter, clientExecutor));
                }
            });
    final Channel client = clientBootstrap.connect(address).awaitUninterruptibly().channel();

    // Run until client is closed
    client.closeFuture().await();
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.ReqRepBenchmark.java

License:Apache License

public static void main(final String... args) throws InterruptedException {
    final ProgressMeter meter = new ProgressMeter("requests");

    // Codecs//from  www  . j a va 2s.c  o  m

    // Server
    final ServerBootstrap serverBootstrap = new ServerBootstrap()
            .group(new NioEventLoopGroup(1), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(ZMTPCodec.builder().socketType(ROUTER).build());
                    ch.pipeline().addLast(new ServerHandler());
                }
            });
    final Channel server = serverBootstrap.bind(ANY_PORT).awaitUninterruptibly().channel();

    // Client
    final SocketAddress address = server.localAddress();
    final Bootstrap clientBootstrap = new Bootstrap().group(new NioEventLoopGroup())
            .channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(ZMTPCodec.builder().socketType(DEALER).build());
                    ch.pipeline().addLast(new ClientHandler(meter));
                }
            });
    final Channel client = clientBootstrap.connect(address).awaitUninterruptibly().channel();

    // Run until client is closed
    client.closeFuture().await();
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.ThroughputBenchmark.java

License:Apache License

public static void main(final String... args) throws InterruptedException {
    final ProgressMeter meter = new ProgressMeter("messages");

    // Server/*  w w  w.  j a  v a2 s.c o  m*/
    final ServerBootstrap serverBootstrap = new ServerBootstrap()
            .group(new NioEventLoopGroup(1), new NioEventLoopGroup(1)).channel(NioServerSocketChannel.class)
            .childOption(ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(ZMTPCodec.of(ROUTER));
                    ch.pipeline().addLast(new ServerHandler(meter));
                }
            });
    final Channel server = serverBootstrap.bind(ANY_PORT).awaitUninterruptibly().channel();

    // Client
    final SocketAddress address = server.localAddress();
    final Bootstrap clientBootstrap = new Bootstrap().group(new NioEventLoopGroup(1))
            .channel(NioSocketChannel.class).option(ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE)
            .handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(ZMTPCodec.of(DEALER));
                    ch.pipeline().addLast(new ClientHandler());
                }
            });
    final Channel client = clientBootstrap.connect(address).awaitUninterruptibly().channel();

    // Run until client is closed
    client.closeFuture().await();
}