use of io.netty.bootstrap.ServerBootstrap in project camel by apache.
the class LumberjackServer method start.
/**
* Starts the server.
*
* @throws InterruptedException when interrupting while connecting the socket
*/
public void start() throws InterruptedException {
LOG.info("Starting the LUMBERJACK server (host={}, port={}).", host, port);
// Create the group that will listen for incoming connections
bossGroup = new NioEventLoopGroup(1);
// Create the group that will process the connections
workerGroup = new NioEventLoopGroup(WORKER_THREADS);
// Create the executor service that will process the payloads without blocking netty threads
executorService = new DefaultEventExecutorGroup(WORKER_THREADS, threadFactory);
// Create the channel initializer
ChannelHandler initializer = new LumberjackChannelInitializer(sslContext, executorService, messageProcessor);
// Bootstrap the netty server
ServerBootstrap serverBootstrap = //
new ServerBootstrap().group(bossGroup, //
workerGroup).channel(//
NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, //
100).childHandler(//
initializer);
// Connect the socket
channel = serverBootstrap.bind(host, port).sync().channel();
LOG.info("LUMBERJACK server is started (host={}, port={}).", host, port);
}
use of io.netty.bootstrap.ServerBootstrap in project flink by apache.
the class NettyConnectionManagerTest method testManualConfiguration.
/**
* Tests that the number of arenas and threads can be configured manually.
*/
@Test
public void testManualConfiguration() throws Exception {
// Expected numbers
int numberOfArenas = 1;
int numberOfClientThreads = 3;
int numberOfServerThreads = 4;
// Expected number of threads
Configuration flinkConfig = new Configuration();
flinkConfig.setInteger(NettyConfig.NUM_ARENAS, numberOfArenas);
flinkConfig.setInteger(NettyConfig.NUM_THREADS_CLIENT, 3);
flinkConfig.setInteger(NettyConfig.NUM_THREADS_SERVER, 4);
NettyConfig config = new NettyConfig(InetAddress.getLocalHost(), NetUtils.getAvailablePort(), 1024, 1337, flinkConfig);
NettyConnectionManager connectionManager = new NettyConnectionManager(config);
connectionManager.start(mock(ResultPartitionProvider.class), mock(TaskEventDispatcher.class), mock(NetworkBufferPool.class));
assertEquals(numberOfArenas, connectionManager.getBufferPool().getNumberOfArenas());
{
// Client event loop group
Bootstrap boostrap = connectionManager.getClient().getBootstrap();
EventLoopGroup group = boostrap.group();
Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
f.setAccessible(true);
Object[] eventExecutors = (Object[]) f.get(group);
assertEquals(numberOfClientThreads, eventExecutors.length);
}
{
// Server event loop group
ServerBootstrap bootstrap = connectionManager.getServer().getBootstrap();
EventLoopGroup group = bootstrap.group();
Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
f.setAccessible(true);
Object[] eventExecutors = (Object[]) f.get(group);
assertEquals(numberOfServerThreads, eventExecutors.length);
}
{
// Server child event loop group
ServerBootstrap bootstrap = connectionManager.getServer().getBootstrap();
EventLoopGroup group = bootstrap.childGroup();
Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
f.setAccessible(true);
Object[] eventExecutors = (Object[]) f.get(group);
assertEquals(numberOfServerThreads, eventExecutors.length);
}
}
use of io.netty.bootstrap.ServerBootstrap in project flink by apache.
the class NettyConnectionManagerTest method testMatchingNumberOfArenasAndThreadsAsDefault.
/**
* Tests that the number of arenas and number of threads of the client and
* server are set to the same number, that is the number of configured
* task slots.
*/
@Test
public void testMatchingNumberOfArenasAndThreadsAsDefault() throws Exception {
// Expected number of arenas and threads
int numberOfSlots = 2;
NettyConfig config = new NettyConfig(InetAddress.getLocalHost(), NetUtils.getAvailablePort(), 1024, numberOfSlots, new Configuration());
NettyConnectionManager connectionManager = new NettyConnectionManager(config);
connectionManager.start(mock(ResultPartitionProvider.class), mock(TaskEventDispatcher.class), mock(NetworkBufferPool.class));
assertEquals(numberOfSlots, connectionManager.getBufferPool().getNumberOfArenas());
{
// Client event loop group
Bootstrap boostrap = connectionManager.getClient().getBootstrap();
EventLoopGroup group = boostrap.group();
Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
f.setAccessible(true);
Object[] eventExecutors = (Object[]) f.get(group);
assertEquals(numberOfSlots, eventExecutors.length);
}
{
// Server event loop group
ServerBootstrap bootstrap = connectionManager.getServer().getBootstrap();
EventLoopGroup group = bootstrap.group();
Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
f.setAccessible(true);
Object[] eventExecutors = (Object[]) f.get(group);
assertEquals(numberOfSlots, eventExecutors.length);
}
{
// Server child event loop group
ServerBootstrap bootstrap = connectionManager.getServer().getBootstrap();
EventLoopGroup group = bootstrap.childGroup();
Field f = group.getClass().getSuperclass().getSuperclass().getDeclaredField("children");
f.setAccessible(true);
Object[] eventExecutors = (Object[]) f.get(group);
assertEquals(numberOfSlots, eventExecutors.length);
}
}
use of io.netty.bootstrap.ServerBootstrap in project flink by apache.
the class NettyServer method init.
void init(final NettyProtocol protocol, NettyBufferPool nettyBufferPool) throws IOException {
checkState(bootstrap == null, "Netty server has already been initialized.");
long start = System.currentTimeMillis();
bootstrap = new ServerBootstrap();
switch(config.getTransportType()) {
case NIO:
initNioBootstrap();
break;
case EPOLL:
initEpollBootstrap();
break;
case AUTO:
if (Epoll.isAvailable()) {
initEpollBootstrap();
LOG.info("Transport type 'auto': using EPOLL.");
} else {
initNioBootstrap();
LOG.info("Transport type 'auto': using NIO.");
}
}
// --------------------------------------------------------------------
// Configuration
// --------------------------------------------------------------------
// Server bind address
bootstrap.localAddress(config.getServerAddress(), config.getServerPort());
// Pooled allocators for Netty's ByteBuf instances
bootstrap.option(ChannelOption.ALLOCATOR, nettyBufferPool);
bootstrap.childOption(ChannelOption.ALLOCATOR, nettyBufferPool);
if (config.getServerConnectBacklog() > 0) {
bootstrap.option(ChannelOption.SO_BACKLOG, config.getServerConnectBacklog());
}
// Receive and send buffer size
int receiveAndSendBufferSize = config.getSendAndReceiveBufferSize();
if (receiveAndSendBufferSize > 0) {
bootstrap.childOption(ChannelOption.SO_SNDBUF, receiveAndSendBufferSize);
bootstrap.childOption(ChannelOption.SO_RCVBUF, receiveAndSendBufferSize);
}
// Low and high water marks for flow control
bootstrap.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, config.getMemorySegmentSize() + 1);
bootstrap.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 2 * config.getMemorySegmentSize());
// SSL related configuration
try {
serverSSLContext = config.createServerSSLContext();
} catch (Exception e) {
throw new IOException("Failed to initialize SSL Context for the Netty Server", e);
}
// --------------------------------------------------------------------
// Child channel pipeline for accepted connections
// --------------------------------------------------------------------
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
if (serverSSLContext != null) {
SSLEngine sslEngine = serverSSLContext.createSSLEngine();
config.setSSLVerAndCipherSuites(sslEngine);
sslEngine.setUseClientMode(false);
channel.pipeline().addLast("ssl", new SslHandler(sslEngine));
}
channel.pipeline().addLast(protocol.getServerChannelHandlers());
}
});
// --------------------------------------------------------------------
// Start Server
// --------------------------------------------------------------------
bindFuture = bootstrap.bind().syncUninterruptibly();
localAddress = (InetSocketAddress) bindFuture.channel().localAddress();
long end = System.currentTimeMillis();
LOG.info("Successful initialization (took {} ms). Listening on SocketAddress {}.", (end - start), bindFuture.channel().localAddress().toString());
}
use of io.netty.bootstrap.ServerBootstrap in project elasticsearch by elastic.
the class Netty4HttpServerTransport method doStart.
@Override
protected void doStart() {
boolean success = false;
try {
this.serverOpenChannels = new Netty4OpenChannelsHandler(logger);
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(new NioEventLoopGroup(workerCount, daemonThreadFactory(settings, HTTP_SERVER_WORKER_THREAD_NAME_PREFIX)));
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(configureServerChannelHandler());
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, SETTING_HTTP_TCP_NO_DELAY.get(settings));
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, SETTING_HTTP_TCP_KEEP_ALIVE.get(settings));
final ByteSizeValue tcpSendBufferSize = SETTING_HTTP_TCP_SEND_BUFFER_SIZE.get(settings);
if (tcpSendBufferSize.getBytes() > 0) {
serverBootstrap.childOption(ChannelOption.SO_SNDBUF, Math.toIntExact(tcpSendBufferSize.getBytes()));
}
final ByteSizeValue tcpReceiveBufferSize = SETTING_HTTP_TCP_RECEIVE_BUFFER_SIZE.get(settings);
if (tcpReceiveBufferSize.getBytes() > 0) {
serverBootstrap.childOption(ChannelOption.SO_RCVBUF, Math.toIntExact(tcpReceiveBufferSize.getBytes()));
}
serverBootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);
serverBootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR, recvByteBufAllocator);
final boolean reuseAddress = SETTING_HTTP_TCP_REUSE_ADDRESS.get(settings);
serverBootstrap.option(ChannelOption.SO_REUSEADDR, reuseAddress);
serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, reuseAddress);
this.boundAddress = createBoundHttpAddress();
if (logger.isInfoEnabled()) {
logger.info("{}", boundAddress);
}
success = true;
} finally {
if (success == false) {
// otherwise we leak threads since we never moved to started
doStop();
}
}
}
Aggregations