Skip to content

Commit ab6c6b1

Browse files
committed
Consistent support for setStartTime in CronTrigger(Factory)Bean and SimpleTrigger(Factory)Bean, and consistent declaration of varargs in scheduling.quartz package
Issue: SPR-10940 (cherry picked from commit b228a06)
1 parent c2459b4 commit ab6c6b1

8 files changed

+70
-53
lines changed

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public class CronTriggerBean extends CronTrigger
7373

7474
private String beanName;
7575

76-
private long startDelay;
76+
private long startDelay = 0;
7777

7878

7979
/**
@@ -108,9 +108,9 @@ public void setMisfireInstructionName(String constantName) {
108108
* @see SchedulerFactoryBean#setTriggerListeners
109109
* @see org.quartz.TriggerListener#getName
110110
*/
111-
public void setTriggerListenerNames(String[] names) {
112-
for (int i = 0; i < names.length; i++) {
113-
addTriggerListener(names[i]);
111+
public void setTriggerListenerNames(String... names) {
112+
for (String name : names) {
113+
addTriggerListener(name);
114114
}
115115
}
116116

@@ -148,19 +148,19 @@ public void setBeanName(String beanName) {
148148
}
149149

150150

151+
/**
152+
* Note that this method's declaration of an Exception is deprecated
153+
* and will be removed in the Spring 4.0 line.
154+
*/
151155
public void afterPropertiesSet() throws Exception {
152-
if (this.startDelay > 0) {
153-
setStartTime(new Date(System.currentTimeMillis() + this.startDelay));
154-
}
155-
156156
if (getName() == null) {
157157
setName(this.beanName);
158158
}
159159
if (getGroup() == null) {
160160
setGroup(Scheduler.DEFAULT_GROUP);
161161
}
162-
if (getStartTime() == null) {
163-
setStartTime(new Date());
162+
if (this.startDelay > 0 || getStartTime() == null) {
163+
setStartTime(new Date(System.currentTimeMillis() + this.startDelay));
164164
}
165165
if (getTimeZone() == null) {
166166
setTimeZone(TimeZone.getDefault());

spring-context-support/src/main/java/org/springframework/scheduling/quartz/CronTriggerFactoryBean.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class CronTriggerFactoryBean implements FactoryBean<CronTrigger>, BeanNam
7878

7979
private Date startTime;
8080

81-
private long startDelay;
81+
private long startDelay = 0;
8282

8383
private String cronExpression;
8484

@@ -141,6 +141,15 @@ public void setJobDataAsMap(Map<String, ?> jobDataAsMap) {
141141
this.jobDataMap.putAll(jobDataAsMap);
142142
}
143143

144+
/**
145+
* Set a specific start time for the trigger.
146+
* <p>Note that a dynamically computed {@link #setStartDelay} specification
147+
* overrides a static timestamp set here.
148+
*/
149+
public void setStartTime(Date startTime) {
150+
this.startTime = startTime;
151+
}
152+
144153
/**
145154
* Set the start delay in milliseconds.
146155
* <p>The start delay is added to the current system time (when the bean starts)
@@ -206,12 +215,9 @@ public void afterPropertiesSet() {
206215
if (this.jobDetail != null) {
207216
this.jobDataMap.put(JobDetailAwareTrigger.JOB_DETAIL_KEY, this.jobDetail);
208217
}
209-
if (this.startDelay > 0) {
218+
if (this.startDelay > 0 || this.startTime == null) {
210219
this.startTime = new Date(System.currentTimeMillis() + this.startDelay);
211220
}
212-
else if (this.startTime == null) {
213-
this.startTime = new Date();
214-
}
215221
if (this.timeZone == null) {
216222
this.timeZone = TimeZone.getDefault();
217223
}

spring-context-support/src/main/java/org/springframework/scheduling/quartz/JobDetailBean.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@
4848
* @see org.springframework.beans.factory.BeanNameAware
4949
* @see org.quartz.Scheduler#DEFAULT_GROUP
5050
*/
51-
@SuppressWarnings("serial")
51+
@SuppressWarnings({"serial", "rawtypes"})
5252
public class JobDetailBean extends JobDetail
5353
implements BeanNameAware, ApplicationContextAware, InitializingBean {
5454

55-
private Class actualJobClass;
55+
private Class<?> actualJobClass;
5656

5757
private String beanName;
5858

@@ -82,7 +82,7 @@ public void setJobClass(Class jobClass) {
8282
* to adapt the given job class to the Quartz Job interface.
8383
*/
8484
@Override
85-
public Class getJobClass() {
85+
public Class<?> getJobClass() {
8686
return (this.actualJobClass != null ? this.actualJobClass : super.getJobClass());
8787
}
8888

@@ -109,9 +109,9 @@ public void setJobDataAsMap(Map jobDataAsMap) {
109109
* @see SchedulerFactoryBean#setJobListeners
110110
* @see org.quartz.JobListener#getName
111111
*/
112-
public void setJobListenerNames(String[] names) {
113-
for (int i = 0; i < names.length; i++) {
114-
addJobListener(names[i]);
112+
public void setJobListenerNames(String... names) {
113+
for (String name : names) {
114+
addJobListener(name);
115115
}
116116
}
117117

spring-context-support/src/main/java/org/springframework/scheduling/quartz/MethodInvokingJobDetailFactoryBean.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class MethodInvokingJobDetailFactoryBean extends ArgumentConvertingMethod
9292
jobDetailImplClass = null;
9393
}
9494
try {
95-
Class jobExecutionContextClass =
95+
Class<?> jobExecutionContextClass =
9696
QuartzJobBean.class.getClassLoader().loadClass("org.quartz.JobExecutionContext");
9797
setResultMethod = jobExecutionContextClass.getMethod("setResult", Object.class);
9898
}
@@ -172,7 +172,7 @@ public void setTargetBeanName(String targetBeanName) {
172172
* @see SchedulerFactoryBean#setJobListeners
173173
* @see org.quartz.JobListener#getName
174174
*/
175-
public void setJobListenerNames(String[] names) {
175+
public void setJobListenerNames(String... names) {
176176
this.jobListenerNames = names;
177177
}
178178

@@ -189,7 +189,7 @@ public void setBeanFactory(BeanFactory beanFactory) {
189189
}
190190

191191
@Override
192-
protected Class resolveClassName(String className) throws ClassNotFoundException {
192+
protected Class<?> resolveClassName(String className) throws ClassNotFoundException {
193193
return ClassUtils.forName(className, this.beanClassLoader);
194194
}
195195

@@ -201,7 +201,7 @@ public void afterPropertiesSet() throws ClassNotFoundException, NoSuchMethodExce
201201
String name = (this.name != null ? this.name : this.beanName);
202202

203203
// Consider the concurrent flag to choose between stateful and stateless job.
204-
Class jobClass = (this.concurrent ? MethodInvokingJob.class : StatefulMethodInvokingJob.class);
204+
Class<?> jobClass = (this.concurrent ? MethodInvokingJob.class : StatefulMethodInvokingJob.class);
205205

206206
// Build JobDetail instance.
207207
if (jobDetailImplClass != null) {
@@ -249,8 +249,8 @@ protected void postProcessJobDetail(JobDetail jobDetail) {
249249
* Overridden to support the {@link #setTargetBeanName "targetBeanName"} feature.
250250
*/
251251
@Override
252-
public Class getTargetClass() {
253-
Class targetClass = super.getTargetClass();
252+
public Class<?> getTargetClass() {
253+
Class<?> targetClass = super.getTargetClass();
254254
if (targetClass == null && this.targetBeanName != null) {
255255
Assert.state(this.beanFactory != null, "BeanFactory must be set when using 'targetBeanName'");
256256
targetClass = this.beanFactory.getType(this.targetBeanName);

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerAccessor.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public void setJobSchedulingDataLocation(String jobSchedulingDataLocation) {
128128
* to jobs defined directly on this SchedulerFactoryBean.
129129
* @see org.quartz.xml.XmlSchedulingDataProcessor
130130
*/
131-
public void setJobSchedulingDataLocations(String[] jobSchedulingDataLocations) {
131+
public void setJobSchedulingDataLocations(String... jobSchedulingDataLocations) {
132132
this.jobSchedulingDataLocations = jobSchedulingDataLocations;
133133
}
134134

@@ -142,9 +142,8 @@ public void setJobSchedulingDataLocations(String[] jobSchedulingDataLocations) {
142142
* @see org.quartz.JobDetail
143143
* @see JobDetailBean
144144
* @see JobDetailAwareTrigger
145-
* @see org.quartz.Trigger#setJobName
146145
*/
147-
public void setJobDetails(JobDetail[] jobDetails) {
146+
public void setJobDetails(JobDetail... jobDetails) {
148147
// Use modifiable ArrayList here, to allow for further adding of
149148
// JobDetail objects during autodetection of JobDetailAwareTriggers.
150149
this.jobDetails = new ArrayList<JobDetail>(Arrays.asList(jobDetails));
@@ -156,7 +155,6 @@ public void setJobDetails(JobDetail[] jobDetails) {
156155
* @param calendars Map with calendar names as keys as Calendar
157156
* objects as values
158157
* @see org.quartz.Calendar
159-
* @see org.quartz.Trigger#setCalendarName
160158
*/
161159
public void setCalendars(Map<String, Calendar> calendars) {
162160
this.calendars = calendars;
@@ -175,56 +173,58 @@ public void setCalendars(Map<String, Calendar> calendars) {
175173
* @see CronTriggerBean
176174
* @see SimpleTriggerBean
177175
*/
178-
public void setTriggers(Trigger[] triggers) {
176+
public void setTriggers(Trigger... triggers) {
179177
this.triggers = Arrays.asList(triggers);
180178
}
181179

182180

183181
/**
184182
* Specify Quartz SchedulerListeners to be registered with the Scheduler.
185183
*/
186-
public void setSchedulerListeners(SchedulerListener[] schedulerListeners) {
184+
public void setSchedulerListeners(SchedulerListener... schedulerListeners) {
187185
this.schedulerListeners = schedulerListeners;
188186
}
189187

190188
/**
191189
* Specify global Quartz JobListeners to be registered with the Scheduler.
192190
* Such JobListeners will apply to all Jobs in the Scheduler.
193191
*/
194-
public void setGlobalJobListeners(JobListener[] globalJobListeners) {
192+
public void setGlobalJobListeners(JobListener... globalJobListeners) {
195193
this.globalJobListeners = globalJobListeners;
196194
}
197195

198196
/**
199197
* Specify named Quartz JobListeners to be registered with the Scheduler.
200198
* Such JobListeners will only apply to Jobs that explicitly activate
201199
* them via their name.
200+
* <p>Note that non-global JobListeners are not supported on Quartz 2.x -
201+
* manually register a Matcher against the Quartz ListenerManager instead.
202202
* @see org.quartz.JobListener#getName
203-
* @see org.quartz.JobDetail#addJobListener
204203
* @see JobDetailBean#setJobListenerNames
205204
*/
206-
public void setJobListeners(JobListener[] jobListeners) {
205+
public void setJobListeners(JobListener... jobListeners) {
207206
this.jobListeners = jobListeners;
208207
}
209208

210209
/**
211210
* Specify global Quartz TriggerListeners to be registered with the Scheduler.
212211
* Such TriggerListeners will apply to all Triggers in the Scheduler.
213212
*/
214-
public void setGlobalTriggerListeners(TriggerListener[] globalTriggerListeners) {
213+
public void setGlobalTriggerListeners(TriggerListener... globalTriggerListeners) {
215214
this.globalTriggerListeners = globalTriggerListeners;
216215
}
217216

218217
/**
219218
* Specify named Quartz TriggerListeners to be registered with the Scheduler.
220219
* Such TriggerListeners will only apply to Triggers that explicitly activate
221220
* them via their name.
221+
* <p>Note that non-global TriggerListeners are not supported on Quartz 2.x -
222+
* manually register a Matcher against the Quartz ListenerManager instead.
222223
* @see org.quartz.TriggerListener#getName
223-
* @see org.quartz.Trigger#addTriggerListener
224224
* @see CronTriggerBean#setTriggerListenerNames
225225
* @see SimpleTriggerBean#setTriggerListenerNames
226226
*/
227-
public void setTriggerListeners(TriggerListener[] triggerListeners) {
227+
public void setTriggerListeners(TriggerListener... triggerListeners) {
228228
this.triggerListeners = triggerListeners;
229229
}
230230

@@ -251,8 +251,8 @@ protected void registerJobsAndTriggers() throws SchedulerException {
251251
if (this.transactionManager != null) {
252252
transactionStatus = this.transactionManager.getTransaction(new DefaultTransactionDefinition());
253253
}
254-
try {
255254

255+
try {
256256
if (this.jobSchedulingDataLocations != null) {
257257
ClassLoadHelper clh = new ResourceLoaderClassLoadHelper(this.resourceLoader);
258258
clh.initialize();
@@ -396,7 +396,7 @@ private JobDetail findJobDetail(Trigger trigger) {
396396
}
397397
else {
398398
try {
399-
Map jobDataMap = (Map) ReflectionUtils.invokeMethod(Trigger.class.getMethod("getJobDataMap"), trigger);
399+
Map<?, ?> jobDataMap = (Map<?, ?>) ReflectionUtils.invokeMethod(Trigger.class.getMethod("getJobDataMap"), trigger);
400400
return (JobDetail) jobDataMap.remove(JobDetailAwareTrigger.JOB_DETAIL_KEY);
401401
}
402402
catch (NoSuchMethodException ex) {

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleTriggerBean.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ public void setMisfireInstructionName(String constantName) {
114114
* @see SchedulerFactoryBean#setTriggerListeners
115115
* @see org.quartz.TriggerListener#getName
116116
*/
117-
public void setTriggerListenerNames(String[] names) {
118-
for (int i = 0; i < names.length; i++) {
119-
addTriggerListener(names[i]);
117+
public void setTriggerListenerNames(String... names) {
118+
for (String name : names) {
119+
addTriggerListener(name);
120120
}
121121
}
122122

@@ -153,14 +153,18 @@ public void setBeanName(String beanName) {
153153
}
154154

155155

156+
/**
157+
* Note that this method's declaration of a ParseException is deprecated
158+
* and will be removed in the Spring 4.0 line.
159+
*/
156160
public void afterPropertiesSet() throws ParseException {
157161
if (getName() == null) {
158162
setName(this.beanName);
159163
}
160164
if (getGroup() == null) {
161165
setGroup(Scheduler.DEFAULT_GROUP);
162166
}
163-
if (getStartTime() == null) {
167+
if (this.startDelay > 0 || getStartTime() == null) {
164168
setStartTime(new Date(System.currentTimeMillis() + this.startDelay));
165169
}
166170
if (this.jobDetail != null) {

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SimpleTriggerFactoryBean.java

+14-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.scheduling.quartz;
1818

1919
import java.lang.reflect.Method;
20-
import java.text.ParseException;
2120
import java.util.Date;
2221
import java.util.Map;
2322

@@ -141,10 +140,20 @@ public void setJobDataAsMap(Map<String, ?> jobDataAsMap) {
141140
this.jobDataMap.putAll(jobDataAsMap);
142141
}
143142

143+
/**
144+
* Set a specific start time for the trigger.
145+
* <p>Note that a dynamically computed {@link #setStartDelay} specification
146+
* overrides a static timestamp set here.
147+
*/
148+
public void setStartTime(Date startTime) {
149+
this.startTime = startTime;
150+
}
151+
144152
/**
145153
* Set the start delay in milliseconds.
146154
* <p>The start delay is added to the current system time (when the bean starts)
147155
* to control the start time of the trigger.
156+
* @see #setStartTime
148157
*/
149158
public void setStartDelay(long startDelay) {
150159
Assert.isTrue(startDelay >= 0, "Start delay cannot be negative");
@@ -200,7 +209,7 @@ public void setBeanName(String beanName) {
200209
}
201210

202211

203-
public void afterPropertiesSet() throws ParseException {
212+
public void afterPropertiesSet() {
204213
if (this.name == null) {
205214
this.name = this.beanName;
206215
}
@@ -210,12 +219,9 @@ public void afterPropertiesSet() throws ParseException {
210219
if (this.jobDetail != null) {
211220
this.jobDataMap.put(JobDetailAwareTrigger.JOB_DETAIL_KEY, this.jobDetail);
212221
}
213-
if (this.startDelay > 0) {
222+
if (this.startDelay > 0 || this.startTime == null) {
214223
this.startTime = new Date(System.currentTimeMillis() + this.startDelay);
215224
}
216-
else if (this.startTime == null) {
217-
this.startTime = new Date();
218-
}
219225

220226
/*
221227
SimpleTriggerImpl sti = new SimpleTriggerImpl();
@@ -231,7 +237,7 @@ else if (this.startTime == null) {
231237
this.simpleTrigger = sti;
232238
*/
233239

234-
Class simpleTriggerClass;
240+
Class<?> simpleTriggerClass;
235241
Method jobKeyMethod;
236242
try {
237243
simpleTriggerClass = getClass().getClassLoader().loadClass("org.quartz.impl.triggers.SimpleTriggerImpl");
@@ -277,4 +283,5 @@ public Class<?> getObjectType() {
277283
public boolean isSingleton() {
278284
return true;
279285
}
286+
280287
}

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SpringBeanJobFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class SpringBeanJobFactory extends AdaptableJobFactory implements Schedul
5959
* ignored if there is no corresponding property found on the particular
6060
* job class (all other unknown properties will still trigger an exception).
6161
*/
62-
public void setIgnoredUnknownProperties(String[] ignoredUnknownProperties) {
62+
public void setIgnoredUnknownProperties(String... ignoredUnknownProperties) {
6363
this.ignoredUnknownProperties = ignoredUnknownProperties;
6464
}
6565

0 commit comments

Comments
 (0)