本文整理了Java中io.netty.channel.nio.NioEventLoopGroup.<init>()
方法的一些代码示例,展示了NioEventLoopGroup.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NioEventLoopGroup.<init>()
方法的具体详情如下:
包路径:io.netty.channel.nio.NioEventLoopGroup
类名称:NioEventLoopGroup
方法名:<init>
[英]Create a new instance using the default number of threads, the default ThreadFactory and the SelectorProvider which is returned by SelectorProvider#provider().
[中]使用默认线程数、默认ThreadFactory和SelectorProvider#provider()返回的SelectorProvider创建新实例。
代码示例来源: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: dreamhead/moco
public final void run(final String host, final int port, final ChannelHandler pipelineFactory) {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(host, port)
.option(ChannelOption.TCP_NODELAY, true)
.handler(pipelineFactory);
try {
Channel channel = bootstrap.connect().sync().channel();
ChannelFuture future = channel.closeFuture().sync();
future.addListener(ChannelFutureListener.CLOSE);
} catch (InterruptedException e) {
throw new MocoException(e);
} finally {
group.shutdownGracefully();
}
}
}
代码示例来源: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: 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: normanmaurer/netty-in-action
public void start()
throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(
new EchoClientHandler());
}
});
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
代码示例来源:origin: ethereum/ethereumj
public Channel create(String bindAddr, int port) throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup(1);
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
public void initChannel(NioDatagramChannel ch)
throws Exception {
ch.pipeline().addLast(new PacketDecoder());
SimpleMessageHandler messageHandler = new SimpleMessageHandler(ch, nodeManager);
nodeManager.setMessageSender(messageHandler);
ch.pipeline().addLast(messageHandler);
}
});
return b.bind(bindAddr, port).sync().channel();
}
代码示例来源:origin: apache/incubator-dubbo
/**
* start server, bind port
*/
public void start() throws Throwable {
if (!started.compareAndSet(false, true)) {
return;
}
boss = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-boss", true));
worker = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-worker", true));
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boss, worker);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new QosProcessHandler(welcome, acceptForeignIp));
}
});
try {
serverBootstrap.bind(port).sync();
logger.info("qos-server bind localhost:" + port);
} catch (Throwable throwable) {
logger.error("qos-server can not bind localhost:" + port, throwable);
throw throwable;
}
}
代码示例来源:origin: ethereum/ethereumj
public void start(String[] args) throws Exception {
NioEventLoopGroup group = new NioEventLoopGroup(1);
channel = b.bind(address, port).sync().channel();
channel.closeFuture().sync();
if (shutdown) {
logger.info("Shutdown discovery UDPListener");
代码示例来源:origin: andsel/moquette
@SuppressWarnings("FutureReturnValueIgnored")
public Client(String host, int port) {
handler.setClient(this);
workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new MqttDecoder());
pipeline.addLast("encoder", MqttEncoder.INSTANCE);
pipeline.addLast("handler", handler);
}
});
// Start the client.
m_channel = b.connect(host, port).sync().channel();
} catch (Exception ex) {
LOG.error("Error received in client setup", ex);
workerGroup.shutdownGracefully();
}
}
代码示例来源: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: ainilili/ratel
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap()
.handler(new DefaultChannelInitializer());
SimplePrinter.printNotice("Connecting to " + serverAddress + ":" + port);
Channel channel = bootstrap.connect(serverAddress, port).sync().channel();
channel.closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
代码示例来源:origin: andsel/moquette
@SuppressWarnings("FutureReturnValueIgnored")
private RawClient(String host, int port) {
handler = new RawMessageHandler();
heapBuffer = Unpooled.buffer(128);
disconnectLatch = new CountDownLatch(1);
readableBytesSem = new Semaphore(0, true);
workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("handler", handler);
}
});
// Start the client.
m_channel = b.connect(host, port).sync().channel();
this.connected = true;
} catch (Exception ex) {
LOG.error("Error received in client setup", ex);
workerGroup.shutdownGracefully();
}
}
代码示例来源: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: 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: apache/incubator-dubbo
/**
* start server, bind port
*/
public void start() throws Throwable {
if (!started.compareAndSet(false, true)) {
return;
}
boss = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-boss", true));
worker = new NioEventLoopGroup(0, new DefaultThreadFactory("qos-worker", true));
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(boss, worker);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new QosProcessHandler(welcome, acceptForeignIp));
}
});
try {
serverBootstrap.bind(port).sync();
logger.info("qos-server bind localhost:" + port);
} catch (Throwable throwable) {
logger.error("qos-server can not bind localhost:" + port, throwable);
throw throwable;
}
}
代码示例来源:origin: glowroot/glowroot
port = uri.getPort();
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
Channel ch = bootstrap.connect(host, port).sync().channel();
if (httpProxyHandler != null) {
ch.closeFuture().sync().get();
Throwable exception = handler.exception;
if (exception != null) {
代码示例来源:origin: yu199195/Raincat
private void groupsNio(final ServerBootstrap bootstrap, final int workThreads) {
workerGroup = new NioEventLoopGroup(workThreads);
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(EpollChannelOption.TCP_CORK, true)
.option(EpollChannelOption.SO_KEEPALIVE, true)
.option(EpollChannelOption.SO_BACKLOG, 100)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(nettyServerHandlerInitializer);
}
代码示例来源: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: kaaproject/kaa
/**
* Netty HTTP server initialization.
*/
public void init() {
try {
LOG.info("NettyServer Initializing...");
bossGroup = new NioEventLoopGroup();
LOG.debug("NettyServer bossGroup created");
workerGroup = new NioEventLoopGroup();
LOG.debug("NettyServer workGroup created");
btsServer = new ServerBootstrap();
LOG.debug("NettyServer ServerBootstrap created");
ChannelInitializer<SocketChannel> serverInit = configureInitializer();
LOG.debug("NettyServer InitClass instance created");
LOG.debug("NettyServer InitClass instance init()");
btsServer.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(serverInit)
.option(ChannelOption.SO_REUSEADDR, true);
LOG.debug("NettyServer ServerBootstrap group initialized");
bindChannel = btsServer.bind(bindAddress, bindPort).sync().channel();
} catch (Exception exception) {
LOG.error("NettyHttpServer init() failed", exception);
}
}
代码示例来源:origin: apache/hive
public void start() throws IOException {
LOG.info("Starting LlapOutputFormatService");
int portFromConf = HiveConf.getIntVar(conf, HiveConf.ConfVars.LLAP_DAEMON_OUTPUT_SERVICE_PORT);
int sendBufferSize = HiveConf.getIntVar(conf,
HiveConf.ConfVars.LLAP_DAEMON_OUTPUT_SERVICE_SEND_BUFFER_SIZE);
// Netty defaults to no of processors * 2. Can be changed via -Dio.netty.eventLoopThreads
eventLoopGroup = new NioEventLoopGroup();
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(eventLoopGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(new LlapOutputFormatServiceChannelHandler(sendBufferSize));
try {
listeningChannelFuture = serverBootstrap.bind(portFromConf).sync();
this.port = ((InetSocketAddress) listeningChannelFuture.channel().localAddress()).getPort();
LOG.info("LlapOutputFormatService: Binding to port: {} with send buffer size: {} ", this.port,
sendBufferSize);
} catch (InterruptedException err) {
throw new IOException("LlapOutputFormatService: Error binding to port " + portFromConf, err);
}
}
内容来源于网络,如有侵权,请联系作者删除!