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.srotya.sidewinder.core.ingress.binary.NettyBinaryIngestionServer.java

License:Apache License

public void start() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(4);

    ServerBootstrap bs = new ServerBootstrap();
    channel = bs.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_RCVBUF, 10485760).option(ChannelOption.SO_SNDBUF, 10485760)
            .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new ChannelInitializer<SocketChannel>() {

                @Override//from  w  w w .ja  v  a  2s  .  co m
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new LengthFieldBasedFrameDecoder(3000, 0, 4, 0, 4));
                    p.addLast(new SeriesDataPointDecoder());
                    p.addLast(new SeriesDataPointWriter(storageEngine));
                }

            }).bind("localhost", 9927).sync().channel();
}

From source file:com.srotya.sidewinder.core.ingress.binary.NettyIngestionClient.java

License:Apache License

public static void main(String[] args) throws InterruptedException {
    EventLoopGroup group = new NioEventLoopGroup(1);
    try {//  w w  w.ja v  a  2s  .c o  m
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_RCVBUF, 10485760)
                .option(ChannelOption.SO_SNDBUF, 10485760).handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new LengthFieldPrepender(4));
                        p.addLast(new SeriesDataPointEncoder());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect("localhost", 9927).sync();
        Channel channel = f.channel();
        for (int k = 0; k < TOTAL; k++) {
            List<DataPoint> data = new ArrayList<>();
            for (int i = 0; i < 100; i++) {
                DataPoint dp = new DataPoint("test", "cpu" + i, "value", Arrays.asList("2"),
                        System.currentTimeMillis() + i * k, System.currentTimeMillis() + i * k);
                dp.setFp(false);
                data.add(dp);
            }
            // Thread.sleep(1);
            channel.writeAndFlush(data);
        }
        System.out.println("Data points:" + TOTAL);
        channel.flush();
        channel.closeFuture().sync();
        System.exit(0);
    } finally {
        // Shut down the event loop to terminate all threads.
    }
}

From source file:com.srotya.sidewinder.core.ingress.http.NettyHTTPIngestionServer.java

License:Apache License

public void start() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(4);

    ServerBootstrap bs = new ServerBootstrap();
    channel = bs.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_RCVBUF, 10485760).option(ChannelOption.SO_SNDBUF, 10485760)
            .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(new ChannelInitializer<SocketChannel>() {

                @Override/*from  www .  ja  v a 2s.co m*/
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();
                    p.addLast(new HttpRequestDecoder());
                    p.addLast(new HttpResponseEncoder());
                    p.addLast(new HTTPDataPointDecoder(storageEngine));
                }

            }).bind("localhost", 9928).sync().channel();
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TCPServerSource.java

License:Apache License

