Home  >  Q&A  >  body text

Is it reasonable to block during Spring initialization?

Read the code of NettyRPC and found that Spring's InitializingBean is implemented in RpcServer. A Netty server is started in the method implementation and blocked.

@Override
public void afterPropertiesSet() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {       
        // 前略...        
        future.channel().closeFuture().sync();
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

Source code address:
https://github.com/luxiaoxun/...

What I want to ask is, will such blocking affect the initialization of Spring? (I have not read the Spring source code and do not know the situation. Please give me some advice...

PHP中文网PHP中文网2674 days ago891

reply all(3)I'll reply

  • 黄舟

    黄舟2017-06-23 09:16:20

    I feel like I asked a very stupid question... The initialization process of the Spring container is single-threaded. If it is blocked here, it will naturally mean that... the follow-up work cannot be carried out.

    A test Bean is added at the end of the bean configuration file, and the log will be logged during initialization, but the result is not printed.

    reply
    0
  • 巴扎黑

    巴扎黑2017-06-23 09:16:20

    This should be reasonable, because if a bean is instantiated by multiple threads, the dependency between the bean and the bean will be difficult to handle, and the code complexity will increase sharply.

    reply
    0
  • PHP中文网

    PHP中文网2017-06-23 09:16:20

    Reasonable, the reason has been mentioned above. If you need to initialize beans asynchronously, just rewrite the logic of BeanFactory. The premise is that you have to ensure that these beans do not depend on each other

    reply
    0
  • Cancelreply