Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shutdown application gracefully upon power loss

Tags:

java

shutdown

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?

  • Power goes out. UPS battery mode. App detects this.
  • Application somehow blocks all incoming requests (any suggestions on doing this would be helpful)
  • Within a few minutes all transactions that were in process should be complete.
  • Shutdown application
  • If battery runs out, server goes down...wait to restore.

Is there a way to automatically restart the server and applications upon the power resuming or will this have to be done manually?

like image 323
ryandlf Avatar asked May 16 '13 15:05

ryandlf


2 Answers

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.

like image 113
gma Avatar answered Nov 05 '22 02:11

gma


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.

like image 33
mirofec Avatar answered Nov 05 '22 03:11

mirofec