private void createAndStartTCPServer(List<ConfigIssue> issues, String portsField) {
    tcpServer = new TCPConsumingServer(config.enableEpoll, config.numThreads, addresses,
            new ChannelInitializer<SocketChannel>() {
                @Override/*from  w  w  w  .j av  a 2s.  c om*/
                public void initChannel(SocketChannel ch) throws Exception {
                    if (config.tlsConfigBean.isEnabled()) {
                        // Add TLS handler into pipeline in the first position
                        ch.pipeline().addFirst("TLS", new SslHandler(config.tlsConfigBean.createSslEngine()));
                    }

                    ch.pipeline().addLast(
                            // first, decode the ByteBuf into some POJO type extending MessageToRecord
                            buildByteBufToMessageDecoderChain(issues).toArray(new ChannelHandler[0]));

                    ch.pipeline().addLast(
                            // next, handle MessageToRecord instances to build SDC records and errors
                            new TCPObjectToRecordHandler(getContext(), config.batchSize, config.maxWaitTime,
                                    pipelineIdsToFail::put, getContext().createELEval(RECORD_PROCESSED_EL_NAME),
                                    getContext().createELVars(), config.recordProcessedAckMessage,
                                    getContext().createELEval(BATCH_COMPLETED_EL_NAME),
                                    getContext().createELVars(), config.batchCompletedAckMessage,
                                    config.timeZoneID, Charset.forName(config.ackMessageCharset)));

                    if (config.readTimeout > 0) {
                        ch.pipeline().addLast(new ReadTimeoutHandler(config.readTimeout));
                    }
                }
            });
    if (issues.isEmpty()) {
        try {
            tcpServer.listen();
            tcpServer.start();
        } catch (Exception ex) {
            tcpServer.destroy();
            tcpServer = null;

            if (ex instanceof SocketException && privilegedPortUsage) {
                issues.add(getContext().createConfigIssue(Groups.TCP.name(), portsField, Errors.TCP_04,
                        config.ports, ex));
            } else {
                LOG.debug("Caught exception while starting up TCP server: {}", ex);
                issues.add(getContext().createConfigIssue(null, null, Errors.TCP_00, addresses.toString(),
                        ex.toString(), ex));
            }
        }
    }
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TestTCPServerSource.java

License:Apache License

private ChannelFuture startTcpClient(TCPServerSourceConfig configBean, EventLoopGroup workerGroup, byte[] data,
        boolean randomlySlice) throws InterruptedException {
    ChannelFuture channelFuture;//from   www . jav  a 2 s. c om
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(workerGroup);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.handler(new ChannelInitializer() {
        @Override
        protected void initChannel(Channel ch) throws Exception {
            ch.pipeline().addLast(new TCPServerSourceClientHandler(randomlySlice, data));
        }
    });

    // Start the client.
    channelFuture = bootstrap.connect("localhost", Integer.parseInt(configBean.ports.get(0))).sync();

    return channelFuture;
}

From source file:com.study.hc.net.netty.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the server.
    // EventLoopGroup   accept NioEventLoop
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    // EventLoopGroup   I/O
    EventLoopGroup workerGroup2 = new NioEventLoopGroup(1);
    try {/*  ww  w  .j av a 2 s .  c  o m*/
        // ??
        ServerBootstrap b = new ServerBootstrap();
        // ???reactor???
        b.group(bossGroup, workerGroup2).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.DEBUG))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new EchoServerHandler());
                    }
                });
        // bind??
        ChannelFuture f = b.bind(PORT).sync();
        // ??
        f.channel().closeFuture().sync();
    } finally {
        // 
        bossGroup.shutdownGracefully();
        workerGroup2.shutdownGracefully();
    }
}

From source file:com.system.distribute.server.FileServer.java

License:Apache License

public void run() throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from   www  . ja v a  2  s .c  o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8),
                                new LineBasedFrameDecoder(8192), new StringDecoder(CharsetUtil.UTF_8),
                                new FileHandler());
                    }
                });

        // Start the server.
        ChannelFuture f = b.bind(port).sync();

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.tesora.dve.db.mysql.MysqlConnection.java

License:Open Source License

