void start() { if (enable) { scheduler.start(); } }
void schedule() { if (enable) { scheduler = new Scheduler(); if (task instanceof Runnable) { scheduler.schedule(cron, (Runnable) task); } else if (task instanceof Task) { scheduler.schedule(cron, (Task) task); } else { scheduler = null; throw new IllegalStateException("Task 必须是 Runnable、ITask、ProcessTask 或者 Task 类型"); } scheduler.setDaemon(daemon); } }
void stop() { if (enable) { if (task instanceof ITask) { // 如果任务实现了 ITask 接口,则回调 ITask.stop() 方法 ((ITask)task).stop(); } scheduler.stop(); } } }
@Override public void init() { scheduler.addTaskCollector(this); scheduler.start(); }
@Override public void start(boolean daemon) { setupLinkedLockIfNeeds(); synchronized (linkedLock) { registerStartTimeCurrentTime(); setupLinkedGuidIfNeeds(); final String threadName = buildThreadName(linkedScheduler.getGuid(), linkedGuid); registerThreadNewCreated(); prepareThread(daemon, threadName); actuallyThreadStart(); } }
private CRaSHTaskDef validateAndParseCronLine(String cronLine) { // cronLine = cronLine.trim(); if (cronLine.length() == 0 || cronLine.charAt(0) == '#') { return null; } // List<String> cronLineParts = Arrays.asList(cronLine.split("\\s+")); if (cronLineParts.size() < 6) { return null; } // String schedulePart = Utils.join(cronLineParts.subList(0, 5), " "); String commandPart = Utils.join(cronLineParts.subList(5, cronLineParts.size()), " "); try { return new CRaSHTaskDef(new SchedulingPattern(schedulePart), commandPart); } catch (InvalidPatternException e) { // ? return null; } } }
protected void setupLinkedExecutorsIfNeeds() { readyExecutorsFieldIfNeeds(); if (linkedExecutors == null) { synchronized (attributeLinkLock) { if (linkedExecutors == null) { linkedExecutors = getFieldValue(executorsField); } } } }
protected void setupLinkedGuidIfNeeds() { readyGuidFieldIfNeeds(); if (linkedGuid == null) { synchronized (attributeLinkLock) { if (linkedGuid == null) { linkedGuid = getFieldValue(guidField); } } } }
protected void readyGuidFieldIfNeeds() { if (guidField == null) { synchronized (reflectionPartyLock) { if (guidField == null) { guidField = getAccessibleField("guid"); } } } }
public void testCron() throws Exception { Support support = new Support("* * * * * foobar toto"); support.lifecycle.bindGroovy("foobar", "" + "public class foobar {\n" + "@Command\n" + "public void main(@Argument String arg) {\n" + CrontabTestCase.class.getName() + ".countDown(arg);\n" + "}\n" + "}\n" ); CronPlugin plugin = support.lifecycle.getContext().getPlugin(CronPlugin.class); assertNotNull(plugin); assertTrue(plugin.spawn()); while (latch.getCount() == 2) { // Do nothing } Logger.getLogger(CrontabTestCase.class.getName()).log(Level.FINE, "Checking cron history"); assertEquals(1, plugin.getHistory().size()); CRaSHTaskProcess process = plugin.getHistory().peek(); assertEquals("foobar toto", process.getLine()); assertEquals("* * * * *", process.getSchedulingPattern().toString()); assertTrue(plugin.spawn()); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals(Arrays.asList("toto", "toto"), queue); assertEquals(2, plugin.getHistory().size()); } }
protected void readyExecutorsFieldIfNeeds() { if (executorsField == null) { synchronized (reflectionPartyLock) { if (executorsField == null) { executorsField = getAccessibleField("executors"); } } } }
@Override public void destroy() { scheduler.stop(); }
void start() { if (enable) { scheduler.start(); } }
protected void readyContextFieldIfNeeds() { if (contextField == null) { synchronized (reflectionPartyLock) { if (contextField == null) { contextField = getAccessibleField("context"); } } } }
private void _disposeScheduler(Scheduler scheduler) { if (scheduler != null) { scheduler.stop(); } }
protected void readyStoppedFieldIfNeeds() { if (stoppedField == null) { synchronized (reflectionPartyLock) { if (stoppedField == null) { stoppedField = getAccessibleField("stopped"); } } } }
/** * 停止 */ synchronized public static void stop() { scheduler.stop(); }
protected void readyStartTimeFieldIfNeeds() { if (startTimeField == null) { synchronized (reflectionPartyLock) { if (startTimeField == null) { startTimeField = getAccessibleField("startTime"); } } } }
@Override public void shutdown() { if (scheduler != null) { scheduler.stop(); scheduler = null; } } }