Example usage for io.netty.channel ChannelOption SO_SNDBUF

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

Introduction

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

Prototype

ChannelOption SO_SNDBUF

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

Click Source Link

Usage

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(ServerBootstrap bootstrap) {
    bootstrap.childOption(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }/*  w  w  w .j  a v a 2  s.co  m*/
    if (tcpReceiveBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }

    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.childOption(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.childOption(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);

    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
    bootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
    bootstrap.option(ChannelOption.SO_BACKLOG, acceptBackLog);
}

From source file:org.vertx.java.core.net.impl.TCPSSLHelper.java

License:Open Source License

public void applyConnectionOptions(Bootstrap bootstrap) {
    bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpSendBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }//from w  ww  . j a v  a 2 s.  c  o  m
    if (tcpReceiveBufferSize != -1) {
        bootstrap.option(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
        bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new FixedRecvByteBufAllocator(tcpReceiveBufferSize));
    }
    bootstrap.option(ChannelOption.SO_LINGER, soLinger);
    if (trafficClass != -1) {
        bootstrap.option(ChannelOption.IP_TOS, trafficClass);
    }
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeout);
    bootstrap.option(ChannelOption.ALLOCATOR, PartialPooledByteBufAllocator.INSTANCE);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
}

From source file:org.waarp.common.utility.WaarpNettyUtil.java

License:Open Source License

/**
 * Add default configuration for client bootstrap
 * /*from   w ww . j  a v a2s .  c om*/
 * @param bootstrap
 * @param group
 * @param timeout
 */
public static void setBootstrap(Bootstrap bootstrap, EventLoopGroup group, int timeout) {
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.group(group);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
    bootstrap.option(ChannelOption.SO_RCVBUF, 1048576);
    bootstrap.option(ChannelOption.SO_SNDBUF, 1048576);
}

From source file:org.waarp.common.utility.WaarpNettyUtil.java

License:Open Source License

/**
 * Add default configuration for server bootstrap
 * /*from  ww  w .j  av  a2s  . com*/
 * @param bootstrap
 * @param groupBoss
 * @param groupWorker
 * @param timeout
 */
public static void setServerBootstrap(ServerBootstrap bootstrap, EventLoopGroup groupBoss,
        EventLoopGroup groupWorker, int timeout) {
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.group(groupBoss, groupWorker);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.TCP_NODELAY, true);
    bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
    bootstrap.childOption(ChannelOption.SO_RCVBUF, 1048576);
    bootstrap.childOption(ChannelOption.SO_SNDBUF, 1048576);
}

From source file:org.wenxueliu.netty.client.ClientTest.java

License:Apache License

private void connectRetry(String ip, int port, ChannelFutureListener clientConnectionListener) {
    try {/*from   w  w w.jav a 2s.  c o m*/
        bootstrap = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_SNDBUF, SEND_BUFFER_SIZE)
                .option(ChannelOption.SO_RCVBUF, SEND_BUFFER_SIZE)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT);

        pipelineFactory = new DefaultChannelInitializer(timer, this);
        bootstrap.handler(pipelineFactory);
        //.handler(new ChannelInitializer<SocketChannel>() {

        //    @Override
        //    protected void initChannel(SocketChannel channel) throws Exception {
        //        ChannelPipeline p = channel.pipeline();
        //        p.addLast(new MessageDecoder(),
        //                  new StringEncoder(CharsetUtil.UTF_8),
        //                  new IdleStateHandler(IDLE_TIMEOUT_SEC, 0, 0),
        //                  new BootstrapTimeoutHandler(timer, 10),
        //                  new ConnectionHandler());
        //    }
        //});
        bootstrap.remoteAddress(ip, port);
        ChannelFuture future = bootstrap.connect();
        future.awaitUninterruptibly();
        future.addListener(clientConnectionListener);
    } catch (Exception e) {
        log.warn("Connection to the server {}:{} failed", ip, port);
    }
}

From source file:org.wso2.carbon.protobuf.registry.internal.ProtobufRegistryActivator.java

