美文网首页
springboot中netty启动后阻塞了其它模块的启动

springboot中netty启动后阻塞了其它模块的启动

作者: Aaha | 来源:发表于2022-06-25 10:19 被阅读0次
    1.现象描述

    netty等一般放在程序启动后再启动,多以下面方式启动:

    @Component
    @Order(value = 2)
    public class NettyUdpServer implements ApplicationRunner {
    

    如果在 Order 后面还有其它模块被启动,那么其它模块就会被阻塞。

    2.原因分析

    主线程启动netty后,netty会将主线程阻塞。因此,需要采用异步方式或使用线程池来启动netty。

    3.解决办法

    添加异步注解@Async
    在NettyUdpServer的run中添加

    @Async
    @Override
    public void run(ApplicationArguments args) throws Exception {
        start();
    }
    

    异步注解的生效还需要在启动类中激活:

    @SpringBootApplication
    @EnableAsync
    public class SpringApplication { ... }
    

    相关文章

      网友评论

          本文标题:springboot中netty启动后阻塞了其它模块的启动

          本文链接:https://www.haomeiwen.com/subject/okrivrtx.html