001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.hook;
015
016import ch.qos.logback.core.util.Duration;
017
018/**
019 * ShutdownHook implementation that <b>stops</b> the Logback context after a
020 * specified delay. The default delay is 0 ms (zero).
021 * 
022 * <p>
023 * Stopping the logback context
024 *
025 * @author Mike Reinhold
026 */
027public class DefaultShutdownHook extends ShutdownHookBase {
028    /**
029     * The default is no delay before shutdown.
030     */
031    public static final Duration DEFAULT_DELAY = Duration.buildByMilliseconds(0);
032
033    /**
034     * The delay in milliseconds before the ShutdownHook stops the logback context
035     */
036    private Duration delay = DEFAULT_DELAY;
037
038    public DefaultShutdownHook() {
039    }
040
041    public Duration getDelay() {
042        return delay;
043    }
044
045    /**
046     * The duration to wait before shutting down the current logback context.
047     *
048     * @param delay
049     */
050    public void setDelay(Duration delay) {
051        this.delay = delay;
052    }
053
054    public void run() {
055        if (delay.getMilliseconds() > 0) {
056            addInfo("Sleeping for " + delay);
057            try {
058                Thread.sleep(delay.getMilliseconds());
059            } catch (InterruptedException e) {
060            }
061        }
062        super.stop();
063    }
064
065}