Port occupancy when springboot integrated netty deployment stops and restarts on weblogic

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();
    }
Mar.22,2021

I have also encountered this problem. My solution is to write a listener, in springboot and call netty's shutdownGracefully method when the springboot container is destroyed. You can shut down the netty service


Brother, have you solved this problem? can you give a solution


public static EventLoopGroup eventLoopGroup = getEventLoopGroup();  
public static EventLoopGroup workGroup = getWorkGroup();  
private static EventLoopGroup getEventLoopGroup() {  
    if (eventLoopGroup == null) {  
        eventLoopGroup = new NioEventLoopGroup();  
    }  
    return eventLoopGroup;  
}  
  
private static EventLoopGroup getWorkGroup() {  
    if (workGroup == null) {  
        workGroup = new NioEventLoopGroup();  
    }  
    return workGroup;  
}

/\*\*  
 \* SpringBoot \* @author Ming  
 \* @date 2020/01/2 09:42  
 \*/@Log4j  
public class ApplicationEventListener implements ApplicationListener {  
  
    @Override  
  public void onApplicationEvent(ApplicationEvent event) {  
        // Spring Boot  
  if (event instanceof ApplicationEnvironmentPreparedEvent) {  
            log.info("SpringBoot");  
        } else if (event instanceof ApplicationPreparedEvent) {  
            log.info("SpringBoot");  
        } else if (event instanceof ContextRefreshedEvent) {  
            log.info("SpringBoot");  
        } else if (event instanceof ApplicationReadyEvent) {  
            log.info("SpringBoot");  
        } else if (event instanceof ContextStartedEvent) {  
            log.info("SpringBoot ");  
        } else if (event instanceof ContextStoppedEvent) {  
            log.info("SpringBoot");  
            RunNettyWebScoket.eventLoopGroup.shutdownGracefully();  
            RunNettyWebScoket.workGroup.shutdownGracefully();  
        } else if (event instanceof ContextClosedEvent) {  
            log.info("SpringBoot");  
            RunNettyWebScoket.eventLoopGroup.shutdownGracefully();  
            RunNettyWebScoket.workGroup.shutdownGracefully();  
        } else {  
              
        }  
    }  
}
Menu