程序关闭监听

This commit is contained in:
bwcx_jzy 2019-08-13 00:00:36 +08:00
parent 577b375156
commit b0405c58bd
2 changed files with 48 additions and 17 deletions

View File

@ -4,9 +4,11 @@ import cn.hutool.core.thread.ThreadUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.keepbx.plugin.netty.NettyThread;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextClosedEvent;
/**
* netty 服务检测
@ -15,7 +17,7 @@ import org.springframework.context.event.ContextRefreshedEvent;
* @date 2019/8/12
*/
@Configuration
public class NettyServerConfig implements ApplicationListener<ContextRefreshedEvent> {
public class NettyServerConfig implements ApplicationListener {
private static final String CLASS_NAME = "io.netty.bootstrap.ServerBootstrap";
/**
@ -24,17 +26,33 @@ public class NettyServerConfig implements ApplicationListener<ContextRefreshedEv
@Value("${netty.port:8888}")
private int port;
private NettyThread nettyThread;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
try {
Class.forName(CLASS_NAME);
} catch (ClassNotFoundException e) {
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationReadyEvent) {
try {
Class.forName(CLASS_NAME);
} catch (ClassNotFoundException e) {
return;
}
if (port <= 0) {
DefaultSystemLog.LOG().info("端口配置错误:" + port);
return;
}
nettyThread = new NettyThread(port);
ThreadUtil.execute(nettyThread);
return;
}
if (port <= 0) {
DefaultSystemLog.LOG().info("端口配置错误:" + port);
return;
if (event instanceof ContextClosedEvent) {
if (nettyThread != null) {
DefaultSystemLog.LOG().info("关闭netty");
try {
nettyThread.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ThreadUtil.execute(new NettyThread(port));
}
}

View File

@ -19,9 +19,12 @@ import io.netty.handler.stream.ChunkedWriteHandler;
* @author myzf
* @date 2019/8/11 18:41
*/
public class NettyThread implements Runnable {
public class NettyThread implements Runnable, AutoCloseable {
private int port;
private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private Channel channel;
public NettyThread(int port) {
this.port = port;
@ -29,8 +32,8 @@ public class NettyThread implements Runnable {
@Override
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO))
@ -45,13 +48,23 @@ public class NettyThread implements Runnable {
pipeline.addLast(new FileServerHandler());
}
});
Channel ch = b.bind(port).sync().channel();
ch.closeFuture().sync();
channel = b.bind(port).sync().channel();
channel.closeFuture().sync();
} catch (InterruptedException e) {
DefaultSystemLog.ERROR().error("netty 错误", e);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
try {
this.close();
} catch (Exception ignored) {
}
}
}
@Override
public void close() throws Exception {
channel.close();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}