I have added a shutdown hook via:
Runtime.getRuntime().addShutdownHook(myShutdownHook);
It works fine normally, but not when I click the red stop button in Eclipse. Is there a way to make the shutdown hook be called in Eclipse?
before launching, press ctrl-F2 . This shortcut terminates the currently running/debugged application.
Execute spring-boot:run via an Eclipse maven build run configuration. Hit the "Terminate" button on Eclipse's "Console" view.
The command line arguments are stored in Eclipse in a Run Configuration (menu: Run > Run Configurations... , or Run > Debug Configurations... ). Just create two of them, reference the same main class, and specify different command line arguments, e.g. to specify different ports, then Run / Debug both of them.
The red stop button forcibly kills the application, i.e. not gracefully, so the JVM doesn't know that the application is exiting, therefore the shutdown hooks are not invoked.
Unfortunately, there is no way (in Windows, at least) to provide a mechanism that ensures that the hook is always invoked. It's just something that may be invoked, but there is no guarantee.
I made a hack by replacing JavaProcess with decorated one:
IProcess p = launch.getProcesses()[0]; launch.addProcess(new JavaProcessDecorator(p)); launch.removeProcess(p);
And decorator is overriding terminate function.
public class JavaProcessDecorator implements IProcess { private IProcess p; public JavaProcessDecorator(IProcess p) { this.p = p; } private boolean sigkill = false; @SuppressWarnings("rawtypes") @Override public Object getAdapter(Class arg) { return p.getAdapter(arg); } ... @Override public ILaunch getLaunch() { return p.getLaunch(); } @Override public IStreamsProxy getStreamsProxy() { return p.getStreamsProxy(); } @Override public void setAttribute(String s1, String s2) { p.setAttribute(s1, s2); } @Override public void terminate() throws DebugException { if(!sigkill) { try { IDebugIService cs = DirmiServer.INSTANCE.getRemote("main", IDebugIService.class); if(cs != null) cs.modelEvent(new TerminateRequest()); } catch (RemoteException e) { } this.sigkill = true; } else p.terminate(); }}
At first click on red button, I send a message to application asking for gently termination. If it is not working, second click on red button will kill it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With