View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.registry;
17  
18  import java.util.logging.Level;
19  import java.util.logging.Logger;
20  
21  /**
22   * Runs periodically and calls {@link org.fourthline.cling.registry.RegistryImpl#maintain()}.
23   *
24   * @author Christian Bauer
25   */
26  public class RegistryMaintainer implements Runnable {
27  
28      private static Logger log = Logger.getLogger(RegistryMaintainer.class.getName());
29  
30      final private RegistryImpl registry;
31      final private int sleepIntervalMillis;
32  
33      private volatile boolean stopped = false;
34  
35      public RegistryMaintainer(RegistryImpl registry, int sleepIntervalMillis) {
36          this.registry = registry;
37          this.sleepIntervalMillis = sleepIntervalMillis;
38      }
39  
40      public void stop() {
41          if (log.isLoggable(Level.FINE))
42              log.fine("Setting stopped status on thread");
43          stopped = true;
44      }
45  
46      public void run() {
47          stopped = false;
48          if (log.isLoggable(Level.FINE))
49              log.fine("Running registry maintenance loop every milliseconds: " + sleepIntervalMillis);
50          while (!stopped) {
51  
52              try {
53                  registry.maintain();
54                  Thread.sleep(sleepIntervalMillis);
55              } catch (InterruptedException ex) {
56                  stopped = true;
57              }
58  
59          }
60          log.fine("Stopped status on thread received, ending maintenance loop");
61      }
62  
63  }