Example usage for io.netty.channel ChannelOption ALLOCATOR

List of usage examples for io.netty.channel ChannelOption ALLOCATOR

Introduction

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

Prototype

ChannelOption ALLOCATOR

To view the source code for io.netty.channel ChannelOption ALLOCATOR.

Click Source Link

Usage

From source file:com.yahoo.pulsar.broker.service.BrokerService.java

License:Apache License

public void start() throws Exception {
    this.producerNameGenerator = new DistributedIdGenerator(pulsar.getZkClient(), producerNameGeneratorPath,
            pulsar.getConfiguration().getClusterName());

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.group(acceptorGroup, workerGroup);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
            new AdaptiveRecvByteBufAllocator(1024, 16 * 1024, 1 * 1024 * 1024));

    if (workerGroup instanceof EpollEventLoopGroup) {
        bootstrap.channel(EpollServerSocketChannel.class);
        bootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    } else {//from  w  w  w  .j  a  va2  s.  c om
        bootstrap.channel(NioServerSocketChannel.class);
    }

    ServiceConfiguration serviceConfig = pulsar.getConfiguration();

    bootstrap.childHandler(new PulsarChannelInitializer(this, serviceConfig, false));
    // Bind and start to accept incoming connections.
    bootstrap.bind(new InetSocketAddress(pulsar.getBindAddress(), port)).sync();
    log.info("Started Pulsar Broker service on port {}", port);

    if (serviceConfig.isTlsEnabled()) {
        ServerBootstrap tlsBootstrap = bootstrap.clone();
        tlsBootstrap.childHandler(new PulsarChannelInitializer(this, serviceConfig, true));
        tlsBootstrap.bind(new InetSocketAddress(pulsar.getBindAddress(), tlsPort)).sync();
        log.info("Started Pulsar Broker TLS service on port {}", tlsPort);
    }

    // start other housekeeping functions
    this.startStatsUpdater();
    this.startInactivityMonitor();
    this.startMessageExpiryMonitor();
    this.startBacklogQuotaChecker();
}

From source file:com.yahoo.pulsar.client.impl.ConnectionPool.java

License:Apache License

public ConnectionPool(final PulsarClientImpl client, EventLoopGroup eventLoopGroup) {
    this.eventLoopGroup = eventLoopGroup;
    this.maxConnectionsPerHosts = client.getConfiguration().getConnectionsPerBroker();

    pool = new ConcurrentHashMap<>();
    bootstrap = new Bootstrap();
    bootstrap.group(eventLoopGroup);//  w w w .  j  ava 2 s. co m
    if (SystemUtils.IS_OS_LINUX && eventLoopGroup instanceof EpollEventLoopGroup) {
        bootstrap.channel(EpollSocketChannel.class);
    } else {
        bootstrap.channel(NioSocketChannel.class);
    }

    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
    bootstrap.option(ChannelOption.TCP_NODELAY, client.getConfiguration().isUseTcpNoDelay());
    bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {
        public void initChannel(SocketChannel ch) throws Exception {
            ClientConfiguration clientConfig = client.getConfiguration();
            if (clientConfig.isUseTls()) {
                SslContextBuilder builder = SslContextBuilder.forClient();
                if (clientConfig.isTlsAllowInsecureConnection()) {
                    builder.trustManager(InsecureTrustManagerFactory.INSTANCE);
                } else {
                    if (clientConfig.getTlsTrustCertsFilePath().isEmpty()) {
                        // Use system default
                        builder.trustManager((File) null);
                    } else {
                        File trustCertCollection = new File(clientConfig.getTlsTrustCertsFilePath());
                        builder.trustManager(trustCertCollection);
                    }
                }

                // Set client certificate if available
                AuthenticationDataProvider authData = clientConfig.getAuthentication().getAuthData();
                if (authData.hasDataForTls()) {
                    builder.keyManager(authData.getTlsPrivateKey(),
                            (X509Certificate[]) authData.getTlsCertificates());
                }

                SslContext sslCtx = builder.build();
                ch.pipeline().addLast(TLS_HANDLER, sslCtx.newHandler(ch.alloc()));
            }
            ch.pipeline().addLast("frameDecoder",
                    new PulsarLengthFieldFrameDecoder(MaxMessageSize, 0, 4, 0, 4));
            ch.pipeline().addLast("handler", new ClientCnx(client));
        }
    });
}

