I came across this question and am looking for some ideas?
If you just need to cleanup some resources, you can call Runtime.getRuntime().addShutdownHook