@Override
public void connect(String url, final String userid, final String password, final long clientCapabilities)
        throws PEException {
    PEUrl peUrl = PEUrl.fromUrlString(url);

    if (!"mysql".equalsIgnoreCase(peUrl.getSubProtocol()))
        throw new PEException(MysqlConnection.class.getSimpleName()
                + " does not support the sub protocol of url \"" + url + "\"");

    InetSocketAddress serverAddress = new InetSocketAddress(peUrl.getHost(), peUrl.getPort());
    final MyBackendDecoder.CharsetDecodeHelper charsetHelper = new CharsetDecodeHelper();
    mysqlBootstrap = new Bootstrap();
    mysqlBootstrap // .group(inboundChannel.eventLoop())
            .channel(NioSocketChannel.class).group(connectionEventGroup)
            .option(ChannelOption.ALLOCATOR,
                    USE_POOLED_BUFFERS ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT)
            .handler(new ChannelInitializer<Channel>() {
                @Override//from   w w w  .  jav  a 2 s . c  om
                protected void initChannel(Channel ch) throws Exception {
                    authHandler = new MysqlClientAuthenticationHandler(new UserCredentials(userid, password),
                            clientCapabilities, NativeCharSetCatalogImpl.getDefaultCharSetCatalog(DBType.MYSQL),
                            targetCharset);

                    if (PACKET_LOGGER)
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));

                    ch.pipeline().addLast(authHandler)
                            .addLast(MyBackendDecoder.class.getSimpleName(),
                                    new MyBackendDecoder(site.getName(), charsetHelper))
                            .addLast(StreamValve.class.getSimpleName(), new StreamValve())
                            .addLast(MysqlCommandSenderHandler.class.getSimpleName(),
                                    new MysqlCommandSenderHandler(site));
                }
            });

    pendingConnection = mysqlBootstrap.connect(serverAddress);

    //      System.out.println("Create connection: Allocated " + totalConnections.incrementAndGet() + ", active " + activeConnections.incrementAndGet());
    channel = pendingConnection.channel();
    physicalID = UUID.randomUUID();

    //TODO: this was moved from execute to connect, which avoids blocking on the execute to be netty friendly, but causes lag on checkout.  Should make this event driven like everything else. -sgossard
    syncToServerConnect();
    authHandler.assertAuthenticated();
    //      channel.closeFuture().addListener(new GenericFutureListener<Future<Void>>() {
    //         @Override
    //         public void operationComplete(Future<Void> future) throws Exception {
    //            System.out.println(channel + " is closed");
    //         }
    //      });
}

From source file:com.tesora.dve.db.mysql.portal.MySqlPortal.java

License:Open Source License

public MySqlPortal(Properties props) throws PEException {
    // This is the port the Portal is going to listen on -
    // default to Mysql's port
    int port = Singletons.require(HostService.class).getPortalPort(props);
    Singletons.replace(MySqlPortalService.class, this);

    InternalLoggerFactory.setDefaultFactory(new Log4JLoggerFactory());

    int max_concurrent = KnownVariables.MAX_CONCURRENT.getValue(null).intValue();

    //TODO: parse/plan is on this pool, which is probably ok, especially with blocking calls to catalog.  Check for responses that can be done by backend netty threads and avoid two context shifts.

    clientExecutorService = new PEThreadPoolExecutor(max_concurrent, max_concurrent, 30L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(), //The thread count limits concurrency here.  Using a bounded queue here would block netty threads (very bad), so this pool could be overrun by 'bad' clients that pipeline. -sgossard
            new PEDefaultThreadFactory("msp-client"));
    clientExecutorService.allowCoreThreadTimeOut(true);

    bossGroup = new NioEventLoopGroup(1, new PEDefaultThreadFactory("msp-boss"));

    //fixes the number of Netty NIO threads to the number of available CPUs.
    workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(),
            new PEDefaultThreadFactory("netty-worker"));

    ServerBootstrap b = new ServerBootstrap();
    try {/*from  w  w  w .  ja  va  2 s .com*/
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        if (PACKET_LOGGER)
                            ch.pipeline().addFirst(new LoggingHandler(LogLevel.INFO));
                        ch.pipeline()
                                .addLast(MSPProtocolDecoder.class.getSimpleName(),
                                        new MSPProtocolDecoder(
                                                MSPProtocolDecoder.MyDecoderState.READ_CLIENT_AUTH))
                                .addLast(new MSPAuthenticateHandlerV10())
                                .addLast(MSPCommandHandler.class.getSimpleName(),
                                        new MSPCommandHandler(clientExecutorService))
                                .addLast(ConnectionHandlerAdapter.getInstance());
                    }
                })

                .childOption(ChannelOption.ALLOCATOR,
                        USE_POOLED_BUFFERS ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true)
                .bind(port).sync();

        logger.info("DVE Server bound to port " + port);

    } catch (Exception e) {
        throw new PEException("Failed to bind DVE server to port " + port + " - " + e.getMessage(), e);
    }
}

From source file:com.tesora.dve.tools.libmy.AsyncExample.java

