View Javadoc
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    *     https://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  
19  import java.util.concurrent.Executors;
20  import java.util.concurrent.ScheduledExecutorService;
21  import java.util.concurrent.ScheduledFuture;
22  import java.util.concurrent.ThreadFactory;
23  import java.util.concurrent.TimeUnit;
24  
25  import org.apache.commons.lang3.concurrent.BasicThreadFactory;
26  
27  /**
28   * <p>
29   * A timer-based trigger for reloading checks.
30   * </p>
31   * <p>
32   * An instance of this class is constructed with a reference to a {@link ReloadingController} and a period. After
33   * calling the {@code start()} method a periodic task is started which calls
34   * {@link ReloadingController#checkForReloading(Object)} on the associated reloading controller. This way changes on a
35   * configuration source can be detected without client code having to poll actively. The {@code ReloadingController}
36   * will perform its checks and generates events if it detects the need for a reloading operation.
37   * </p>
38   * <p>
39   * Triggering of the controller can be disabled by calling the {@code stop()} method and later be resumed by calling
40   * {@code start()} again. When the trigger is no more needed its {@code shutdown()} method should be called.
41   * </p>
42   * <p>
43   * When creating an instance a {@code ScheduledExecutorService} can be provided which is then used by the object.
44   * Otherwise, a default executor service is created and used. When shutting down this object it can be specified whether
45   * the {@code ScheduledExecutorService} should be shut down, too.
46   * </p>
47   *
48   * @since 2.0
49   * @see ReloadingController
50   */
51  public class PeriodicReloadingTrigger {
52  
53      /**
54       * Creates a default executor service. This method is called if no executor has been passed to the constructor.
55       *
56       * @return the default executor service
57       */
58      private static ScheduledExecutorService createDefaultExecutorService() {
59          final ThreadFactory factory = BasicThreadFactory.builder().namingPattern("ReloadingTrigger-%s").daemon(true).build();
60          return Executors.newScheduledThreadPool(1, factory);
61      }
62  
63      /** The executor service used by this trigger. */
64      private final ScheduledExecutorService executorService;
65  
66      /** The associated reloading controller. */
67      private final ReloadingController controller;
68  
69      /** The parameter to be passed to the controller. */
70      private final Object controllerParam;
71  
72      /** The period. */
73      private final long period;
74  
75      /** The time unit. */
76      private final TimeUnit timeUnit;
77  
78      /** Stores the future object for the current trigger task. */
79      private ScheduledFuture<?> triggerTask;
80  
81      /**
82       * Creates a new instance of {@code PeriodicReloadingTrigger} with a default executor service.
83       *
84       * @param ctrl the {@code ReloadingController} (must not be <strong>null</strong>)
85       * @param ctrlParam the optional parameter to be passed to the controller when doing reloading checks
86       * @param triggerPeriod the period in which the controller is triggered
87       * @param unit the time unit for the period
88       * @throws IllegalArgumentException if a required argument is missing
89       */
90      public PeriodicReloadingTrigger(final ReloadingController ctrl, final Object ctrlParam, final long triggerPeriod, final TimeUnit unit) {
91          this(ctrl, ctrlParam, triggerPeriod, unit, null);
92      }
93  
94      /**
95       * Creates a new instance of {@code PeriodicReloadingTrigger} and sets all parameters.
96       *
97       * @param ctrl the {@code ReloadingController} (must not be <strong>null</strong>)
98       * @param ctrlParam the optional parameter to be passed to the controller when doing reloading checks
99       * @param triggerPeriod the period in which the controller is triggered
100      * @param unit the time unit for the period
101      * @param exec the executor service to use (can be <strong>null</strong>, then a default executor service is created
102      * @throws IllegalArgumentException if a required argument is missing
103      */
104     public PeriodicReloadingTrigger(final ReloadingController ctrl, final Object ctrlParam, final long triggerPeriod, final TimeUnit unit,
105         final ScheduledExecutorService exec) {
106         if (ctrl == null) {
107             throw new IllegalArgumentException("ReloadingController must not be null.");
108         }
109 
110         controller = ctrl;
111         controllerParam = ctrlParam;
112         period = triggerPeriod;
113         timeUnit = unit;
114         executorService = exec != null ? exec : createDefaultExecutorService();
115     }
116 
117     /**
118      * Creates the task which triggers the reloading controller.
119      *
120      * @return the newly created trigger task
121      */
122     private Runnable createTriggerTaskCommand() {
123         return () -> controller.checkForReloading(controllerParam);
124     }
125 
126     /**
127      * Gets the {@code ScheduledExecutorService} used by this object.
128      *
129      * @return the associated {@code ScheduledExecutorService}
130      */
131     ScheduledExecutorService getExecutorService() {
132         return executorService;
133     }
134 
135     /**
136      * Returns a flag whether this trigger is currently active.
137      *
138      * @return a flag whether this trigger is running
139      */
140     public synchronized boolean isRunning() {
141         return triggerTask != null;
142     }
143 
144     /**
145      * Shuts down this trigger and its {@code ScheduledExecutorService}. This is a shortcut for {@code shutdown(true)}.
146      *
147      * @see #shutdown(boolean)
148      */
149     public void shutdown() {
150         shutdown(true);
151     }
152 
153     /**
154      * Shuts down this trigger and optionally shuts down the {@code ScheduledExecutorService} used by this object. This
155      * method should be called if this trigger is no more needed. It ensures that the trigger is stopped. If the parameter
156      * is <strong>true</strong>, the executor service is also shut down. This should be done if this trigger is the only user of this
157      * executor service.
158      *
159      * @param shutdownExecutor a flag whether the associated {@code ScheduledExecutorService} is to be shut down
160      */
161     public void shutdown(final boolean shutdownExecutor) {
162         stop();
163         if (shutdownExecutor) {
164             getExecutorService().shutdown();
165         }
166     }
167 
168     /**
169      * Starts this trigger. The associated {@code ReloadingController} will be triggered according to the specified period.
170      * The first triggering happens after a period. If this trigger is already started, this invocation has no effect.
171      */
172     public synchronized void start() {
173         if (!isRunning()) {
174             triggerTask = getExecutorService().scheduleAtFixedRate(createTriggerTaskCommand(), period, period, timeUnit);
175         }
176     }
177 
178     /**
179      * Stops this trigger. The associated {@code ReloadingController} is no more triggered. If this trigger is already
180      * stopped, this invocation has no effect.
181      */
182     public synchronized void stop() {
183         if (isRunning()) {
184             triggerTask.cancel(false);
185             triggerTask = null;
186         }
187     }
188 }