Is it reasonable to block during Spring initialization?
PHP中文网
PHP中文网 2017-06-23 09:14:20
0
3
924

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中文网

认证高级PHP讲师

reply all(3)
黄舟

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.

巴扎黑

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.

洪涛

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template