在Quarkus中如何在关闭信号后接受http请求? [英] How to accept http requests after shutdown signal in Quarkus?
本文介绍了在Quarkus中如何在关闭信号后接受http请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试过:
void onShutdown(@Observes final ShutdownEvent event) throws InterruptedException {
log.infof("ShutdownEvent received, waiting for %s seconds before shutting down", shutdownWaitSeconds);
TimeUnit.SECONDS.sleep(shutdownWaitSeconds);
log.info("Continue shutting down");
}
但在收到ShutdownEvent
后,Quarkus已经用503响应http请求。看起来这可以通过preShutdown
中的ShutdownListener方法来完成。我已经实现了这个侦听器,但它还没有被调用。如何注册ShutdownListener
?
此处的使用案例是OpenShift向终止Pod发送请求。
Quarkus
选项1:创建推荐答案扩展
说明为here。ShutdownController
是我自己的类实现ShutdownListener
,其中我在preShutdown
方法中休眠。
class ShutdownControllerProcessor {
@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem("shutdown-controller");
}
@BuildStep
ShutdownListenerBuildItem shutdownListener() {
// Called at build time. Default constructor will be called at runtime.
// Getting MethodNotFoundException when calling default constructor here.
return new ShutdownListenerBuildItem(new ShutdownController(10));
}
}
选项2:修改Shutdown Recorder私有静态最终字段
可以使用反射添加新的关机监听程序。这是一个有点难看的解决方案。registerIfNeeded()
需要在Quarkus启动后调用,例如,计时器在@PostConstruct
之后1秒。
@ApplicationScoped
public class ListenerRegisterer {
public void registerIfNeeded() {
try {
tryToRegister();
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
private void tryToRegister() throws NoSuchFieldException, IllegalAccessException {
final var field = ShutdownRecorder.class.getDeclaredField("shutdownListeners");
field.setAccessible(true);
final var listeners = (List<ShutdownListener>) field.get(null);
if (listeners != null && !listeners.toString().contains("ShutdownController")) {
listeners.add(new ShutdownController(10));
setFinalStatic(field, listeners);
}
}
private static void setFinalStatic(final Field field, final Object newValue) throws NoSuchFieldException, IllegalAccessException {
field.setAccessible(true);
final var modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
}
这篇关于在Quarkus中如何在关闭信号后接受http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文