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发送请求。

选项1:创建Quarkus扩展

说明在这里。ShutdownController是我自己实现ShutdownListener的类,其中我在preShutdown方法中有一个sleep。

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:修改ShutdownRecorder专用静态最终字段

可以使用反射添加新的关闭侦听器。这是一个有点难看的解决方案。

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);
}
}

最新更新