java netty线程外定时停止并重启
个人方法,有更好的欢迎留言交流
1.定时器
public class test implements CommandLineRunner {
private final static Logger
log
= LoggerFactory.
getLogger
(ScheduledRestart.class);
public static int
isClose
= 0;//0默认开启,1代表关闭
//凌晨0点执行一次
@Scheduled(cron = “0 0 0 /1 * ? “)
private void restartNetty() throws InterruptedException {
log
.debug(“=========定时重启执行==========”);
this.
isClose
= 1;
Thread.
currentThread
();
Thread.
sleep
(10000l);
log
.debug(“=========睡眠10秒后执行重启执行==========”);
start();
}
@Override
public void run(String… args){
log
.debug(“=========初始启动==========”);
start();
}
private void start(){
this.
isClose
= 0;
//netty启动逻辑
}
}
2.在ChannelHandler逻辑处理类在心跳或者接受方法内增加判断关闭线程通道
3.当然要是有超时设置,也可以修改ReadTimeoutHandler这个类的关闭方法增加这个判断
if (ScheduledRestart.
isClose
== 1) {//代表关闭
channelHandlerContext.channel().close();
}