Empty reply
(環境: spring boot 2.1.5 with reactor-netty 0.8.8)
//reactor.netty.resources.LoopResources#dispose
@Override
default void dispose() {
//noop default
disposeLater().subscribe();
}
複製代碼
/** * @author Lambda.J * @version $Id: GracefulShutdown.java, v 0.1 2019-05-27 */
@Component
public class GracefulShutdown {
@Autowired
ReactorResourceFactory reactorResourceFactory;
LoopResources loopResources;
// SpringBoot 2.1.5 reactor.netty.resources.LoopResources#dispose 只 subscribe 沒有 block 形成沒有等待關閉,這邊手工調用,後面若是修復了直接刪除就好
@PostConstruct
void init() throws Exception {
Field field = TcpResources.class.getDeclaredField("defaultLoops");
field.setAccessible(true);
loopResources = (LoopResources) field.get(reactorResourceFactory.getLoopResources());
field.setAccessible(false);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
System.out.println("Graceful: block long to 20s before real shutdown!");
loopResources.disposeLater().block(Duration.ofSeconds(20));
}));
}
}
複製代碼
org.springframework.context.support.AbstractApplicationContext#registerShutdownHook
org.springframework.context.support.AbstractApplicationContext#doClose
reactorServerResourceFactory
關閉後(org.springframework.http.client.reactive.ReactorResourceFactory#destroy
),端口就已經關閉,但還未響應的請求還可繼續響應。