0

We use exchange server for getting email and open connection for 30 minutes and then automatically reopen it for next 30 minutes. But when we want to deploy new war-file and stops tomcat service, connection doesn't close while connection period doesn't end (30 minutes). So tomcat service cannot be started for this interval. I tried to handle server stopping with shutdown hook like this

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
        public void run() {
            connection.close();
        }
    }));

but it doesn't work. Is there some another way to close connection?

Connection to exchange server is in new thread. Tomcat 7.

Also we want to handle (close connection) stopping tomcat in any way.

P.S. Sorry for my English.

Update: I found that destroy method of bean executes, but connection closes in 1-2 minutes. It's still too long for websites.

Vitaliy Borisok
  • 822
  • 3
  • 11
  • 21

2 Answers2

0

If you kill java proccess shutdown hook will not work, certainly. Therefore - you cannot handle this sort of situation in some common way.

But just think about what is the reason to close the connection when you stop server ? Which resources are keeping opened when server is killed ?

We have 2 options.

  1. Resource you want close is JVM-object. When you kill JVM - that object dies. Nothing to close here.
  2. Resource you want close is something persistent. Then - why do you want to close it ? Just reuse in new deploy.
Boris
  • 1,054
  • 1
  • 13
  • 23
  • Thx for your reply. Problem that Tomcat doesn't stop and I can't deploy new war file without killing process. How can I reuse it? – Vitaliy Borisok Jan 13 '14 at 12:00
  • Oh, got it now. In this situation shutdown hook will not help you as it doesn't know that tomcat shutdown happens. Shutdown in this situation is servlet-specific entity. You can write **ServletContextListener** http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html – Boris Jan 13 '14 at 13:52
  • Thx for your answer, but now I have another problem: connection is closing too long. About 1-2 minutes. It's long break for website. – Vitaliy Borisok Jan 13 '14 at 15:53
  • Then the only option is to kill java process instead of stopping Tomcat using native way. – Boris Jan 14 '14 at 07:02
  • yep. I've added System.exit(0) in destroy method, but some hours ago I saw that it works only before first reconnect. My problem in how I use exchange service. :( – Vitaliy Borisok Jan 14 '14 at 19:34
0

If your Tomcat doesn't stop, probably there's another never ending thread that is still alive. So you should stop that thread first in the shutdown hook.

And I believe using a ServletContextListener is way better than "addShutdownHook".

Ozan Tabak
  • 662
  • 3
  • 9
  • Thx for your answer, but now I have another problem: connection is closing too long. About 1-2 minutes. It's too long break for website. – Vitaliy Borisok Jan 13 '14 at 15:53