in shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/shutdown/ShenyuClientShutdownHook.java [82:135]
public void run() {
int shutdownWaitTime = Integer.parseInt(props.getProperty("shutdownWaitTime", "3000"));
int delayOtherHooksExecTime = Integer.parseInt(props.getProperty("delayOtherHooksExecTime", "2000"));
IdentityHashMap<Thread, Thread> hooks = null;
try {
Class<?> clazz = Class.forName(props.getProperty("applicationShutdownHooksClassName", "java.lang.ApplicationShutdownHooks"));
Field field = clazz.getDeclaredField(props.getProperty("applicationShutdownHooksFieldName", "hooks"));
field.setAccessible(true);
hooks = (IdentityHashMap<Thread, Thread>) field.get(clazz);
} catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException ex) {
LOG.error(ex.getMessage(), ex);
}
long s = System.currentTimeMillis();
while (System.currentTimeMillis() - s < delayOtherHooksExecTime) {
for (Iterator<Thread> iterator = Objects.requireNonNull(hooks).keySet().iterator(); iterator.hasNext();) {
Thread hook = iterator.next();
if (hook.getName().equals(ShutdownHookManager.getHookName())) {
continue;
}
if (delayHooks.containsKey(hook) || delayedHooks.containsKey(hook)) {
continue;
}
Thread delayHook = new Thread(() -> {
LOG.info("sleep {}ms", shutdownWaitTime);
try {
TimeUnit.MILLISECONDS.sleep(shutdownWaitTime);
} catch (InterruptedException ex) {
LOG.error(ex.getMessage(), ex);
}
hook.run();
}, hook.getName());
delayHooks.put(delayHook, delayHook);
iterator.remove();
}
for (Iterator<Thread> iterator = delayHooks.keySet().iterator(); iterator.hasNext();) {
Thread delayHook = iterator.next();
Runtime.getRuntime().addShutdownHook(delayHook);
delayedHooks.put(delayHook, delayHook);
iterator.remove();
LOG.info("hook {} will sleep {}ms when it start", delayHook.getName(), shutdownWaitTime);
}
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException ex) {
LOG.error(ex.getMessage(), ex);
}
}
ShutdownHookManager.clearHookName();
props = null;
delayHooks = null;
delayedHooks = null;
}