From source file:com.yahoo.pulsar.discovery.service.DiscoveryService.java

License:Apache License

/**
 * starts server to handle discovery-request from client-channel
 * /*from   w ww  .  ja va 2 s  .  c  o  m*/
 * @throws Exception
 */
public void startServer() throws Exception {

    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.group(acceptorGroup, workerGroup);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
            new AdaptiveRecvByteBufAllocator(1024, 16 * 1024, 1 * 1024 * 1024));

    if (workerGroup instanceof EpollEventLoopGroup) {
        bootstrap.channel(EpollServerSocketChannel.class);
        bootstrap.childOption(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);
    } else {
        bootstrap.channel(NioServerSocketChannel.class);
    }

    bootstrap.childHandler(new ServiceChannelInitializer(this, config, false));
    // Bind and start to accept incoming connections.
    bootstrap.bind(config.getServicePort()).sync();
    LOG.info("Started Pulsar Broker service on port {}", config.getWebServicePort());

    if (config.isTlsEnabled()) {
        ServerBootstrap tlsBootstrap = bootstrap.clone();
        tlsBootstrap.childHandler(new ServiceChannelInitializer(this, config, true));
        tlsBootstrap.bind(config.getServicePortTls()).sync();
        LOG.info("Started Pulsar Broker TLS service on port {}", config.getWebServicePortTls());
    }
}

From source file:com.ztesoft.zsmart.zmq.remoting.netty.NettyRemotingServer.java

License:Apache License

@Override
public void start() {
    this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(//
            nettyServerConfig.getServerWorkThreads(), //
            new ThreadFactory() {

                private AtomicInteger threadIndex = new AtomicInteger(0);

                @Override/*from  ww w.  j  a  v  a2  s  .  c  o  m*/
                public Thread newThread(Runnable r) {
                    return new Thread(r, "NettyServerWorkerThread_" + this.threadIndex.incrementAndGet());
                }
            });

    ServerBootstrap childHandler = //
            this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupWorker)
                    .channel(NioServerSocketChannel.class)
                    //
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    //
                    .option(ChannelOption.SO_REUSEADDR, true)
                    //
                    .option(ChannelOption.SO_KEEPALIVE, false)
                    //
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    //
                    .option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize())
                    //
                    .option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize())
                    //
                    .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort()))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    //
                                    defaultEventExecutorGroup, //
                                    new NettyEncoder(), //
                                    new NettyDecoder(), //
                                    new IdleStateHandler(0, 0,
                                            nettyServerConfig.getServerChannelMaxIdleTimeSeconds()), //
                                    new NettyConnetManageHandler(), //
                                    new NettyServerHandler());
                        }
                    });

    if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) {
        // ????
        childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    }

    try {
        ChannelFuture sync = this.serverBootstrap.bind().sync();
        InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress();
        this.port = addr.getPort();
    } catch (InterruptedException e1) {
        throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1);
    }

    if (this.channelEventListener != null) {
        this.nettyEventExecuter.start();
    }

    // ?1??
    this.timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                NettyRemotingServer.this.scanResponseTable();
            } catch (Exception e) {
                log.error("scanResponseTable exception", e);
            }
        }
    }, 1000 * 3, 1000);
}

From source file:darks.grid.network.GridMessageClient.java

License:Apache License

@Override
public boolean initialize() {
    super.initialize();
    try {//from  w w w  .j  av  a 2 s  . c  om
        log.info("Initialize message client.");
        NetworkConfig config = GridRuntime.config().getNetworkConfig();
        int workerNum = config.getClientWorkerThreadNumber();
        workerGroup = new NioEventLoopGroup(workerNum, ThreadUtils.getThreadFactory());
        bootstrap = new Bootstrap();
        bootstrap.group(workerGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, config.isTcpNodelay())
                .option(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive())
                .option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true))
                //                   .option(ChannelOption.SO_TIMEOUT, config.getRecvTimeout())
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout())
                .option(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize())
                .option(ChannelOption.SO_RCVBUF, config.getTcpRecvBufferSize());
        bootstrap.handler(newChannelHandler());
        return true;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return false;
    }
}

