本文整理汇总了Java中io.netty.channel.nio.NioEventLoopGroup.shutdownGracefully方法的典型用法代码示例。如果您正苦于以下问题:Java NioEventLoopGroup.shutdownGracefully方法的具体用法?Java NioEventLoopGroup.shutdownGracefully怎么用?Java NioEventLoopGroup.shutdownGracefully使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.netty.channel.nio.NioEventLoopGroup
的用法示例。
在下文中一共展示了NioEventLoopGroup.shutdownGracefully方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: startNotificationRegisterEndpoint
import io.netty.channel.nio.NioEventLoopGroup; //导入方法依赖的package包/类
protected void startNotificationRegisterEndpoint(final String host, final int port) {
Runnable notificationRegisterEndpointRunnable = new Runnable() {
@Override
public void run() {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new NotificationRegisterServerInitializer());
Channel ch = b.bind(host, port).sync().channel();
logger.info(String.format("Notification register endpoint started at %s:%s", host, port));
ch.closeFuture().sync();
} catch (InterruptedException ex) {
logger.info("Notification register endpoint was interrupted.");
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
};
notificationEndpointThread = new Thread(notificationRegisterEndpointRunnable);
notificationEndpointThread.start();
}
示例2: start
import io.netty.channel.nio.NioEventLoopGroup; //导入方法依赖的package包/类
public void start() {
Configuration config = Configuration.INSTANCE;
InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE);
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline()
.addLast("logging", new LoggingHandler(LogLevel.DEBUG))
.addLast(new SocksInitRequestDecoder())
.addLast(new SocksMessageEncoder())
.addLast(new Socks5Handler())
.addLast(Status.TRAFFIC_HANDLER);
}
});
log.info("\tStartup {}-{}-client [{}{}]", Constants.APP_NAME, Constants.APP_VERSION, config.getMode(), config.getMode().equals("socks5") ? "" : ":" + config.getProtocol());
new Thread(() -> new UdpServer().start()).start();
ChannelFuture future = bootstrap.bind(config.getLocalHost(), config.getLocalPort()).sync();
future.addListener(future1 -> log.info("\tTCP listening at {}:{}...", config.getLocalHost(), config.getLocalPort()));
future.channel().closeFuture().sync();
} catch (Exception e) {
log.error("\tSocket bind failure ({})", e.getMessage());
} finally {
log.info("\tShutting down");
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例3: start
import io.netty.channel.nio.NioEventLoopGroup; //导入方法依赖的package包/类
public void start(int port) {
bossGroup = new NioEventLoopGroup();
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
// .option(ChannelOption.SO_BACKLOG, 100)
// .handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast("httpCodec", new HttpServerCodec());
ch.pipeline().addLast("serverHandle",
new HttpProxyServerHandle(serverConfig, proxyInterceptInitializer, proxyConfig,
httpProxyExceptionHandle));
}
});
ChannelFuture f = b
.bind(port)
.sync();
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
示例4: startServer
import io.netty.channel.nio.NioEventLoopGroup; //导入方法依赖的package包/类
/**
* 初始化
*
* @throws Exception
*/
public void startServer(int port) throws Exception {
NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
NioEventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler())
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new RequestWrapperCodec())
.addLast(new ResponseWrapperCodec())
.addLast(new BusinessServerHandler());
}
});
ChannelFuture channelFuture = bootstrap.bind(port).sync();
channelFuture.addListener((future) -> System.out.println("server listening at: " + ((ChannelFuture) future).channel()));
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
示例5: main
import io.netty.channel.nio.NioEventLoopGroup; //导入方法依赖的package包/类
public static void main(String[] args) throws InterruptedException, NoSuchAlgorithmException {
InetSocketAddress addr = new InetSocketAddress(GOOGLE_SERVER_HOST, GOOGLE_SERVER_PORT);
System.out.printf("Sending request to %s\n", addr);
// Below is Netty boilerplate for setting-up an event loop and registering a handler
NioEventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap()
.group(group)
.remoteAddress(addr)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) throws Exception {
ch.pipeline()
.addLast(new ReadTimeoutHandler(5))
.addLast(new RequestHandler(addr));
}
});
ChannelFuture connectFuture = bootstrap.connect();
connectFuture.addListener(fut -> {
if (!fut.isSuccess()) {
System.out.println("Connect fail:");
System.out.println(fut.cause().getMessage());
}
});
connectFuture.channel().closeFuture().sync();
group.shutdownGracefully();
}
示例6: start
import io.netty.channel.nio.NioEventLoopGroup; //导入方法依赖的package包/类
public void start(int port) {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
ethereumChannelInitializer = ctx.getBean(EthereumChannelInitializer.class, "");
ethereumListener.trace("Listening on port " + port);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());
b.handler(new LoggingHandler());
b.childHandler(ethereumChannelInitializer);
// Start the client.
logger.info("Listening for incoming connections, port: [{}] ", port);
logger.info("NodeId: [{}] ", Hex.toHexString(config.nodeId()));
channelFuture = b.bind(port).sync();
listening = true;
// Wait until the connection is closed.
channelFuture.channel().closeFuture().sync();
logger.debug("Connection is closed");
} catch (Exception e) {
logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName());
throw new Error("Server Disconnected");
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
listening = false;
}
}
示例7: start
import io.netty.channel.nio.NioEventLoopGroup; //导入方法依赖的package包/类
/**
* 步骤:
* 1.创建一个ServerBootstrap的实例以引导和绑定服务器
* 2.创建并分配一个NioEventLoopGroup实例以进行事件的处理,如接受新连接以及读/写数据
* 3.指定服务器绑定的本地的InetSocketAddress
* 4.使用一个EchoServerHandler的实例初始化每一个新的Channel
* 5.调用ServerBootstrap.bind()方法以绑定服务器
*/
public void start() {
/**
* 创建EventLoopGroup
* NioEventLoopGroup用来接收和处理新的连接
*/
NioEventLoopGroup loopGroup = new NioEventLoopGroup();
try {
/**
* 1.创建 ServerBootstrap
*/
new ServerBootstrap()
.group(loopGroup)
/**
* 2.指定所使用的NIO传输Channel
*/
.channel(NioServerSocketChannel.class)
/**
* 3.使用指定的端口设置套接字地址
*/
.localAddress(new InetSocketAddress(port))
/**
* 4.添加一个EchoServerHandler到子Channel的ChannelPipeline
* 当一个新的连接被接受时,一个新的子Channel将会被创建
* ChannelInitializer会把一个EchoServerHandler的实例添加到该Channel的ChannelPipeline中
*/
.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
/**
* EchoServerHandler 被标注为@Shareable,所以我们可以总是使用同样的实例
*/
ch.pipeline().addLast(new EchoServerHandler());
}
})
/**
* 5.异步地绑定服务器,调用sync()方法阻塞等待直到绑定完成
* 返回 ChannelFuture
*/
.bind().sync()
/**
* 6.获取Channel的CloseFuture,并且阻塞当前线程直到它完成
*/
.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
/**
* 7.关闭EventLoopGroup,释放所有的资源
*/
loopGroup.shutdownGracefully();
}
}
示例8: start
import io.netty.channel.nio.NioEventLoopGroup; //导入方法依赖的package包/类
/**
* 启动
*
* @throws Exception
*/
public void start() throws Exception {
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup(workerNum);
NrpcServer nrpcServer = this;
entryThread = new Thread(
new Runnable() {
@Override
public void run() {
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
// 注册handler
ch.pipeline().addLast(new NrpcDecoder(), new NrpcClientHandler(nrpcServer));
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_RCVBUF, 1048576)
.childOption(ChannelOption.SO_SNDBUF, 1048576);
// Bind and start to accept incoming connections.
ChannelFuture future = bootstrap.bind("0.0.0.0",port).sync();
// begin to check service
nrpcServiceRegistry.startCheckService();
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
future.channel().closeFuture().sync();
} catch (Exception ex){
logger.error(ex.getMessage(),ex);
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
},"NrpcServer-"+atomicInteger.addAndGet(1)
);
entryThread.start();
}