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
20   * specified delay. The default delay is 0 ms (zero).
21   * 
22   * <p>
23   * Stopping the logback context
24   *
25   * @author Mike Reinhold
26   */
27  public class DefaultShutdownHook extends ShutdownHookBase {
28      /**
29       * The default is no delay before shutdown.
30       */
31      public static final Duration DEFAULT_DELAY = Duration.buildByMilliseconds(0);
32  
33      /**
34       * The delay in milliseconds before the ShutdownHook stops the logback context
35       */
36      private Duration delay = DEFAULT_DELAY;
37  
38      public DefaultShutdownHook() {
39      }
40  
41      public Duration getDelay() {
42          return delay;
43      }
44  
45      /**
46       * The duration to wait before shutting down the current logback context.
47       *
48       * @param delay
49       */
50      public void setDelay(Duration delay) {
51          this.delay = delay;
52      }
53  
54      public void run() {
55          if (delay.getMilliseconds() > 0) {
56              addInfo("Sleeping for " + delay);
57              try {
58                  Thread.sleep(delay.getMilliseconds());
59              } catch (InterruptedException e) {
60              }
61          }
62          super.stop();
63      }
64  }