1 64 65 package com.jcorporate.expresso.services.crontab; 66 67 import com.jcorporate.expresso.core.registry.ExpressoThread; 68 import com.jcorporate.expresso.core.registry.MutableRequestRegistry; 69 import com.jcorporate.expresso.core.security.SuperUser; 70 71 72 79 public class CronDaemon 80 implements Runnable { 81 82 protected Crontab mgr; 83 protected Thread thread; 84 private long sleepUntil = -1; 85 private boolean shutdown = false; 86 87 public CronDaemon(Crontab mgr, boolean isDaemon, String waiterName) { 88 this.mgr = mgr; 89 thread = new ExpressoThread(this, waiterName); 90 thread.setDaemon(isDaemon); 91 thread.start(); 92 } 93 94 99 public synchronized void update(long sleepUntil) { 100 this.sleepUntil = sleepUntil; 101 notify(); 102 } 103 104 109 public synchronized void restart(long sleepUntil) { 110 this.sleepUntil = sleepUntil; 111 notify(); 112 } 113 114 117 public synchronized void stop() { 118 shutdown = true; 119 notify(); 120 } 121 122 public synchronized void run() { 123 new MutableRequestRegistry("default", SuperUser.SUPER_USER); 125 126 while (!shutdown) { 127 try { 128 129 if (sleepUntil <= 0) { 131 132 wait(); 134 } else { 135 136 long timeout = sleepUntil - System.currentTimeMillis(); 138 139 if (timeout > 0) { 140 wait(timeout); 141 } 142 } 143 if (sleepUntil >= 0 && 146 (sleepUntil - System.currentTimeMillis() < 1000)) { 147 148 sleepUntil = -1; 150 mgr.notifyListeners(); 151 } 152 } catch (InterruptedException e) { 153 154 } 155 } 156 } 157 } 158 | Popular Tags |