PeriodicReloadingTrigger.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.reloading;

  18. import java.util.concurrent.Executors;
  19. import java.util.concurrent.ScheduledExecutorService;
  20. import java.util.concurrent.ScheduledFuture;
  21. import java.util.concurrent.ThreadFactory;
  22. import java.util.concurrent.TimeUnit;

  23. import org.apache.commons.lang3.concurrent.BasicThreadFactory;

  24. /**
  25.  * <p>
  26.  * A timer-based trigger for reloading checks.
  27.  * </p>
  28.  * <p>
  29.  * An instance of this class is constructed with a reference to a {@link ReloadingController} and a period. After
  30.  * calling the {@code start()} method a periodic task is started which calls
  31.  * {@link ReloadingController#checkForReloading(Object)} on the associated reloading controller. This way changes on a
  32.  * configuration source can be detected without client code having to poll actively. The {@code ReloadingController}
  33.  * will perform its checks and generates events if it detects the need for a reloading operation.
  34.  * </p>
  35.  * <p>
  36.  * Triggering of the controller can be disabled by calling the {@code stop()} method and later be resumed by calling
  37.  * {@code start()} again. When the trigger is no more needed its {@code shutdown()} method should be called.
  38.  * </p>
  39.  * <p>
  40.  * When creating an instance a {@code ScheduledExecutorService} can be provided which is then used by the object.
  41.  * Otherwise, a default executor service is created and used. When shutting down this object it can be specified whether
  42.  * the {@code ScheduledExecutorService} should be shut down, too.
  43.  * </p>
  44.  *
  45.  * @since 2.0
  46.  * @see ReloadingController
  47.  */
  48. public class PeriodicReloadingTrigger {
  49.     /**
  50.      * Creates a default executor service. This method is called if no executor has been passed to the constructor.
  51.      *
  52.      * @return the default executor service
  53.      */
  54.     private static ScheduledExecutorService createDefaultExecutorService() {
  55.         final ThreadFactory factory = new BasicThreadFactory.Builder().namingPattern("ReloadingTrigger-%s").daemon(true).build();
  56.         return Executors.newScheduledThreadPool(1, factory);
  57.     }

  58.     /** The executor service used by this trigger. */
  59.     private final ScheduledExecutorService executorService;

  60.     /** The associated reloading controller. */
  61.     private final ReloadingController controller;

  62.     /** The parameter to be passed to the controller. */
  63.     private final Object controllerParam;

  64.     /** The period. */
  65.     private final long period;

  66.     /** The time unit. */
  67.     private final TimeUnit timeUnit;

  68.     /** Stores the future object for the current trigger task. */
  69.     private ScheduledFuture<?> triggerTask;

  70.     /**
  71.      * Creates a new instance of {@code PeriodicReloadingTrigger} with a default executor service.
  72.      *
  73.      * @param ctrl the {@code ReloadingController} (must not be <strong>null</strong>)
  74.      * @param ctrlParam the optional parameter to be passed to the controller when doing reloading checks
  75.      * @param triggerPeriod the period in which the controller is triggered
  76.      * @param unit the time unit for the period
  77.      * @throws IllegalArgumentException if a required argument is missing
  78.      */
  79.     public PeriodicReloadingTrigger(final ReloadingController ctrl, final Object ctrlParam, final long triggerPeriod, final TimeUnit unit) {
  80.         this(ctrl, ctrlParam, triggerPeriod, unit, null);
  81.     }

  82.     /**
  83.      * Creates a new instance of {@code PeriodicReloadingTrigger} and sets all parameters.
  84.      *
  85.      * @param ctrl the {@code ReloadingController} (must not be <strong>null</strong>)
  86.      * @param ctrlParam the optional parameter to be passed to the controller when doing reloading checks
  87.      * @param triggerPeriod the period in which the controller is triggered
  88.      * @param unit the time unit for the period
  89.      * @param exec the executor service to use (can be <strong>null</strong>, then a default executor service is created
  90.      * @throws IllegalArgumentException if a required argument is missing
  91.      */
  92.     public PeriodicReloadingTrigger(final ReloadingController ctrl, final Object ctrlParam, final long triggerPeriod, final TimeUnit unit,
  93.         final ScheduledExecutorService exec) {
  94.         if (ctrl == null) {
  95.             throw new IllegalArgumentException("ReloadingController must not be null!");
  96.         }

  97.         controller = ctrl;
  98.         controllerParam = ctrlParam;
  99.         period = triggerPeriod;
  100.         timeUnit = unit;
  101.         executorService = exec != null ? exec : createDefaultExecutorService();
  102.     }

  103.     /**
  104.      * Creates the task which triggers the reloading controller.
  105.      *
  106.      * @return the newly created trigger task
  107.      */
  108.     private Runnable createTriggerTaskCommand() {
  109.         return () -> controller.checkForReloading(controllerParam);
  110.     }

  111.     /**
  112.      * Gets the {@code ScheduledExecutorService} used by this object.
  113.      *
  114.      * @return the associated {@code ScheduledExecutorService}
  115.      */
  116.     ScheduledExecutorService getExecutorService() {
  117.         return executorService;
  118.     }

  119.     /**
  120.      * Returns a flag whether this trigger is currently active.
  121.      *
  122.      * @return a flag whether this trigger is running
  123.      */
  124.     public synchronized boolean isRunning() {
  125.         return triggerTask != null;
  126.     }

  127.     /**
  128.      * Shuts down this trigger and its {@code ScheduledExecutorService}. This is a shortcut for {@code shutdown(true)}.
  129.      *
  130.      * @see #shutdown(boolean)
  131.      */
  132.     public void shutdown() {
  133.         shutdown(true);
  134.     }

  135.     /**
  136.      * Shuts down this trigger and optionally shuts down the {@code ScheduledExecutorService} used by this object. This
  137.      * method should be called if this trigger is no more needed. It ensures that the trigger is stopped. If the parameter
  138.      * is <strong>true</strong>, the executor service is also shut down. This should be done if this trigger is the only user of this
  139.      * executor service.
  140.      *
  141.      * @param shutdownExecutor a flag whether the associated {@code ScheduledExecutorService} is to be shut down
  142.      */
  143.     public void shutdown(final boolean shutdownExecutor) {
  144.         stop();
  145.         if (shutdownExecutor) {
  146.             getExecutorService().shutdown();
  147.         }
  148.     }

  149.     /**
  150.      * Starts this trigger. The associated {@code ReloadingController} will be triggered according to the specified period.
  151.      * The first triggering happens after a period. If this trigger is already started, this invocation has no effect.
  152.      */
  153.     public synchronized void start() {
  154.         if (!isRunning()) {
  155.             triggerTask = getExecutorService().scheduleAtFixedRate(createTriggerTaskCommand(), period, period, timeUnit);
  156.         }
  157.     }

  158.     /**
  159.      * Stops this trigger. The associated {@code ReloadingController} is no more triggered. If this trigger is already
  160.      * stopped, this invocation has no effect.
  161.      */
  162.     public synchronized void stop() {
  163.         if (isRunning()) {
  164.             triggerTask.cancel(false);
  165.             triggerTask = null;
  166.         }
  167.     }
  168. }