License:Open Source License

public static void main(String[] args) throws Exception {
    final String mysqlHost = "localhost";
    final int mysqlPort = 3307;
    final String username = "root";
    final String password = "password";
    final boolean isClearText = true;

    final InetSocketAddress serverAddress = new InetSocketAddress(mysqlHost, mysqlPort);

    final MyBackendDecoder.CharsetDecodeHelper charsetHelper = constructCharsetDecodeHelper();
    final SimpleCredentials cred = constructCredentials(username, password, isClearText);
    final JavaCharsetCatalog javaCharsetCatalog = constructJavaCharsetCatalog();
    final MysqlClientAuthenticationHandler authHandler = new MysqlClientAuthenticationHandler(cred,
            ClientCapabilities.DEFAULT_PSITE_CAPABILITIES, javaCharsetCatalog, new AtomicReference<Charset>());

    final NioEventLoopGroup connectionEventGroup = new NioEventLoopGroup(1);
    final Bootstrap mysqlBootstrap = new Bootstrap();
    mysqlBootstrap // .group(inboundChannel.eventLoop())
            .channel(NioSocketChannel.class).group(connectionEventGroup)
            .option(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT)
            .handler(new ChannelInitializer<Channel>() {
                @Override//from  ww  w . java2 s. c  o m
                protected void initChannel(Channel ch) throws Exception {

                    ch.pipeline().addLast(authHandler).addLast(MyBackendDecoder.class.getSimpleName(),
                            new MyBackendDecoder(charsetHelper)).addLast(new ChannelDuplexHandler() {

                                @Override
                                public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
                                        throws Exception {
                                    System.out.println("WRITE:" + msg);
                                    super.write(ctx, msg, promise);
                                }

                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg)
                                        throws Exception {
                                    if (msg instanceof MyFieldPktResponse) {
                                        final MyFieldPktResponse myFieldPktResponse = (MyFieldPktResponse) msg;
                                        System.out.println("COLUMN: " + myFieldPktResponse.getOrig_column()
                                                + "," + myFieldPktResponse.getColumn_type());
                                    } else if (msg instanceof MyTextResultRow) {
                                        final StringBuilder builder = new StringBuilder();
                                        builder.append("ROW:");
                                        final MyTextResultRow textRow = (MyTextResultRow) msg;
                                        for (int i = 0; i < textRow.size(); i++) {
                                            builder.append('\t');
                                            builder.append(textRow.getString(i));
                                        }
                                        System.out.println(builder.toString());
                                    }
                                    super.channelRead(ctx, msg);
                                }

                                @Override
                                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                                    super.channelInactive(ctx);
                                    System.out.println("CLOSE. ");
                                }
                            });
                }
            });

    final ChannelFuture connectFut = mysqlBootstrap.connect(serverAddress);
    connectFut.sync();
    System.out.println("Waiting to finish authenticate handshake");

    authHandler.assertAuthenticated();//don't write a request until you know the login is complete.

    System.out.println("Connected, and authenticated, getting channel to write requests");

    final Channel chan = connectFut.channel();

    System.out.println("Sending two pipelined requests without blocking for first response.");

    chan.write(MSPComQueryRequestMessage.newMessage("show databases", CharsetUtil.UTF_8));
    chan.write(
            MSPComQueryRequestMessage.newMessage("select * from information_schema.tables", CharsetUtil.UTF_8));
    chan.flush();//NOTE:  nothing is sent until this is called.  Use writeAndFlush if you want it to go out immediately.

    System.out.println("Sleeping 5 sec so all results come back"); //normally you would sync to responses here.
    TimeUnit.SECONDS.sleep(5);
    System.out.println("Closing socket.");
    chan.writeAndFlush(MSPComQuitRequestMessage.newMessage());//send friendly hangup message. Probably correct to also wait for server close or an OK packet.

    chan.close();
    chan.closeFuture().sync();
    System.out.println("Exiting.");
    System.exit(0);
}