License:Open Source License

public void start(BundleContext bundleContext) {

    log.info("/////////////////////////////////////");

    // load protobuf server configurations from pbs xml
    ProtobufConfiguration configuration = null;
    try {//ww  w . j ava2s .c  o m
        configuration = ProtobufConfigFactory.build();
    } catch (ProtobufConfigurationException e) {
        String msg = "Error while loading pbs xml file " + e.getLocalizedMessage();
        log.error(msg, e);
        return;
    }

    if (!configuration.isEnabled()) {
        log.debug("ProtobufServer is not enabled in pbs xml");
        return;
    }

    log.info("Starting ProtobufServer...");

    // gathering configurations into local variables
    ServerConfiguration carbonConfig = ServerConfiguration.getInstance();
    org.wso2.carbon.protobuf.registry.config.ServerConfiguration serverConfig = configuration
            .getServerConfiguration();
    ServerCallExecutorThreadPoolConfiguration callExecutorConfig = serverConfig
            .getServerCallExecutorThreadPoolConfiguration();
    TimeoutExecutorThreadPoolConfiguration timeoutExecutorConfig = serverConfig
            .getTimeoutExecutorThreadPoolConfiguration();
    TimeoutCheckerThreadPoolConfiguration timeoutCheckerConfig = serverConfig
            .getTimeoutCheckerThreadPoolConfiguration();
    LoggerConfiguration loggerConfig = serverConfig.getLoggerConfiguration();
    TransportConfiguration transportConfig = configuration.getTransportConfiguration();
    AcceptorsConfiguration acceptorsConfig = transportConfig.getAcceptorsConfiguration();
    ChannelHandlersConfiguration channelHandlersConfig = transportConfig.getChannelHandlersConfiguration();

    String hostName = carbonConfig.getFirstProperty("HostName");
    int port = serverConfig.getPort();
    int portOffset = Integer.parseInt(carbonConfig.getFirstProperty("Ports.Offset"));
    int effectivePort = port + portOffset;
    // server information
    PeerInfo serverInfo = new PeerInfo(hostName, effectivePort);

    int callExecutorCorePoolSize = callExecutorConfig.getCorePoolSize();
    int callExecutorMaxPoolSize = callExecutorConfig.getMaxPoolSize();
    int callExecutorMaxPoolTimeout = callExecutorConfig.getMaxPoolTimeout();
    int callExecutorWorkQueueCapacity = callExecutorConfig.getWorkQueueCapacity();
    // call executor
    RpcServerCallExecutor callExecutor = new ThreadPoolCallExecutor(callExecutorCorePoolSize,
            callExecutorMaxPoolSize, callExecutorMaxPoolTimeout, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(callExecutorWorkQueueCapacity), Executors.defaultThreadFactory());

    serverFactory = new DuplexTcpServerPipelineFactory(serverInfo);
    serverFactory.setRpcServerCallExecutor(callExecutor);

    // if SSL encryption is enabled
    if (serverConfig.isSSLEnabled()) {
        //read keystore and truststore from carbon
        String keystorePassword = carbonConfig.getFirstProperty("Security.KeyStore.Password");
        String keystorePath = carbonConfig.getFirstProperty("Security.KeyStore.Location");
        String truststorePassword = carbonConfig.getFirstProperty("Security.TrustStore.Password");
        String truststorePath = carbonConfig.getFirstProperty("Security.TrustStore.Location");
        RpcSSLContext sslCtx = new RpcSSLContext();
        sslCtx.setKeystorePassword(keystorePassword);
        sslCtx.setKeystorePath(keystorePath);
        sslCtx.setTruststorePassword(truststorePassword);
        sslCtx.setTruststorePath(truststorePath);
        try {
            sslCtx.init();
        } catch (Exception e) {
            String msg = "Couldn't create SSL Context : " + e.getLocalizedMessage();
            log.error(msg, e);
            return;
        }
        serverFactory.setSslContext(sslCtx);
    }

    // Timeout Executor
    int timeoutExecutorCorePoolSize = timeoutExecutorConfig.getCorePoolSize();
    int timeoutExecutorMaxPoolSize = timeoutExecutorConfig.getMaxPoolSize();
    int timeoutExecutorKeepAliveTime = timeoutExecutorConfig.getKeepAliveTime();
    BlockingQueue<Runnable> timeoutExecutorWorkQueue = new ArrayBlockingQueue<Runnable>(
            timeoutExecutorCorePoolSize, false);
    ThreadFactory timeoutExecutorTF = new RenamingThreadFactoryProxy("timeout",
            Executors.defaultThreadFactory());
    RpcTimeoutExecutor timeoutExecutor = new TimeoutExecutor(timeoutExecutorCorePoolSize,
            timeoutExecutorMaxPoolSize, timeoutExecutorKeepAliveTime, TimeUnit.SECONDS,
            timeoutExecutorWorkQueue, timeoutExecutorTF);

    // Timeout Checker
    int timeoutCheckerSleepTimeMs = timeoutCheckerConfig.getPeriod();
    int timeoutCheckerCorePoolSize = timeoutCheckerConfig.getCorePoolSize();
    ThreadFactory timeoutCheckerTF = new RenamingThreadFactoryProxy("check", Executors.defaultThreadFactory());
    RpcTimeoutChecker timeoutChecker = new TimeoutChecker(timeoutCheckerSleepTimeMs, timeoutCheckerCorePoolSize,
            timeoutCheckerTF);
    timeoutChecker.setTimeoutExecutor(timeoutExecutor);
    timeoutChecker.startChecking(serverFactory.getRpcClientRegistry());

    // setup a RPC event listener - it just logs what happens
    RpcConnectionEventNotifier rpcEventNotifier = new RpcConnectionEventNotifier();
    RpcConnectionEventListener listener = new RpcConnectionEventListener() {
        @Override
        public void connectionReestablished(RpcClientChannel clientChannel) {
            log.info("Protobuf connection Reestablished " + clientChannel);
        }

        @Override
        public void connectionOpened(RpcClientChannel clientChannel) {
            log.info("Protobuf connection Opened " + clientChannel);
        }

        @Override
        public void connectionLost(RpcClientChannel clientChannel) {
            log.info("Protobuf connection Lost " + clientChannel);
        }

        @Override
        public void connectionChanged(RpcClientChannel clientChannel) {
            log.info("Protobuf connection Changed " + clientChannel);
        }
    };
    rpcEventNotifier.setEventListener(listener);
    serverFactory.registerConnectionEventListener(rpcEventNotifier);

    // ProtobufServer Logger
    boolean isLogReq = loggerConfig.isLogReqProtoEnabled();
    boolean isLogRes = loggerConfig.isLogResProtoEnabled();
    boolean isLogEve = loggerConfig.isLogEventProtoEnabled();
    CategoryPerServiceLogger logger = new CategoryPerServiceLogger();
    logger.setLogRequestProto(isLogReq);
    logger.setLogResponseProto(isLogRes);
    logger.setLogEventProto(isLogEve);

    if (isLogReq || isLogRes || isLogEve) {
        serverFactory.setLogger(logger);
    } else {
        serverFactory.setLogger(null);
    }

    // Call acceptors parameters
    int acceptorsPoolSize = acceptorsConfig.getPoolSize();
    int acceptorsSendBufferSize = acceptorsConfig.getSendBufferSize();
    int acceptorsReceiverBufferSize = acceptorsConfig.getReceiverBufferSize();
    // Channel handlers parameters
    int channelHandlersPoolSize = channelHandlersConfig.getPoolSize();
    int channelHandlersSendBufferSize = channelHandlersConfig.getSendBufferSize();
    int channelHandlersReceiverBufferSize = channelHandlersConfig.getReceiverBufferSize();
    // enable nagle's algorithm or not
    boolean tcpNoDelay = transportConfig.isTCPNoDelay();

    // boss and worker thread factories
    ThreadFactory bossTF = new RenamingThreadFactoryProxy("boss", Executors.defaultThreadFactory());
    NioEventLoopGroup boss = new NioEventLoopGroup(acceptorsPoolSize, bossTF);
    ThreadFactory workersTF = new RenamingThreadFactoryProxy("worker", Executors.defaultThreadFactory());
    NioEventLoopGroup workers = new NioEventLoopGroup(channelHandlersPoolSize, workersTF);

    // Configure the server.
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(boss, workers);
    bootstrap.channel(NioServerSocketChannel.class);
    bootstrap.option(ChannelOption.SO_SNDBUF, acceptorsSendBufferSize);
    bootstrap.option(ChannelOption.SO_RCVBUF, acceptorsReceiverBufferSize);
    bootstrap.childOption(ChannelOption.SO_RCVBUF, channelHandlersReceiverBufferSize);
    bootstrap.childOption(ChannelOption.SO_SNDBUF, channelHandlersSendBufferSize);
    bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
    bootstrap.childHandler(serverFactory);
    bootstrap.localAddress(serverInfo.getPort());

    // To release resources on shutdown
    CleanShutdownHandler shutdownHandler = new CleanShutdownHandler();
    shutdownHandler.addResource(boss);
    shutdownHandler.addResource(workers);
    shutdownHandler.addResource(callExecutor);
    shutdownHandler.addResource(timeoutChecker);
    shutdownHandler.addResource(timeoutExecutor);

    // Bind and start to accept incoming connections.
    bootstrap.bind();
    log.info("ProtobufServer Serving " + serverInfo);
    // Register ProtobufServer Registry as an OSGi service
    ProtobufRegistry pbsRegistry = new ProtobufRegistryImpl(serverFactory);
    bundleContext.registerService(ProtobufRegistry.class.getName(), pbsRegistry, null);
}