From source file:darks.grid.network.GridMessageServer.java

License:Apache License

@Override
public boolean initialize() {
    try {/*from   w  w w  . j a v  a 2 s  .  c o  m*/
        NetworkConfig config = GridRuntime.config().getNetworkConfig();
        int bossNum = Runtime.getRuntime().availableProcessors() * config.getServerBossThreadDelta();
        int workerNum = config.getServerWorkerThreadNumber();
        bossGroup = new NioEventLoopGroup(bossNum);
        workerGroup = new NioEventLoopGroup(workerNum);
        super.initialize();
        bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, config.isTcpNodelay())
                .option(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive())
                .option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true))
                //            .option(ChannelOption.SO_TIMEOUT, config.getRecvTimeout())
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout())
                .option(ChannelOption.SO_REUSEADDR, config.isTcpReuseAddr())
                //            .option(ChannelOption.SO_BACKLOG, config.getTcpBacklog())
                .option(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize())
                .option(ChannelOption.SO_RCVBUF, config.getTcpRecvBufferSize())
                .childOption(ChannelOption.TCP_NODELAY, config.isTcpNodelay())
                .childOption(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive())
                .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true))
                .childOption(ChannelOption.SO_TIMEOUT, config.getRecvTimeout())
                .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout())
                .childOption(ChannelOption.SO_REUSEADDR, config.isTcpReuseAddr())
                .childOption(ChannelOption.SO_BACKLOG, config.getTcpBacklog())
                .childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize())
                .childOption(ChannelOption.SO_RCVBUF, config.getTcpRecvBufferSize());
        bootstrap.childHandler(newChannelHandler());
        return true;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return false;
    }
}

From source file:dbseer.middleware.client.MiddlewareClient.java

License:Apache License

public void run() {
    // debug info
    Log.debug(String.format("host = %s", host));
    Log.debug(String.format("port = %d", port));
    Log.debug(String.format("log path = %s", logPath));

    // client needs to handle incoming messages from the middleware as well.
    EventLoopGroup group = new NioEventLoopGroup(4);

    try {/*from  w w w. j  ava2 s.  c o m*/
        // attach shutdown hook.
        MiddlewareClientShutdown shutdownThread = new MiddlewareClientShutdown(this);
        Runtime.getRuntime().addShutdownHook(shutdownThread);

        File logDir = new File(logPath);
        if (!logDir.exists()) {
            logDir.mkdirs();
        }

        final MiddlewareClient client = this;

        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class)
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new IdleStateHandler(10, 0, 0));
                        p.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB));
                        p.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.ZLIB));
                        p.addLast(new MiddlewarePacketDecoder());
                        p.addLast(new MiddlewarePacketEncoder());
                        p.addLast(new MiddlewareClientHandler(client));
                    }
                });

        ChannelFuture f = b.connect(host, port).sync();
        channel = f.channel();
        Log.debug("Connected to the middleware.");

        MiddlewarePacket checkPacket = new MiddlewarePacket(MiddlewareConstants.PACKET_CHECK_VERSION,
                MiddlewareConstants.PROTOCOL_VERSION);
        //         ByteBuf buf = Unpooled.buffer();
        //         buf.writeInt(MiddlewareConstants.PACKET_CHECK_VERSION);
        //         buf.writeInt(MiddlewareConstants.PROTOCOL_VERSION.getBytes("UTF-8").length);
        //         buf.writeBytes(MiddlewareConstants.PROTOCOL_VERSION.getBytes("UTF-8"));
        //         channel.writeAndFlush(buf);
        channel.writeAndFlush(checkPacket);

        channel.closeFuture().sync();
    } catch (Exception e) {
        if (e instanceof InterruptedException) {

        } else {
            setChanged();
            notifyObservers(new MiddlewareClientEvent(MiddlewareClientEvent.ERROR, e));
        }
        Log.error(e.getMessage());
        e.printStackTrace();
    } finally {
        group.shutdownGracefully();
        this.stopExecutors();
        if (txLogFileRaw.exists()) {
            txLogFileRaw.delete();
        }
        if (txZipOutputStream != null) {
            try {
                txZipOutputStream.closeEntry();
                txZipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            txZipOutputStream = null;
        }
    }
}

