Skip to content

Commit 90652bf

Browse files
author
Rob Harrop
committed
[SPR-5727] CronTriggerBean supports start delay
1 parent 320f08a commit 90652bf

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

org.springframework.context.support/src/main/java/org/springframework/scheduling/quartz/CronTriggerBean.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.beans.factory.BeanNameAware;
2828
import org.springframework.beans.factory.InitializingBean;
2929
import org.springframework.core.Constants;
30+
import org.springframework.util.Assert;
3031

3132
/**
3233
* Convenience subclass of Quartz's {@link org.quartz.CronTrigger} class,
@@ -70,6 +71,7 @@ public class CronTriggerBean extends CronTrigger
7071

7172
private String beanName;
7273

74+
private long startDelay;
7375

7476
/**
7577
* Register objects in the JobDataMap via a given Map.
@@ -121,6 +123,20 @@ public void setJobDetail(JobDetail jobDetail) {
121123
this.jobDetail = jobDetail;
122124
}
123125

126+
/**
127+
* Set the start delay in milliseconds.
128+
* <p>The start delay is added to the current system time
129+
* (when the bean starts) to control the {@link #setStartTime start time}
130+
* of the trigger.
131+
* <p>If the start delay is non-zero it will <strong>always</strong>
132+
* take precedence over start time.
133+
* @param startDelay the start delay, in milliseconds.
134+
*/
135+
public void setStartDelay(long startDelay) {
136+
Assert.state(startDelay >= 0, "Start delay cannot be negative.");
137+
this.startDelay = startDelay;
138+
}
139+
124140
public JobDetail getJobDetail() {
125141
return this.jobDetail;
126142
}
@@ -131,6 +147,10 @@ public void setBeanName(String beanName) {
131147

132148

133149
public void afterPropertiesSet() throws Exception {
150+
if(this.startDelay > 0) {
151+
setStartTime(new Date(System.currentTimeMillis() + this.startDelay));
152+
}
153+
134154
if (getName() == null) {
135155
setName(this.beanName);
136156
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scheduling.quartz;
18+
19+
import java.util.Date;
20+
import java.util.Calendar;
21+
22+
import org.junit.Test;
23+
import static org.junit.Assert.assertEquals;
24+
25+
/** @author Rob Harrop */
26+
public class CronTriggerBeanTests {
27+
28+
@Test(expected = IllegalStateException.class)
29+
public void testInvalidStartDelay() {
30+
createTriggerBean().setStartDelay(-1);
31+
}
32+
33+
@Test
34+
public void testStartTime() throws Exception {
35+
CronTriggerBean bean = createTriggerBean();
36+
Date startTime = new Date(System.currentTimeMillis() + 1234L);
37+
bean.setStartTime(startTime);
38+
bean.afterPropertiesSet();
39+
assertTimeEquals(startTime, bean.getStartTime());
40+
}
41+
42+
@Test
43+
public void testStartDelay() throws Exception {
44+
CronTriggerBean bean = createTriggerBean();
45+
long startDelay = 1234L;
46+
Date startTime = new Date(System.currentTimeMillis() + startDelay);
47+
bean.setStartDelay(startDelay);
48+
bean.afterPropertiesSet();
49+
assertTimeEquals(startTime, bean.getStartTime());
50+
}
51+
52+
@Test
53+
public void testStartDelayOverridesStartTime() throws Exception {
54+
CronTriggerBean bean = createTriggerBean();
55+
long startDelay = 1234L;
56+
Date startTime = new Date(System.currentTimeMillis() + startDelay);
57+
bean.setStartTime(new Date(System.currentTimeMillis() + 123456789L));
58+
bean.setStartDelay(startDelay);
59+
bean.afterPropertiesSet();
60+
assertTimeEquals(startTime, bean.getStartTime());
61+
}
62+
63+
private CronTriggerBean createTriggerBean() {
64+
CronTriggerBean triggerBean = new CronTriggerBean();
65+
triggerBean.setName("test");
66+
return triggerBean;
67+
}
68+
69+
private void assertTimeEquals(Date a, Date b) {
70+
Calendar ca = Calendar.getInstance();
71+
ca.setTime(a);
72+
ca.set(Calendar.MILLISECOND, 0);
73+
Calendar cb = Calendar.getInstance();
74+
cb.setTime(b);
75+
cb.set(Calendar.MILLISECOND, 0);
76+
assertEquals(ca, cb);
77+
}
78+
79+
}

0 commit comments

Comments
 (0)