Capture Windows shutdown, log off and restart in Windows service built in Java?
Is there a way to catch a Windows log off or/and shutdown event i开发者_StackOverflow社区n a Windows service built in Java?
I want to capture windows event in a Windows service and after capturing I want to send an mail.
You can be notified in regular usermode code via WM_QUERYENDSESSION. However you'll need to use JNI and create a window with which you associate a WndProc to receive this message.
I do not think that you have a pure Java solution here.
I can see two ways.
You can run your process as a Windows service. So, you can assume that if it is going down it happens together with the OS shutting down the process. It is not 100% correct but relatively simple.
Another way is to find some process that starts when the OS is going down and try to perform polling on processes. Alternatively you can probably use WMI events. To connect to WMI from Java you can use one of: JaWin, J-integra, J-interop or create your own WMI script in VBScript or JScript and run it from Java using CScript.
精彩评论