summaryrefslogtreecommitdiff
path: root/java/broker-plugins/experimental/shutdown/src/main/java/org/apache/qpid/shutdown/Shutdown.java
blob: cef4a42b64cc7398e3634c8fb2dbdc18820d0c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.qpid.shutdown;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.management.NotCompliantMBeanException;

import org.apache.log4j.Logger;
import org.apache.qpid.server.management.DefaultManagedObject;

/**
 * Implementation of the JMX broker shutdown plugin.
 */
public class Shutdown extends DefaultManagedObject implements ShutdownMBean
{

    private static final Logger _logger = Logger.getLogger(Shutdown.class);

    private static final String FORMAT = "yyyy/MM/dd HH:mm:ss";
    private static final int THREAD_COUNT = 1;
    private static final ScheduledExecutorService EXECUTOR = new ScheduledThreadPoolExecutor(THREAD_COUNT);

    private final Runnable _shutdown = new SystemExiter();

    public Shutdown() throws NotCompliantMBeanException
    {
        super(ShutdownMBean.class, ShutdownMBean.TYPE);
    }

    /** @see ShutdownMBean#shutdown() */
    public void shutdown()
    {
        _logger.info("Shutting down at user's request");
        shutdownBroker(0);
    }

    /** @see ShutdownMBean#shutdown(long) */
    public void shutdown(final long delay)
    {
        if (delay < 0)
        {
            _logger.info("Shutting down at user's request");
            shutdownBroker(0);
        }
        else
        {
            _logger.info("Scheduled broker shutdown after " + delay + "ms");
            shutdownBroker(delay);
        }
    }

    /** @see ShutdownMBean#shutdownAt(String) */
    public void shutdownAt(final String when)
    {
        Date date;
        DateFormat df = new SimpleDateFormat(FORMAT);
        try
        {
            date = df.parse(when);
        }
        catch (ParseException e)
        {
            _logger.error("Invalid date \"" + when + "\": expecting " + FORMAT, e);
            return;
        }
        _logger.info("Scheduled broker shutdown at " + when);
        long now = System.currentTimeMillis();
        long time = date.getTime();
        if (time > now)
        {
            shutdownBroker(time - now);
        }
        else
        {
            shutdownBroker(0);
        }
    }

    /**
     * Submits the {@link SystemExiter} job to shutdown the broker.
     */
    private void shutdownBroker(long delay)
    {
        EXECUTOR.schedule(_shutdown, delay, TimeUnit.MILLISECONDS);
    }

    /**
     * Shutting down the system in another thread to avoid JMX exceptions being thrown.
     */
    class SystemExiter implements Runnable
    {
        public void run()
        {
            System.exit(0);
        }
    }

    /**
     * @see org.apache.qpid.server.management.ManagedObject#getObjectInstanceName()
     */
    @Override
    public String getObjectInstanceName()
    {
        return "Shutdown";
    }
}