Problem with Shutdown Hooks involving multiple class calls

Ok,

I am working on a program for a jsp page where e-mail messages are queued in my MailQueue class and stored when they cannot be sent out through the mail server. The problem I am having is that we want to be able to write the e-mail objects to a file when our server is shutdown so I am trying to employ a shutdown hook to call my function that creates the output file. The problem is I have a MailQueue class where the messages are stored that employs a _mailQueue singleton. I have another class, MailArchive, that employs the functionality of writing and reading from a file and requires a MailQueue object for the object writing methods. Of course this is not all the code but the programs are quite extensive spanning many classes so I did not feel it would be prudent to try and post it all here. I understand if that makes it more difficult to give advice. The _mailQueue object is intialized so don't think that the value stay = null. This is the last thing I came up with along with trying about anything else I could imagine and I can't get anything to push through, the logger shows that the startShutdownHook() method is getting called so the thread should be getting registered with the JVM, but I don't even know if you can do this type of thing with a jsp. If you have any questions about missing code, or any advice that could be offered would be appreciated. I have tried simpler versions of this and it works fine until I try to reference the class that the shutdown hook subclass is a part of and then everything falls apart for me. Sorry about the formatting I kinda n00bed it up before putting the code tags in, lol.

MailQueue:

privatestatic MailQueue _mailQueue =null;

publicvoid startShutdownHook(){

ArchiveShutdownHook archiveShutdownHook =new ArchiveShutdownHook();

Runtime.getRuntime().addShutdownHook(archiveShutdownHook);

//LOG.debug("SHUTDOWN HOOK has been called.");

}

publicclass ArchiveShutdownHookextends Thread{

publicvoid run(){

try{

MailArchive mailArchive =new MailArchive();

mailArchive.archiveAll(_mailQueue);

}

catch(Exception e){

}

}

}

MailArchive:

publicvoid archiveAll(MailQueue mailQueue)throws Exception{

archiveSimpleMail(mailQueue);

archiveMimeMessages(mailQueue);

}

mail.jsp

MailArchive archive = (MailArchive) appContext.getBean("mailArchive");

MailQueue mailQueue = MailQueue.getInstance(sender, archive);

mailQueue.startShutdownHook();

Message was edited by:

rcrain

Message was edited by:

rcrain

Message was edited by:

rcrain

Message was edited by:

rcrain

[3706 byte] By [rcraina] at [2007-11-26 17:09:12]
# 1

In a webapp environment you want to do this kind of saving operation when the webapp is shut down, not wait until the JVM is closing.

You do this with a Listener. In the new webapp standards you can use a ContextListener object. You define a class implementing the ContextListener interface and add a listener element to the web.xml.

The contextDestroyed() method is invoked as the web app is being shut down. Do your storage stuff there.

malcolmmca at 2007-7-8 23:37:01 > top of Java-index,Java Essentials,Java Programming...
# 2
That's a great idea, I did not know you could do that, I'm kind of new to this stuff so the help is greatly appreciated.
rcraina at 2007-7-8 23:37:01 > top of Java-index,Java Essentials,Java Programming...