I know the importance of using a UPS to prevent immediate shutdowns of a server. How do I listen for such an event in a Java application?
Say my UPS will continue to run under full load for 5 minutes when the power is discontinued. They server will continue to run but how will my application know that its time to start shutting things down properly?
This particular app is mostly deals with client to database transactions. I'm mostly concerned with corrupted data in the case of a the server losing power immediately in the middle of mysql transactions. Are the points below a proper way of handling a power situation?
Is there a way to automatically restart the server and applications upon the power resuming or will this have to be done manually?
Dealing with UPS has probably to be handled at OS level, with supervision tools like SNMP or System scripts.
I you look online, you will probably find many scripts to execute some actions when receiving a Power Failure event from an UPS.
You have to send a QUIT signal (on Unix/Linux) to your JVM PID and on the JVM side you can register a shutdown hook with method Runtime.addShutdownHook
If using a framework like Spring, it's certainly already registering something and will call the destroy methods of your beans if configured.
Create java socket to listen on defined port from localhost in Java application and configure UPS software to send event to this port when power lost.
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