From source file:dbseer.middleware.server.MiddlewareServer.java

License:Apache License

public void run() throws Exception {
    // basic log info.
    Log.info(String.format("Listening port = %d", port));
    Log.info(String.format("DB log dir = %s", dbLogPath));
    Log.info(String.format("System log dir = %s", sysLogPath));

    // print server info.
    for (Server s : servers.values()) {
        s.printLogInfo();/*w w w.  j ava2  s  .c  o m*/
        // test MySQL/MariaDB connection using JDBC before we start anything.
        if (!s.testConnection()) {
            throw new Exception("Unable to connect to the MySQL server with the given credential.");
        } else if (!s.testMonitoringDir()) {
            throw new Exception("Specified monitoring directory and script do not exist.");
        }
    }

    // open named pipe.
    File checkPipeFile = new File(this.namedPipePath);
    if (checkPipeFile == null || !checkPipeFile.exists() || checkPipeFile.isDirectory()) {
        throw new Exception("Cannot open the named pipe for communication with dbseerroute. "
                + "You must run Maxscale with dbseerroute with correct named pipe first.");
    }

    namedPipeFile = new RandomAccessFile(this.namedPipePath, "rwd");
    if (namedPipeFile == null) {
        throw new Exception("Cannot open the named pipe for communication with dbseerroute. "
                + "You must run Maxscale with dbseerroute with correct named pipe first.");
    }

    // attach shutdown hook.
    MiddlewareServerShutdown shutdownThread = new MiddlewareServerShutdown(this);
    Runtime.getRuntime().addShutdownHook(shutdownThread);

    // let's start accepting connections.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(4);

    final MiddlewareServer server = this;
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128)
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .handler(new MiddlewareServerConnectionHandler(server))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new IdleStateHandler(10, 0, 0));
                        p.addLast(ZlibCodecFactory.newZlibEncoder(ZlibWrapper.ZLIB));
                        p.addLast(ZlibCodecFactory.newZlibDecoder(ZlibWrapper.ZLIB));
                        p.addLast(new MiddlewarePacketDecoder());
                        p.addLast(new MiddlewarePacketEncoder());
                        p.addLast(new MiddlewareServerHandler(server));
                        //                     p.addLast(new MiddlewarePacketDecoder(), new MiddlewareServerHandler(server));
                    }
                });

        Log.info("Middleware is now accepting connections.");

        // bind and start accepting connections.
        ChannelFuture cf = b.bind(port).sync();

        // shutdown the server.
        if (cf != null) {
            cf.channel().closeFuture().sync();
        }
    } catch (Exception e) {
        Log.error(e.getMessage());
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        if (tailerExecutor != null && !tailerExecutor.isShutdown()) {
            tailerExecutor.shutdown();
        }
    }
}

From source file:de.jackwhite20.apex.udp.ApexDatagram.java

License:Open Source License

@Override
public Channel bootstrap(EventLoopGroup bossGroup, EventLoopGroup workerGroup, String ip, int port, int backlog,
        int readTimeout, int writeTimeout) throws Exception {

    logger.info("Bootstrapping datagram server");

    Bootstrap bootstrap = new Bootstrap().group(workerGroup).channel(PipelineUtils.getDatagramChannel())
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .handler(new DatagramUpstreamHandler());

    if (PipelineUtils.isEpoll()) {
        bootstrap.option(EpollChannelOption.EPOLL_MODE, EpollMode.LEVEL_TRIGGERED);

        logger.debug("Epoll mode is now level triggered");
    }//from  w  w  w  . ja v  a 2  s .c  o  m

    return bootstrap.bind(ip, port).sync().channel();
}

From source file:de.ocarthon.core.network.HttpClient.java

License:Apache License

private static Bootstrap defaultBootstrap() {
    if (defaultHttpBootstrap == null) {
        defaultHttpBootstrap = new Bootstrap();
        defaultHttpBootstrap.group(eventLoopGroup);
        defaultHttpBootstrap.option(ChannelOption.TCP_NODELAY, true);
        defaultHttpBootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
        defaultHttpBootstrap.channelFactory(channelFactory);
    }//from  ww  w .  j  ava  2s.com
    return defaultHttpBootstrap;
}