本文整理了Java中io.netty.channel.nio.NioEventLoopGroup
类的一些代码示例,展示了NioEventLoopGroup
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NioEventLoopGroup
类的具体详情如下:
包路径:io.netty.channel.nio.NioEventLoopGroup
类名称:NioEventLoopGroup
[英]MultithreadEventLoopGroup implementations which is used for NIO Selector based Channels.
[中]多线程EventLoopGroup实现,用于基于NIO选择器的通道。
代码示例来源:origin: openzipkin/brave
@Override protected int initServer() throws InterruptedException {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(final Channel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpServerCodec());
p.addLast(new TracingDispatchHandler());
p.addLast(new HelloWorldHandler());
}
});
Channel ch = b.bind(0).sync().channel();
return ((InetSocketAddress) ch.localAddress()).getPort();
}
代码示例来源:origin: blynkkk/blynk-server
public void start(BufferedReader commandInputStream) {
this.nioEventLoopGroup = new NioEventLoopGroup(1);
try {
Bootstrap b = new Bootstrap();
b.group(nioEventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(getChannelInitializer());
// Start the connection attempt.
this.channel = b.connect(host, port).sync().channel();
readUserInput(commandInputStream);
} catch (UnresolvedAddressException uae) {
log.error("Host name '{}' is invalid. Please make sure it is correct name.", host);
} catch (ConnectTimeoutException cte) {
log.error("Timeout exceeded when connecting to '{}:{}'. "
+ "Please make sure host available and port is open on target host.", host, port);
} catch (IOException | InterruptedException e) {
log.error("Error running client. Shutting down.", e);
} catch (Exception e) {
log.error(e);
} finally {
// The connection is closed automatically on shutdown.
nioEventLoopGroup.shutdownGracefully();
}
}
代码示例来源:origin: eclipse-vertx/vert.x
/**
* @return a new event loop group
*/
public EventLoopGroup eventLoopGroup(int nThreads, ThreadFactory threadFactory, int ioRatio) {
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(nThreads, threadFactory);
eventLoopGroup.setIoRatio(ioRatio);
return eventLoopGroup;
}
代码示例来源:origin: blynkkk/blynk-server
public ChannelFuture stop() {
if (nioEventLoopGroup.isTerminated()) {
return channel.voidPromise();
}
ChannelFuture channelFuture = channel.close().awaitUninterruptibly();
nioEventLoopGroup.shutdownGracefully();
return channelFuture;
}
}
代码示例来源:origin: normanmaurer/netty-in-action
public LogEventBroadcaster(InetSocketAddress address, File file) {
group = new NioEventLoopGroup();
bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new LogEventEncoder(address));
this.file = file;
}
代码示例来源:origin: normanmaurer/netty-in-action
/**
* Listing 8.6 Bootstrapping and using ChannelInitializer
* */
public void bootstrap() throws InterruptedException {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializerImpl());
ChannelFuture future = bootstrap.bind(new InetSocketAddress(8080));
future.sync();
}
代码示例来源:origin: ethereum/ethereumj
public void start(String[] args) throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1);
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
channel = b.bind(address, port).sync().channel();
channel.closeFuture().sync();
if (shutdown) {
logger.info("Shutdown discovery UDPListener");
group.shutdownGracefully().sync();
代码示例来源:origin: glowroot/glowroot
port = uri.getPort();
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
Channel ch = bootstrap.connect(host, port).sync().channel();
if (httpProxyHandler != null) {
ch.writeAndFlush(request).get();
ch.closeFuture().sync().get();
Throwable exception = handler.exception;
if (exception != null) {
group.shutdownGracefully(0, 10, SECONDS).get();
代码示例来源:origin: jwpttcg66/NettyGameServer
@Override
public boolean startService() throws Exception{
boolean serviceFlag = super.startService();
Bootstrap b = new Bootstrap();
eventLoopGroup = new NioEventLoopGroup();
try {
b.group(eventLoopGroup)
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, false)
// .option(ChannelOption.DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION,true)
.option(ChannelOption.SO_REUSEADDR, true) //重用地址
.option(ChannelOption.SO_RCVBUF, 65536)
.option(ChannelOption.SO_SNDBUF, 65536)
.option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(false)) // heap buf 's better
.handler(new LoggingHandler(LogLevel.DEBUG))
.handler(channelInitializer);
// .handler(new GameNetProtoMessageUdpServerChannelInitializer());
// 服务端监听在9999端口
serverChannelFuture = b.bind(serverPort).sync();
// serverChannelFuture.channel().closeFuture().sync();
serverChannelFuture.channel().closeFuture().addListener(ChannelFutureListener.CLOSE);
}catch (Exception e){
logger.error(e.toString(), e);
serviceFlag = false;
}
return serviceFlag;
}
代码示例来源:origin: wuyinxian124/nettybook2
public void run(int port) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new ChineseProverbServerHandler());
b.bind(port).sync().channel().closeFuture().await();
} finally {
group.shutdownGracefully();
}
}
代码示例来源:origin: apache/incubator-dubbo
@Override
protected void doOpen() throws Throwable {
bootstrap = new ServerBootstrap();
bossGroup = new NioEventLoopGroup(1, new DefaultThreadFactory("NettyServerBoss", true));
workerGroup = new NioEventLoopGroup(getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS),
new DefaultThreadFactory("NettyServerWorker", true));
channels = nettyServerHandler.getChannels();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE)
.childOption(ChannelOption.SO_REUSEADDR, Boolean.TRUE)
ChannelFuture channelFuture = bootstrap.bind(getBindAddress());
channelFuture.syncUninterruptibly();
channel = channelFuture.channel();
代码示例来源:origin: apache/incubator-shardingsphere
private void groupsNio(final ServerBootstrap bootstrap) {
workerGroup = new NioEventLoopGroup();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024 * 1024, 16 * 1024 * 1024))
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.TCP_NODELAY, true)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ServerHandlerInitializer());
}
}
代码示例来源:origin: apache/hive
public RpcServer(Map<String, String> mapConf, HiveConf hiveConf) throws IOException, InterruptedException {
this.config = new RpcConfiguration(mapConf);
this.hiveConf = hiveConf;
this.group = new NioEventLoopGroup(
this.config.getRpcThreadCount(),
new ThreadFactoryBuilder()
.setDaemon(true)
.build());
ServerBootstrap serverBootstrap = new ServerBootstrap()
.group(group)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
.childOption(ChannelOption.SO_KEEPALIVE, true);
this.channel = bindServerPort(serverBootstrap).channel();
this.port = ((InetSocketAddress) channel.localAddress()).getPort();
this.pendingClients = Maps.newConcurrentMap();
this.address = this.config.getServerAddress();
代码示例来源:origin: normanmaurer/netty-in-action
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(
new SimpleChannelInboundHandler<ByteBuf>() {
ChannelFuture future = bootstrap.connect(
new InetSocketAddress("www.manning.com", 80));
future.syncUninterruptibly();
代码示例来源:origin: relayrides/pushy
this.bootstrap = new ServerBootstrap();
this.bootstrap.group(eventLoopGroup);
this.shouldShutDownEventLoopGroup = false;
} else {
this.bootstrap.group(new NioEventLoopGroup(1));
this.shouldShutDownEventLoopGroup = true;
this.allChannels = new DefaultChannelGroup(this.bootstrap.config().group().next());
this.bootstrap.channel(ServerChannelClassUtil.getServerSocketChannelClass(this.bootstrap.config().group()));
代码示例来源:origin: eclipse-vertx/vert.x
private ServerBootstrap createH2Server(BiFunction<Http2ConnectionDecoder, Http2ConnectionEncoder, Http2FrameListener> handler) {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.channel(NioServerSocketChannel.class);
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
eventLoopGroups.add(eventLoopGroup);
bootstrap.group(eventLoopGroup);
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
SSLHelper sslHelper = new SSLHelper(serverOptions, Cert.SERVER_JKS.get(), null);
SslHandler sslHandler = new SslHandler(sslHelper.setApplicationProtocols(Arrays.asList(HttpVersion.HTTP_2, HttpVersion.HTTP_1_1)).createEngine((VertxInternal) vertx, DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT));
ch.pipeline().addLast(sslHandler);
ch.pipeline().addLast(new ApplicationProtocolNegotiationHandler("whatever") {
@Override
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) {
if (ApplicationProtocolNames.HTTP_2.equals(protocol)) {
ChannelPipeline p = ctx.pipeline();
Http2ConnectionHandler clientHandler = createHttpConnectionHandler(handler);
p.addLast("handler", clientHandler);
return;
}
ctx.close();
throw new IllegalStateException("unknown protocol: " + protocol);
}
});
}
});
return bootstrap;
}
代码示例来源:origin: testcontainers/testcontainers-java
@Override
public EventLoopGroup init(Bootstrap bootstrap, final DockerClientConfig dockerClientConfig) {
EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0, createThreadFactory());
// TODO do we really need BouncyCastle?
Security.addProvider(new BouncyCastleProvider());
ChannelFactory<NioSocketChannel> factory = () -> configure(new NioSocketChannel());
bootstrap.group(nioEventLoopGroup).channelFactory(factory)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(final SocketChannel channel) throws Exception {
channel.pipeline().addLast(new HttpClientCodec());
channel.pipeline().addLast(new HttpContentDecompressor());
}
});
return nioEventLoopGroup;
}
代码示例来源:origin: ltsopensource/light-task-scheduler
public NettyRemotingServer(RemotingServerConfig remotingServerConfig, final ChannelEventListener channelEventListener) {
super(remotingServerConfig, channelEventListener);
this.serverBootstrap = new ServerBootstrap();
this.bossSelectorGroup = new NioEventLoopGroup(1, new NamedThreadFactory("NettyBossSelectorThread_"));
this.workerSelectorGroup = new NioEventLoopGroup(remotingServerConfig.getServerSelectorThreads(), new NamedThreadFactory("NettyServerSelectorThread_", true));
}
代码示例来源:origin: neo4j/neo4j
@Override
public EventLoopGroup createEventLoopGroup( int numberOfThreads, ThreadFactory threadFactory )
{
return new NioEventLoopGroup( numberOfThreads, threadFactory );
}
代码示例来源:origin: redisson/redisson
copy.setGroup(new NioEventLoopGroup());
hasOwnGroup = true;
channels = new DefaultChannelGroup(copy.getGroup().next());
bootstrap = createBootstrap(copy, Type.PLAIN);
pubSubBootstrap = createBootstrap(copy, Type.PUBSUB);
内容来源于网络,如有侵权,请联系作者删除!