From source file:org.wso2.carbon.transport.http.netty.listener.HTTPTransportListener.java

License:Open Source License

private void startTransport() {
    //Create Bootstrap Configuration from listener parameters
    ServerBootstrapConfiguration.createBootStrapConfiguration(transportProperties);
    ServerBootstrapConfiguration serverBootstrapConfiguration = ServerBootstrapConfiguration.getInstance();
    //boss group is for accepting channels
    EventLoopGroup bossGroup = HTTPTransportContextHolder.getInstance().getBossGroup();
    if (bossGroup == null) {
        bossGroup = new NioEventLoopGroup(
                bossGroupSize != 0 ? bossGroupSize : Runtime.getRuntime().availableProcessors());
        HTTPTransportContextHolder.getInstance().setBossGroup(bossGroup);
    }//ww  w. j ava 2 s .  com
    //worker group is for processing IO
    EventLoopGroup workerGroup = HTTPTransportContextHolder.getInstance().getWorkerGroup();
    if (workerGroup == null) {
        workerGroup = new NioEventLoopGroup(
                workerGroupSize != 0 ? workerGroupSize : Runtime.getRuntime().availableProcessors() * 2);
        HTTPTransportContextHolder.getInstance().setWorkerGroup(workerGroup);
    }
    log.debug("Netty Boss group size " + bossGroup);
    log.debug("Netty Worker group Size" + workerGroup);
    bootstrap = new ServerBootstrap();
    bootstrap.option(ChannelOption.SO_BACKLOG, serverBootstrapConfiguration.getSoBackLog());
    log.debug("Netty Server Socket BACKLOG " + serverBootstrapConfiguration.getSoBackLog());

    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);

    addChannelInitializer();

    bootstrap.childOption(ChannelOption.TCP_NODELAY, serverBootstrapConfiguration.isTcpNoDelay());
    log.debug("Netty Server Socket TCP_NODELAY " + serverBootstrapConfiguration.isTcpNoDelay());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, serverBootstrapConfiguration.isKeepAlive());
    log.debug("Netty Server Socket SO_KEEPALIVE " + serverBootstrapConfiguration.isKeepAlive());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serverBootstrapConfiguration.getConnectTimeOut());
    log.debug(
            " Netty Server Socket CONNECT_TIMEOUT_MILLIS " + serverBootstrapConfiguration.getConnectTimeOut());

    bootstrap.option(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());
    bootstrap.option(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReciveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReciveBufferSize());
    bootstrap.childOption(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReciveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReciveBufferSize());
    bootstrap.childOption(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());

    if (defaultListenerConfig.isBindOnStartup()) {
        bindInterface(defaultListenerConfig);
    }

    TransportListenerManager transportListenerManager = HTTPTransportContextHolder.getInstance().getManager();
    if (transportListenerManager != null) {
        transportListenerManager.registerTransportListener(this);
    }

}

