From 90652bf8ae0ec68a4756af5e0796fbce61cfed18 Mon Sep 17 00:00:00 2001 From: Rob Harrop Date: Mon, 14 Sep 2009 15:53:05 +0000 Subject: [PATCH] [SPR-5727] CronTriggerBean supports start delay --- .../scheduling/quartz/CronTriggerBean.java | 20 +++++ .../quartz/CronTriggerBeanTests.java | 79 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 org.springframework.context.support/src/test/java/org/springframework/scheduling/quartz/CronTriggerBeanTests.java diff --git a/org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/CronTriggerBean.java b/org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/CronTriggerBean.java index e44931f1eb..ee99f4fc80 100644 --- a/org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/CronTriggerBean.java +++ b/org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/CronTriggerBean.java @@ -27,6 +27,7 @@ import org.quartz.Scheduler; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.core.Constants; +import org.springframework.util.Assert; /** * Convenience subclass of Quartz's {@link org.quartz.CronTrigger} class, @@ -70,6 +71,7 @@ public class CronTriggerBean extends CronTrigger private String beanName; + private long startDelay; /** * Register objects in the JobDataMap via a given Map. @@ -121,6 +123,20 @@ public class CronTriggerBean extends CronTrigger this.jobDetail = jobDetail; } + /** + * Set the start delay in milliseconds. + *

The start delay is added to the current system time + * (when the bean starts) to control the {@link #setStartTime start time} + * of the trigger. + *

If the start delay is non-zero it will always + * take precedence over start time. + * @param startDelay the start delay, in milliseconds. + */ + public void setStartDelay(long startDelay) { + Assert.state(startDelay >= 0, "Start delay cannot be negative."); + this.startDelay = startDelay; + } + public JobDetail getJobDetail() { return this.jobDetail; } @@ -131,6 +147,10 @@ public class CronTriggerBean extends CronTrigger public void afterPropertiesSet() throws Exception { + if(this.startDelay > 0) { + setStartTime(new Date(System.currentTimeMillis() + this.startDelay)); + } + if (getName() == null) { setName(this.beanName); } diff --git a/org.springframework.context.support/src/test/java/org/springframework/scheduling/quartz/CronTriggerBeanTests.java b/org.springframework.context.support/src/test/java/org/springframework/scheduling/quartz/CronTriggerBeanTests.java new file mode 100644 index 0000000000..fcd1a2f7c5 --- /dev/null +++ b/org.springframework.context.support/src/test/java/org/springframework/scheduling/quartz/CronTriggerBeanTests.java @@ -0,0 +1,79 @@ +/* + * Copyright 2002-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.scheduling.quartz; + +import java.util.Date; +import java.util.Calendar; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +/** @author Rob Harrop */ +public class CronTriggerBeanTests { + + @Test(expected = IllegalStateException.class) + public void testInvalidStartDelay() { + createTriggerBean().setStartDelay(-1); + } + + @Test + public void testStartTime() throws Exception { + CronTriggerBean bean = createTriggerBean(); + Date startTime = new Date(System.currentTimeMillis() + 1234L); + bean.setStartTime(startTime); + bean.afterPropertiesSet(); + assertTimeEquals(startTime, bean.getStartTime()); + } + + @Test + public void testStartDelay() throws Exception { + CronTriggerBean bean = createTriggerBean(); + long startDelay = 1234L; + Date startTime = new Date(System.currentTimeMillis() + startDelay); + bean.setStartDelay(startDelay); + bean.afterPropertiesSet(); + assertTimeEquals(startTime, bean.getStartTime()); + } + + @Test + public void testStartDelayOverridesStartTime() throws Exception { + CronTriggerBean bean = createTriggerBean(); + long startDelay = 1234L; + Date startTime = new Date(System.currentTimeMillis() + startDelay); + bean.setStartTime(new Date(System.currentTimeMillis() + 123456789L)); + bean.setStartDelay(startDelay); + bean.afterPropertiesSet(); + assertTimeEquals(startTime, bean.getStartTime()); + } + + private CronTriggerBean createTriggerBean() { + CronTriggerBean triggerBean = new CronTriggerBean(); + triggerBean.setName("test"); + return triggerBean; + } + + private void assertTimeEquals(Date a, Date b) { + Calendar ca = Calendar.getInstance(); + ca.setTime(a); + ca.set(Calendar.MILLISECOND, 0); + Calendar cb = Calendar.getInstance(); + cb.setTime(b); + cb.set(Calendar.MILLISECOND, 0); + assertEquals(ca, cb); + } + +} -- GitLab