Results 1 to 3 of 3
  1. #1
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Shutdown hook not logging output

    I have an application that starts up, the first thing it does is logs that its starting. The second thing it does is executes a notrunning function that checks to see if the application is running, if not, it adds a shutdown hook. The function is as below:
    Java Code:
    private static boolean notRunning(final PCHLogger log){
    		try {
    			final File file = new File(MyPaths.LOCK.path());
    			final RandomAccessFile raf = new RandomAccessFile(file, "rw");
    			final FileLock fl = raf.getChannel().tryLock();
    			if(fl != null){
    				Runtime.getRuntime().addShutdownHook(new Thread() {
    					public void run() {
    						try {
    							log.info("*** SHUTDOWN ***");
    							fl.release();
    							raf.close();
    							file.delete();
    						}
    						catch (Exception ex){
    							log.severe("Unable to remove lockfile " + file + ex);
    						}
    					}
    				});
    				return false;
    			}
    		}
    		catch (Exception ex){
    			log.severe("Unable to create and/or lock file " + ex);
    		}
    		return true;
    	}
    On my development system, the "SHUTDOWN" message logs without exception. But after having moved the application to another workstation, the "SHUTDOWN" is logged seemingly randomly. Sometimes it logs, sometimes it doesn't., In every case, the lock file is getting delete, so I know its making it into that part of the logic. Its almost like that PCHLogger log object is getting destroyed before the hook thread executes. Am I on the right track? Is there a way around this?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Shutdown hook not logging output

    Never used them before. But my understanding is that they need to be defensively coded with thread safety in mind. Is it possible that the speed of your two machines is sufficiently different to cause this behavior? That can be a characteristic side effect of thread processing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Re: Shutdown hook not logging output

    The problem is that my PCHLogger class uses java.util.logging.Logger. This class has its own shutdown hook coded it. On shutdown, java executes all shutdown hooks, but doesn't do so with any rhyme or reason so if the Logger shutdown hook executes before the one I have, the Logging object is dead.

    I found this resource that explains the workaround, but its not working for me probably because I have multiple classes, not all contained in one like the example.

    I built a seperate class file MyLogManager and did as in the example above.
    Java Code:
    public class MyLogManager extends LogManager {
    
        static MyLogManager instance;
        public MyLogManager() {
        	instance = this;
        }
        
        @Override public void reset() { /* don't reset yet. */ }
        private void reset0() { super.reset(); }
        public static void resetFinally() { 
        	instance.reset0();
        }
    }
    Then from my main class I call the method from the shutdown hook
    Java Code:
    MyLogManager.resetFinally();
    But am getting a null pointer exception. It seems that the instance variable is not getting set to anything. Why?

Similar Threads

  1. Replies: 5
    Last Post: 04-29-2014, 08:57 AM
  2. How I can hook the keyboard?
    By SSaAA in forum New To Java
    Replies: 3
    Last Post: 06-10-2012, 01:51 PM
  3. How to hook into another Java application's thread?
    By mnpeep in forum Advanced Java
    Replies: 2
    Last Post: 03-05-2012, 11:41 PM
  4. Logging Standard output to a file
    By jhyuk in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2009, 11:31 PM
  5. Need to hook up my jsp code to third party vendor
    By priya123 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-28-2008, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •