recently, there is a problem on hand. When the project written by springboot integration netty is packed as a war package and deployed on weblogic11g, use weblogic to start the project and then close the project. When you start the project again, netty has a port occupation problem. After inspection, it is found that netty is not closed when weblogic closes the project, which causes an error when the project starts initializing netty again. Is there any good solution?
@Component
//@DependsOn("fieldChangeListener")
public class NettyInital implements ApplicationListener<ContextRefreshedEvent>{
private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
@Autowired
private MessageController messageControllerImpl;
public void start() {
EventLoopGroup parentGroup = new NioEventLoopGroup();
EventLoopGroup childGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(parentGroup, childGroup);
//
bootstrap.option(ChannelOption.SO_BACKLOG, 128)
//false
.childOption(ChannelOption.SO_KEEPALIVE, false)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
logger.info("has a request....");
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter()[0]));
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new ServerHandler(messageControllerImpl));
//handler
ch.pipeline().addLast(new StringEncoder());
}
});
ChannelFuture f = bootstrap.bind(ConfigUtil.getNettyPort()).sync();
InetAddress netAddress = InetAddress.getLocalHost();
logger.info(">>>>>>>>>>netty statrt successfully!");
// f.channel().closeFuture().sync();//channelCloseFuture
} catch (Exception e) {
e.printStackTrace();
parentGroup.shutdownGracefully();
childGroup.shutdownGracefully();
}
}
//
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
this.start();
}