From source file:org.wso2.carbon.transport.http.netty.listener.NettyListener.java

License:Open Source License

private void startTransport() {
    ServerBootstrapConfiguration.createBootStrapConfiguration(nettyConfig.getParameters());
    ServerBootstrapConfiguration serverBootstrapConfiguration = ServerBootstrapConfiguration.getInstance();
    bossGroup = new NioEventLoopGroup(nettyConfig.getBossThreadPoolSize());
    workerGroup = new NioEventLoopGroup(nettyConfig.getWorkerThreadPoolSize());
    bootstrap = new ServerBootstrap();
    bootstrap.option(ChannelOption.SO_BACKLOG, serverBootstrapConfiguration.getSoBackLog());

    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);

    addChannelInitializer();//from   w  w w.j a v  a2 s  . co  m
    bootstrap.childOption(ChannelOption.TCP_NODELAY, serverBootstrapConfiguration.isTcpNoDelay());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, serverBootstrapConfiguration.isKeepAlive());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serverBootstrapConfiguration.getConnectTimeOut());

    bootstrap.option(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    bootstrap.option(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReciveBufferSize());
    bootstrap.childOption(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReciveBufferSize());
    bootstrap.childOption(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());

    setupChannelInitializer();
    try {
        bootstrap.bind(new InetSocketAddress(nettyConfig.getHost(), nettyConfig.getPort())).sync();
        TransportListenerManager artifactDeployer = NettyTransportContextHolder.getInstance().getManager();
        if (artifactDeployer != null) {
            artifactDeployer.registerTransportListener(id, this);
        }
        log.info("Netty Listener starting on port " + nettyConfig.getPort());
    } catch (InterruptedException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.transport.http.netty.listener.ServerConnectorBootstrap.java

License:Open Source License

public void addSocketConfiguration(ServerBootstrapConfiguration serverBootstrapConfiguration) {
    // Set other serverBootstrap parameters
    serverBootstrap.option(ChannelOption.SO_BACKLOG, serverBootstrapConfiguration.getSoBackLog());
    serverBootstrap.childOption(ChannelOption.TCP_NODELAY, serverBootstrapConfiguration.isTcpNoDelay());
    serverBootstrap.option(ChannelOption.SO_KEEPALIVE, serverBootstrapConfiguration.isKeepAlive());
    serverBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
            serverBootstrapConfiguration.getConnectTimeOut());
    serverBootstrap.option(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    serverBootstrap.option(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReceiveBufferSize());
    serverBootstrap.childOption(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReceiveBufferSize());
    serverBootstrap.childOption(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());

    log.debug("Netty Server Socket BACKLOG " + serverBootstrapConfiguration.getSoBackLog());
    log.debug("Netty Server Socket TCP_NODELAY " + serverBootstrapConfiguration.isTcpNoDelay());
    log.debug("Netty Server Socket SO_KEEPALIVE " + serverBootstrapConfiguration.isKeepAlive());
    log.debug("Netty Server Socket CONNECT_TIMEOUT_MILLIS " + serverBootstrapConfiguration.getConnectTimeOut());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReceiveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReceiveBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());
}

From source file:org.wso2.carbon.transport.http.netty.listener.ServerConnectorController.java

License:Open Source License

public void start() {

    Set<TransportProperty> transportPropertiesSet = transportsConfiguration.getTransportProperties();

    Map<String, Object> transportProperties = new HashMap<>();

    if (transportPropertiesSet != null && !transportPropertiesSet.isEmpty()) {
        transportProperties = transportPropertiesSet.stream()
                .collect(Collectors.toMap(TransportProperty::getName, TransportProperty::getValue));

    }/*from w w  w .  j  ava 2s .c  om*/

    // Create Bootstrap Configuration from listener parameters
    ServerBootstrapConfiguration.createBootStrapConfiguration(transportProperties);
    ServerBootstrapConfiguration serverBootstrapConfiguration = ServerBootstrapConfiguration.getInstance();

    // Create Boss Group - boss group is for accepting channels
    EventLoopGroup bossGroup = HTTPTransportContextHolder.getInstance().getBossGroup();
    if (bossGroup == null) {
        int bossGroupSize = Util.getIntProperty(transportProperties, Constants.SERVER_BOOTSTRAP_BOSS_GROUP_SIZE,
                Runtime.getRuntime().availableProcessors());

        bossGroup = new NioEventLoopGroup(bossGroupSize);
        HTTPTransportContextHolder.getInstance().setBossGroup(bossGroup);
    }

    // Create Worker Group - worker group is for processing IO
    EventLoopGroup workerGroup = HTTPTransportContextHolder.getInstance().getWorkerGroup();
    if (workerGroup == null) {
        int workerGroupSize = Util.getIntProperty(transportProperties,
                Constants.SERVER_BOOTSTRAP_WORKER_GROUP_SIZE, Runtime.getRuntime().availableProcessors() * 2);
        workerGroup = new NioEventLoopGroup(workerGroupSize);
        HTTPTransportContextHolder.getInstance().setWorkerGroup(workerGroup);
    }
    // Set Handler Executor
    HTTPTransportContextHolder.getInstance().setHandlerExecutor(new HandlerExecutor());

    bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);

    // Register Channel initializer
    handler = new HTTPServerChannelInitializer();
    handler.setupConnectionManager(transportProperties);
    bootstrap.childHandler(handler);

    int bufferSize = Util.getIntProperty(transportProperties, Constants.OUTPUT_CONTENT_BUFFER_SIZE, 0);

    if (bufferSize != 0) {
        BufferFactory.createInstance(bufferSize);
    }

    // Set other bootstrap parameters
    bootstrap.option(ChannelOption.SO_BACKLOG, serverBootstrapConfiguration.getSoBackLog());
    log.debug("Netty Server Socket BACKLOG " + serverBootstrapConfiguration.getSoBackLog());
    bootstrap.childOption(ChannelOption.TCP_NODELAY, serverBootstrapConfiguration.isTcpNoDelay());
    log.debug("Netty Server Socket TCP_NODELAY " + serverBootstrapConfiguration.isTcpNoDelay());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, serverBootstrapConfiguration.isKeepAlive());
    log.debug("Netty Server Socket SO_KEEPALIVE " + serverBootstrapConfiguration.isKeepAlive());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serverBootstrapConfiguration.getConnectTimeOut());
    log.debug(
            " Netty Server Socket CONNECT_TIMEOUT_MILLIS " + serverBootstrapConfiguration.getConnectTimeOut());
    bootstrap.option(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());
    bootstrap.option(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReceiveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReceiveBufferSize());
    bootstrap.childOption(ChannelOption.SO_RCVBUF, serverBootstrapConfiguration.getReceiveBufferSize());
    log.debug("Netty Server Socket SO_RCVBUF " + serverBootstrapConfiguration.getReceiveBufferSize());
    bootstrap.childOption(ChannelOption.SO_SNDBUF, serverBootstrapConfiguration.getSendBufferSize());
    log.debug("Netty Server Socket SO_SNDBUF " + serverBootstrapConfiguration.getSendBufferSize());

    initialized = true;
}