鍍金池/ 問(wèn)答/Java  網(wǎng)絡(luò)安全  HTML/ springboot集成netty部署在weblogic上停止再啟動(dòng)時(shí)端口占用

springboot集成netty部署在weblogic上停止再啟動(dòng)時(shí)端口占用

最近手頭上出現(xiàn)了一個(gè)問(wèn)題,將springboot集成netty編寫(xiě)的項(xiàng)目打成war包部署在weblogic11g上時(shí),使用weblogic啟動(dòng)一次項(xiàng)目再關(guān)閉項(xiàng)目,再次啟動(dòng)的時(shí)候netty就發(fā)生了端口占用問(wèn)題,經(jīng)檢查發(fā)現(xiàn)是weblogic關(guān)閉項(xiàng)目的時(shí)候netty并沒(méi)有關(guān)閉,使得項(xiàng)目再次啟動(dòng)初始化netty的時(shí)候報(bào)錯(cuò),請(qǐng)問(wèn)有什么好的解決辦法嗎?

@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);
            //指定通道排隊(duì)數(shù)量
            bootstrap.option(ChannelOption.SO_BACKLOG, 128)
            //心跳包,設(shè)置成false,自己寫(xiě)心跳包
                     .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));
                             //中介者h(yuǎn)andler
                             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();//獲取channel的CloseFuture,阻塞當(dāng)前線程直到關(guān)閉操作完成
            
            
        } catch (Exception e) {
            e.printStackTrace();
            parentGroup.shutdownGracefully();
            childGroup.shutdownGracefully();
        }
        
    }

    //程序啟動(dòng)初始化該類(lèi)
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        this.start();
    }
回答
編輯回答
若相惜

大兄弟,你這問(wèn)題解決了嗎,能否給個(gè)方案

2018年4月23日 02:24