How to properly kill local streams belonging to webapp, running on tomcat, which are instructed to disable

The webapp backend is deployed in the Tomcat 6 servlet container. Several web threads are launched in webapp. The problem is disconnecting.

  • How do I know if a web server is requested to disconnect?
  • How do I handle this in my threads?

My thread is currently implemented as shown below. When a servlet is instructed to shut down (shutdown.sh), it completes a clean shutdown and does not freeze due to this thread. Why?

class Updater extends Thread {
  volatile boolean interrupted = false;

  @Override
  public void run() {
    Integer lastUpdateLogId = CommonBeanFactory.getXXX()
                              .getLastUpdateLogRecordKey(MLConstants.SMART_DB_NAME);

    List<UpdateLog> updateLogRecords;
    while (!interrupted) {
      boolean isConfigurationUpdateRequested = false;

      try {
        TimeUnit.SECONDS.sleep(5);
      } catch (InterruptedException e) {
        setInterrupted(true);
      }

      updateLogRecords = CommonBeanFactory.getXXX()
                         .getLastFactsUpdateLogRecords(MLConstants.XXXX, lastUpdateLogId);

      for(UpdateLog updateLog : updateLogRecords) {
        if (updateLog.getTable_name().equals(MLConstants.CONFIG_RELOAD)) {
          isConfigurationUpdateRequested = true;
        }

        lastUpdateLogId = updateLog.getObjectKey();
      }

      if (isConfigurationUpdateRequested) {
        Configuration.getInstance().loadConfiguration();
      }
    }
  }

  public boolean getInterrupted() {
    return interrupted;
  }

  public void setInterrupted(boolean interrupted) {
    this.interrupted = interrupted;
  }
}
+5
source share
2 answers

. , . , , init(). , destroy().

destroy() .

shutdown.sh, JVM . JVM , ( ) , . System.exit(0);

+3

, . .

, , webapp ; , . *. , , - . .

Ctrl + C Tomcat ( Windows) , init script, Tomcat, . , , ...

: . ServletContextListener, "", . , , Thread.interrupt() contextDestroyed(). , destroy().

, , JVM . {sh | bat}. , script Tomcat. , .

+5

All Articles