View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.hook;
15  
16  import ch.qos.logback.core.util.Duration;
17  
18  /**
19   * ShutdownHook implementation that <b>stops</b> the Logback context after a specified
20   * delay.  The default delay is 0 ms (zero).
21   * 
22   * <p>Stopping the logback context
23   *
24   * @author Mike Reinhold
25   */
26  public class DefaultShutdownHook extends ShutdownHookBase {
27      /**
28       * The default is no delay before shutdown.
29       */
30      public static final Duration DEFAULT_DELAY = Duration.buildByMilliseconds(0);
31  
32      /**
33       * The delay in milliseconds before the ShutdownHook stops the logback context
34       */
35      private Duration delay = DEFAULT_DELAY;
36  
37      public DefaultShutdownHook() {
38      }
39  
40      public Duration getDelay() {
41          return delay;
42      }
43  
44      /**
45       * The duration to wait before shutting down the current logback context.
46       *
47       * @param delay
48       */
49      public void setDelay(Duration delay) {
50          this.delay = delay;
51      }
52  
53      public void run() {
54          if (delay.getMilliseconds() > 0) {
55              addInfo("Sleeping for " + delay);
56              try {
57                  Thread.sleep(delay.getMilliseconds());
58              } catch (InterruptedException e) {
59              }
60          }
61          super.stop();
62      }
63  }