Java Spring How to invoke #PreDestroy when windows shuts down? - java

I run the application from the command line and I need the method to be executed and send a post request when Windows shuts down
#PreDestroy - It only works if I end the application manually. Is it possible to somehow call the method in the code when windows shuts down?
I already tried to prescribe in the configuration server.shutdown: graceful
Or maybe there is another way to call the method before Windows shuts down?

Related

killing a java process spawned by another java process

I have created a windows service in java in start of the service i have following code
mProcess = Runtime.getRuntime().exec("D:\\manager.exe");
This manager.exe spawn another java process, I can see the process in task manager as "javaw.exe".
I want to kill this process when service is stopped for this i have used the below code in stop method of service
mProcess.destroy();
But this does not work.
Note: Just as trial tried it for notepad it works fine.
So, how can get the handle to the spawned javaw.exe so that i can kill it when the service is stopped.
The question is not how to close the process from task manager but from Stop method of my service.

Gracefully stop java cli application on windows (ctrl+c)

This is only Windows specific, since in Unix system this works properly.
I have a java cli application (its actually a service written using spring-boot) that runs in the console (cmd).
When pressing ctrl+c in console the application gets killed on windows in a way that java virtual machine itself still remains in memory but the service is killed. This is very annoying since some of the resources still remains locked.
On uinux systems this works by generating an SIGINT and service has the possibility of graceful stop and jvm also exit properly.
Any workarounds?

Windows Tomcat properly shut down

I have an application that starts tomcat with an hidden command line (like startup.bat), when I shut down Windows tomcat process is not closed propery but simply killed. I can feel it because is not call the ContextDestroy method of my web application. By other hand if I call tomcat shutdown.bat the method is called.
Is there any way to let windows close correctly tomcat?

How to exit rmi server application

if we are to have an RMI server application which runs permanently via autosys and then shuts down for a few hours over night, how to we send the shutdown message if the application was started from the command line.
for example, we start the jar using java -jar server.jar and now the application is running. if it contained a applicationClose() method how could we call it - we could parse the message from the client but of course this is undesirable?
It is perfectly acceptable to make a closedown request method available, that returns OK and then calls System.exit().
You might want to protect it with some sort of required credentials before it can be called, but other than that there is no reason why you can't.
The other option is for the process to have another TCP port open with your own protocol for shutting down, or to have a JMX extension installed to remotely shut down the service.

Starting and reconnecting to a spring bean from console

I like to write a little server application being controlled by a little console app (start, pause, stop, etc). For the server spring should be used (part of it already exist). So what I do to start the server is something like this:
start a server thread and exit main method
and then, in the thread:
load application context from xml
start some worker threads connecting to beans doing stuff
Now I want the server to be stopped by another command. How can I connect to the already running context and shut it down?
Thanks for your help,
Alexander
The classical way to manage running Java code is through JMX.
Spring lets you export beans as MBeans.
And for accessing JMX via the command line, look at the answers to this recent question:
Calling JMX MBean method from a shell script
You could create a pid file, when the server starts, it should log the pid to a file, server.pid. When you do a stop, you can read the process and kill it. Or even simpler, have a ever running thread in your main class which keeps looking for a file in some folder. As soon as the file becomes available or gets modified, it will stop the server.

Categories