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    *     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  
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       * Creates a default executor service. This method is called if no executor has been passed to the constructor.
54       *
55       * @return the default executor service
56       */
57      private static ScheduledExecutorService createDefaultExecutorService() {
58          final ThreadFactory factory = new BasicThreadFactory.Builder().namingPattern("ReloadingTrigger-%s").daemon(true).build();
59          return Executors.newScheduledThreadPool(1, factory);
60      }
61  
62      /** The executor service used by this trigger. */
63      private final ScheduledExecutorService executorService;
64  
65      /** The associated reloading controller. */
66      private final ReloadingController controller;
67  
68      /** The parameter to be passed to the controller. */
69      private final Object controllerParam;
70  
71      /** The period. */
72      private final long period;
73  
74      /** The time unit. */
75      private final TimeUnit timeUnit;
76  
77      /** Stores the future object for the current trigger task. */
78      private ScheduledFuture<?> triggerTask;
79  
80      /**
81       * Creates a new instance of {@code PeriodicReloadingTrigger} with a default executor service.
82       *
83       * @param ctrl the {@code ReloadingController} (must not be <strong>null</strong>)
84       * @param ctrlParam the optional parameter to be passed to the controller when doing reloading checks
85       * @param triggerPeriod the period in which the controller is triggered
86       * @param unit the time unit for the period
87       * @throws IllegalArgumentException if a required argument is missing
88       */
89      public PeriodicReloadingTrigger(final ReloadingController ctrl, final Object ctrlParam, final long triggerPeriod, final TimeUnit unit) {
90          this(ctrl, ctrlParam, triggerPeriod, unit, null);
91      }
92  
93      /**
94       * Creates a new instance of {@code PeriodicReloadingTrigger} and sets all parameters.
95       *
96       * @param ctrl the {@code ReloadingController} (must not be <strong>null</strong>)
97       * @param ctrlParam the optional parameter to be passed to the controller when doing reloading checks
98       * @param triggerPeriod the period in which the controller is triggered
99       * @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 }