Detect OS sleep and wake up events in Java
Is there any way for Java programs to detect when the operating system is about to go to sleep, or fail, or at least detect wake-up?
The practical problem is that in a particular application, many MySQL database operations run in the background When testing on Windows machines, these database transactions were interrupted after sleep / wake cycle, resulting in a large number of errors in the program These errors are usually as follows:
java.net.socketException MESSAGE: Software caused connection abort: recv Failed
If we can respond to the "going to sleep" event, we can try to pause the background operation to solve the problem Ideally, if we can respond to "just wake up" events, we can at least suppress error messages
Solution
You can detect wakeup by periodically comparing the current system time with the previous system time
Edit: This is an example. It seems that it can help you detect when the machine goes to sleep: http://www.codeguru.com/cpp/w-p/system/messagehandling/article.php/c6907