[SPR-5727] CronTriggerBean supports start delay
This commit is contained in:
parent
320f08ac79
commit
90652bf8ae
|
|
@ -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.
|
||||
* <p>The start delay is added to the current system time
|
||||
* (when the bean starts) to control the {@link #setStartTime start time}
|
||||
* of the trigger.
|
||||
* <p>If the start delay is non-zero it will <strong>always</strong>
|
||||
* 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue