use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project atomix by atomix.
the class NettyMessagingService method initEventLoopGroup.
private void initEventLoopGroup() {
// try Epoll first and if that does work, use nio.
try {
clientGroup = new EpollEventLoopGroup(0, namedThreads("netty-messaging-event-epoll-client-%d", log));
serverGroup = new EpollEventLoopGroup(0, namedThreads("netty-messaging-event-epoll-server-%d", log));
serverChannelClass = EpollServerSocketChannel.class;
clientChannelClass = EpollSocketChannel.class;
return;
} catch (Throwable e) {
log.debug("Failed to initialize native (epoll) transport. " + "Reason: {}. Proceeding with nio.", e.getMessage());
}
clientGroup = new NioEventLoopGroup(0, namedThreads("netty-messaging-event-nio-client-%d", log));
serverGroup = new NioEventLoopGroup(0, namedThreads("netty-messaging-event-nio-server-%d", log));
serverChannelClass = NioServerSocketChannel.class;
clientChannelClass = NioSocketChannel.class;
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project java by wavefrontHQ.
the class StreamIngester method run.
public void run() {
activeListeners.inc();
// Configure the server.
ServerBootstrap b = new ServerBootstrap();
EventLoopGroup parentGroup;
EventLoopGroup childGroup;
Class<? extends ServerChannel> socketChannelClass;
if (Epoll.isAvailable()) {
logger.fine("Using native socket transport for port " + listeningPort);
parentGroup = new EpollEventLoopGroup(1);
childGroup = new EpollEventLoopGroup();
socketChannelClass = EpollServerSocketChannel.class;
} else {
logger.fine("Using NIO socket transport for port " + listeningPort);
parentGroup = new NioEventLoopGroup(1);
childGroup = new NioEventLoopGroup();
socketChannelClass = NioServerSocketChannel.class;
}
try {
b.group(parentGroup, childGroup).channel(socketChannelClass).option(ChannelOption.SO_BACKLOG, 1024).localAddress(listeningPort).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("frame decoder", frameDecoderFactory.getDecoder());
pipeline.addLast("byte array decoder", new ByteArrayDecoder());
pipeline.addLast(commandHandler);
}
});
if (parentChannelOptions != null) {
for (Map.Entry<ChannelOption<?>, ?> entry : parentChannelOptions.entrySet()) {
b.option((ChannelOption<Object>) entry.getKey(), entry.getValue());
}
}
if (childChannelOptions != null) {
for (Map.Entry<ChannelOption<?>, ?> entry : childChannelOptions.entrySet()) {
b.childOption((ChannelOption<Object>) entry.getKey(), entry.getValue());
}
}
// Start the server.
ChannelFuture f = b.bind().sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} catch (final InterruptedException e) {
logger.log(Level.WARNING, "Interrupted");
parentGroup.shutdownGracefully();
childGroup.shutdownGracefully();
logger.info("Listener on port " + String.valueOf(listeningPort) + " shut down");
} catch (Exception e) {
// ChannelFuture throws undeclared checked exceptions, so we need to handle it
if (e instanceof BindException) {
logger.severe("Unable to start listener - port " + String.valueOf(listeningPort) + " is already in use!");
} else {
logger.log(Level.SEVERE, "StreamIngester exception: ", e);
}
} finally {
activeListeners.dec();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.
the class LocalChannelTest method test.
@Test
public void test() throws Exception {
try (NettyChannelAcceptor acceptor = new NettyChannelAcceptor("localhost", 1111, true)) {
acceptor.setEnableRealNetwork(false);
acceptor.setAcceptor(new ServerSideConnectionAcceptor() {
@Override
public ServerSideConnection createConnection(Channel channel) {
channel.setMessagesReceiver(new ChannelEventListener() {
@Override
public void messageReceived(Message message, Channel channel) {
channel.sendReplyMessage(message, Message.ACK("ciao"));
}
@Override
public void channelClosed(Channel channel) {
}
});
return new ServerSideConnection() {
@Override
public long getConnectionId() {
return new Random().nextLong();
}
};
}
});
acceptor.start();
ExecutorService executor = Executors.newCachedThreadPool();
try (Channel client = NettyConnector.connect("localhost", 1111, true, 0, 0, new ChannelEventListener() {
@Override
public void messageReceived(Message message, Channel channel) {
System.out.println("client messageReceived " + message);
}
@Override
public void channelClosed(Channel channel) {
System.out.println("client channelClosed");
}
}, executor, new NioEventLoopGroup(10, executor), new DefaultEventLoopGroup())) {
for (int i = 0; i < 100; i++) {
Message result = client.sendMessageWithReply(Message.ACK("clientId"), 10000);
// System.out.println("result:" + result);
assertEquals(Message.TYPE_ACK, result.type);
}
} finally {
executor.shutdown();
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project herddb by diennea.
the class HDBClient method init.
private void init() {
LOG.log(Level.SEVERE, "init {0}", this);
this.thredpool = Executors.newCachedThreadPool(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r, "hdb-client");
t.setDaemon(true);
return t;
}
});
this.networkGroup = NetworkUtils.isEnableEpoolNative() ? new EpollEventLoopGroup(0, thredpool) : new NioEventLoopGroup(0, thredpool);
this.localEventsGroup = new DefaultEventLoopGroup();
String mode = configuration.getString(ClientConfiguration.PROPERTY_MODE, ClientConfiguration.PROPERTY_MODE_LOCAL);
switch(mode) {
case ClientConfiguration.PROPERTY_MODE_LOCAL:
case ClientConfiguration.PROPERTY_MODE_STANDALONE:
this.clientSideMetadataProvider = new StaticClientSideMetadataProvider(configuration.getString(ClientConfiguration.PROPERTY_SERVER_ADDRESS, ClientConfiguration.PROPERTY_SERVER_ADDRESS_DEFAULT), configuration.getInt(ClientConfiguration.PROPERTY_SERVER_PORT, ClientConfiguration.PROPERTY_SERVER_PORT_DEFAULT), configuration.getBoolean(ClientConfiguration.PROPERTY_SERVER_SSL, ClientConfiguration.PROPERTY_SERVER_SSL_DEFAULT));
break;
case ClientConfiguration.PROPERTY_MODE_CLUSTER:
this.clientSideMetadataProvider = new ZookeeperClientSideMetadataProvider(configuration.getString(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS_DEFAULT), configuration.getInt(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT_DEFAULT), configuration.getString(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, ClientConfiguration.PROPERTY_ZOOKEEPER_PATH_DEFAULT));
break;
default:
throw new IllegalStateException(mode);
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.nio.NioEventLoopGroup in project swift by luastar.
the class HttpServer method start.
public void start() {
// 设置线程名称
ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
EventLoopGroup bossGroup = new NioEventLoopGroup(HttpConstant.SWIFT_BOSS_THREADS, threadFactoryBuilder.setNameFormat("boss-group-%d").build());
EventLoopGroup workerGroup = new NioEventLoopGroup(HttpConstant.SWIFT_WORKER_THREADS, threadFactoryBuilder.setNameFormat("worker-group-%d").build());
// EventExecutorGroup executorGroup = new DefaultEventExecutorGroup(HttpConstant.SWIFT_BUSINESS_THREADS, threadFactoryBuilder.setNameFormat("executor-group-%d").build());
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (ssl) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
SslContext sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
pipeline.addLast(sslContext.newHandler(ch.alloc()));
}
// http request decode and response encode
pipeline.addLast(new HttpServerCodec());
// 将消息头和体聚合成FullHttpRequest和FullHttpResponse
pipeline.addLast(new HttpObjectAggregator(HttpConstant.SWIFT_MAX_CONTENT_LENGTH));
// 压缩处理
pipeline.addLast(new HttpContentCompressor(HttpConstant.SWIFT_COMPRESSION_LEVEL));
// 自定义http服务
// pipeline.addLast(executorGroup, "http-handler", new HttpChannelHandler(handlerMapping));
pipeline.addLast(new HttpChannelHandler(handlerMapping));
}
});
Channel channel = bootstrap.bind(port).sync().channel();
channel.closeFuture().sync();
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
// executorGroup.shutdownGracefully();
HttpThreadPoolExecutor.shutdownGracefully();
}
}
Aggregations