    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.reloading;
    018
    019import java.util.concurrent.Executors;
    020import java.util.concurrent.ScheduledExecutorService;
    021import java.util.concurrent.ScheduledFuture;
    022import java.util.concurrent.ThreadFactory;
    023import java.util.concurrent.TimeUnit;
    024
    025import org.apache.commons.lang3.concurrent.BasicThreadFactory;
    026
    027/**
    028 * <p>
    029 * A timer-based trigger for reloading checks.
    030 * </p>
    031 * <p>
    032 * An instance of this class is constructed with a reference to a {@link ReloadingController} and a period. After
    033 * calling the {@code start()} method a periodic task is started which calls
    034 * {@link ReloadingController#checkForReloading(Object)} on the associated reloading controller. This way changes on a
    035 * configuration source can be detected without client code having to poll actively. The {@code ReloadingController}
    036 * will perform its checks and generates events if it detects the need for a reloading operation.
    037 * </p>
    038 * <p>
    039 * Triggering of the controller can be disabled by calling the {@code stop()} method and later be resumed by calling
    040 * {@code start()} again. When the trigger is no more needed its {@code shutdown()} method should be called.
    041 * </p>
    042 * <p>
    043 * When creating an instance a {@code ScheduledExecutorService} can be provided which is then used by the object.
    044 * Otherwise, a default executor service is created and used. When shutting down this object it can be specified whether
    045 * the {@code ScheduledExecutorService} should be shut down, too.
    046 * </p>
    047 *
    048 * @since 2.0
    049 * @see ReloadingController
    050 */
    051public class PeriodicReloadingTrigger {
    052    /**
    053     * Creates a default executor service. This method is called if no executor has been passed to the constructor.
    054     *
    055     * @return the default executor service
    056     */
    057    private static ScheduledExecutorService createDefaultExecutorService() {
    058        final ThreadFactory factory = new BasicThreadFactory.Builder().namingPattern("ReloadingTrigger-%s").daemon(true).build();
    059        return Executors.newScheduledThreadPool(1, factory);
    060    }
    061
    062    /** The executor service used by this trigger. */
    063    private final ScheduledExecutorService executorService;
    064
    065    /** The associated reloading controller. */
    066    private final ReloadingController controller;
    067
    068    /** The parameter to be passed to the controller. */
    069    private final Object controllerParam;
    070
    071    /** The period. */
    072    private final long period;
    073
    074    /** The time unit. */
    075    private final TimeUnit timeUnit;
    076
    077    /** Stores the future object for the current trigger task. */
    078    private ScheduledFuture<?> triggerTask;
    079
    080    /**
    081     * Creates a new instance of {@code PeriodicReloadingTrigger} with a default executor service.
    082     *
    083     * @param ctrl the {@code ReloadingController} (must not be <strong>null</strong>)
    084     * @param ctrlParam the optional parameter to be passed to the controller when doing reloading checks
    085     * @param triggerPeriod the period in which the controller is triggered
    086     * @param unit the time unit for the period
    087     * @throws IllegalArgumentException if a required argument is missing
    088     */
    089    public PeriodicReloadingTrigger(final ReloadingController ctrl, final Object ctrlParam, final long triggerPeriod, final TimeUnit unit) {
    090        this(ctrl, ctrlParam, triggerPeriod, unit, null);
    091    }
    092
    093    /**
    094     * Creates a new instance of {@code PeriodicReloadingTrigger} and sets all parameters.
    095     *
    096     * @param ctrl the {@code ReloadingController} (must not be <strong>null</strong>)
    097     * @param ctrlParam the optional parameter to be passed to the controller when doing reloading checks
    098     * @param triggerPeriod the period in which the controller is triggered
    099     * @param unit the time unit for the period
    100     * @param exec the executor service to use (can be <strong>null</strong>, then a default executor service is created
    101     * @throws IllegalArgumentException if a required argument is missing
    102     */
    103    public PeriodicReloadingTrigger(final ReloadingController ctrl, final Object ctrlParam, final long triggerPeriod, final TimeUnit unit,
    104        final ScheduledExecutorService exec) {
    105        if (ctrl == null) {
    106            throw new IllegalArgumentException("ReloadingController must not be null!");
    107        }
    108
    109        controller = ctrl;
    110        controllerParam = ctrlParam;
    111        period = triggerPeriod;
    112        timeUnit = unit;
    113        executorService = exec != null ? exec : createDefaultExecutorService();
    114    }
    115
    116    /**
    117     * Creates the task which triggers the reloading controller.
    118     *
    119     * @return the newly created trigger task
    120     */
    121    private Runnable createTriggerTaskCommand() {
    122        return () -> controller.checkForReloading(controllerParam);
    123    }
    124
    125    /**
    126     * Gets the {@code ScheduledExecutorService} used by this object.
    127     *
    128     * @return the associated {@code ScheduledExecutorService}
    129     */
    130    ScheduledExecutorService getExecutorService() {
    131        return executorService;
    132    }
    133
    134    /**
    135     * Returns a flag whether this trigger is currently active.
    136     *
    137     * @return a flag whether this trigger is running
    138     */
    139    public synchronized boolean isRunning() {
    140        return triggerTask != null;
    141    }
    142
    143    /**
    144     * Shuts down this trigger and its {@code ScheduledExecutorService}. This is a shortcut for {@code shutdown(true)}.
    145     *
    146     * @see #shutdown(boolean)
    147     */
    148    public void shutdown() {
    149        shutdown(true);
    150    }
    151
    152    /**
    153     * Shuts down this trigger and optionally shuts down the {@code ScheduledExecutorService} used by this object. This
    154     * method should be called if this trigger is no more needed. It ensures that the trigger is stopped. If the parameter
    155     * is <strong>true</strong>, the executor service is also shut down. This should be done if this trigger is the only user of this
    156     * executor service.
    157     *
    158     * @param shutdownExecutor a flag whether the associated {@code ScheduledExecutorService} is to be shut down
    159     */
    160    public void shutdown(final boolean shutdownExecutor) {
    161        stop();
    162        if (shutdownExecutor) {
    163            getExecutorService().shutdown();
    164        }
    165    }
    166
    167    /**
    168     * Starts this trigger. The associated {@code ReloadingController} will be triggered according to the specified period.
    169     * The first triggering happens after a period. If this trigger is already started, this invocation has no effect.
    170     */
    171    public synchronized void start() {
    172        if (!isRunning()) {
    173            triggerTask = getExecutorService().scheduleAtFixedRate(createTriggerTaskCommand(), period, period, timeUnit);
    174        }
    175    }
    176
    177    /**
    178     * Stops this trigger. The associated {@code ReloadingController} is no more triggered. If this trigger is already
    179     * stopped, this invocation has no effect.
    180     */
    181    public synchronized void stop() {
    182        if (isRunning()) {
    183            triggerTask.cancel(false);
    184            triggerTask = null;
    185        }
    186    }
    187}