Coverage Report - net.admin4j.util.Daemon
 
Classes in this File Line Coverage Branch Coverage Complexity
Daemon
89%
26/29
62%
5/8
2
DaemonShutdownHook
100%
3/3
N/A
2
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.util;
 15  
 
 16  
 import java.util.concurrent.atomic.AtomicBoolean;
 17  
 import java.util.concurrent.atomic.AtomicLong;
 18  
 
 19  
 import net.admin4j.deps.commons.lang3.Validate;
 20  
 
 21  
 import org.slf4j.Logger;
 22  
 import org.slf4j.LoggerFactory;
 23  
 
 24  
 /**
 25  
  * Generic Daemon worker thread that iterates through a work/sleep cycle forever.
 26  
  * @author D. Ashmore
 27  
  *
 28  
  */
 29  
 public class Daemon extends Thread {
 30  
     
 31  57
     private static final AtomicBoolean jvmInShutdown = new AtomicBoolean(false);
 32  
     static {
 33  57
         Runtime.getRuntime().addShutdownHook(new DaemonShutdownHook());
 34  57
     }
 35  
     
 36  
     private Runnable task;
 37  
     
 38  
     static final long DEFAULT_SLEEP_TIME_IN_MILLIS = 600000;   // 10 minutes
 39  78
     private final AtomicLong sleepTime = new AtomicLong(DEFAULT_SLEEP_TIME_IN_MILLIS);
 40  
     
 41  78
     protected Logger logger = LoggerFactory.getLogger(this.getClass());
 42  
     
 43  
     public Daemon(Runnable task, String threadName) {        
 44  12
         this(task, threadName, (Long)null);
 45  12
     }
 46  
     
 47  
     /**
 48  
      * @Deprecated Use Long constructor instead
 49  
      * @param task
 50  
      * @param threadName
 51  
      * @param sleepTimeInMillis
 52  
      */
 53  
     public Daemon(Runnable task, String threadName, Integer sleepTimeInMillis) {
 54  48
         this(task, threadName, sleepTimeInMillis == null ? (Long)null: sleepTimeInMillis.longValue());
 55  48
     }
 56  
     
 57  78
     public Daemon(Runnable task, String threadName, Long sleepTimeInMillis) {        
 58  
         Validate.notNull(task, "Null task not allowed.");
 59  
         Validate.notEmpty(threadName, "Null or blank threadName not allowed.");
 60  78
         this.setDaemon(true);
 61  78
         this.setName(threadName);
 62  
         
 63  78
         if (sleepTimeInMillis != null && sleepTimeInMillis > 0) {
 64  66
             this.sleepTime.set(sleepTimeInMillis);
 65  
         }
 66  
         
 67  78
         this.task = task;
 68  78
         this.start();
 69  78
     }
 70  
 
 71  
     /* (non-Javadoc)
 72  
      * @see java.lang.Thread#run()
 73  
      */
 74  
     @Override
 75  
     public void run() {
 76  
         try
 77  
         {
 78  
             while (true)
 79  
             {
 80  285
                 Thread.sleep(sleepTime.get());
 81  207
                 try {this.task.run();}
 82  0
                 catch (Throwable t)
 83  
                 {
 84  0
                     logger.error(this.getName() + " errored out.", t);
 85  207
                 }
 86  
             }
 87  
         }
 88  3
         catch (Throwable t)
 89  
         {
 90  3
             if ( !getJvminshutdown().get() ) {
 91  3
                 logger.error(this.getName() + " restarted.", t);
 92  3
                 this.start();
 93  
             }
 94  
         }
 95  0
     }
 96  
 
 97  
     public Runnable getTask() {
 98  3
         return task;
 99  
     }
 100  
 
 101  
     static AtomicBoolean getJvminshutdown() {
 102  60
         return jvmInShutdown;
 103  
     }
 104  
 
 105  
 }
 106  
 
 107  57
 class DaemonShutdownHook extends Thread {
 108  
 
 109  
     @Override
 110  
     public void run() {
 111  57
         Daemon.getJvminshutdown().set(true);
 112  57
     }
 113  
     
 114  
 }