Skip to content

Commit 8adff1b

Browse files
snicolljhoeller
authored andcommitted
Support Quartz trigger without JobDetail
This commit allows to create a Quartz trigger implementation via either `CronTriggerFactoryBean` or `SimpleTriggerFactoryBean` even if no job detail is provided. Issue: SPR-13604 (cherry picked from commit 2970065)
1 parent fe76f40 commit 8adff1b

File tree

4 files changed

+108
-16
lines changed

4 files changed

+108
-16
lines changed

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -245,7 +245,9 @@ public void afterPropertiesSet() {
245245
CronTriggerImpl cti = new CronTriggerImpl();
246246
cti.setName(this.name);
247247
cti.setGroup(this.group);
248-
cti.setJobKey(this.jobDetail.getKey());
248+
if (this.jobDetail != null) {
249+
cti.setJobKey(this.jobDetail.getKey());
250+
}
249251
cti.setJobDataMap(this.jobDataMap);
250252
cti.setStartTime(this.startTime);
251253
cti.setCronExpression(this.cronExpression);
@@ -274,12 +276,14 @@ public void afterPropertiesSet() {
274276
MutablePropertyValues pvs = new MutablePropertyValues();
275277
pvs.add("name", this.name);
276278
pvs.add("group", this.group);
277-
if (jobKeyMethod != null) {
278-
pvs.add("jobKey", ReflectionUtils.invokeMethod(jobKeyMethod, this.jobDetail));
279-
}
280-
else {
281-
pvs.add("jobName", this.jobDetail.getName());
282-
pvs.add("jobGroup", this.jobDetail.getGroup());
279+
if (this.jobDetail != null) {
280+
if (jobKeyMethod != null) {
281+
pvs.add("jobKey", ReflectionUtils.invokeMethod(jobKeyMethod, this.jobDetail));
282+
}
283+
else {
284+
pvs.add("jobName", this.jobDetail.getName());
285+
pvs.add("jobGroup", this.jobDetail.getGroup());
286+
}
283287
}
284288
pvs.add("jobDataMap", this.jobDataMap);
285289
pvs.add("startTime", this.startTime);

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -237,7 +237,9 @@ public void afterPropertiesSet() {
237237
SimpleTriggerImpl sti = new SimpleTriggerImpl();
238238
sti.setName(this.name);
239239
sti.setGroup(this.group);
240-
sti.setJobKey(this.jobDetail.getKey());
240+
if (this.jobDetail != null) {
241+
sti.setJobKey(this.jobDetail.getKey());
242+
}
241243
sti.setJobDataMap(this.jobDataMap);
242244
sti.setStartTime(this.startTime);
243245
sti.setRepeatInterval(this.repeatInterval);
@@ -265,12 +267,14 @@ public void afterPropertiesSet() {
265267
MutablePropertyValues pvs = new MutablePropertyValues();
266268
pvs.add("name", this.name);
267269
pvs.add("group", this.group);
268-
if (jobKeyMethod != null) {
269-
pvs.add("jobKey", ReflectionUtils.invokeMethod(jobKeyMethod, this.jobDetail));
270-
}
271-
else {
272-
pvs.add("jobName", this.jobDetail.getName());
273-
pvs.add("jobGroup", this.jobDetail.getGroup());
270+
if (this.jobDetail != null) {
271+
if (jobKeyMethod != null) {
272+
pvs.add("jobKey", ReflectionUtils.invokeMethod(jobKeyMethod, this.jobDetail));
273+
}
274+
else {
275+
pvs.add("jobName", this.jobDetail.getName());
276+
pvs.add("jobGroup", this.jobDetail.getGroup());
277+
}
274278
}
275279
pvs.add("jobDataMap", this.jobDataMap);
276280
pvs.add("startTime", this.startTime);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2016 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.text.ParseException;
20+
21+
import org.junit.Test;
22+
import org.quartz.CronTrigger;
23+
24+
import static org.junit.Assert.*;
25+
26+
/**
27+
* @author Stephane Nicoll
28+
*/
29+
public class CronTriggerFactoryBeanTests {
30+
31+
@Test
32+
public void createWithoutJobDetail() throws ParseException {
33+
CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
34+
factory.setName("myTrigger");
35+
factory.setCronExpression("0 15 10 ? * *");
36+
factory.afterPropertiesSet();
37+
CronTrigger trigger = factory.getObject();
38+
assertEquals("0 15 10 ? * *", trigger.getCronExpression());
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2016 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.text.ParseException;
20+
21+
import org.junit.Test;
22+
import org.quartz.SimpleTrigger;
23+
24+
import static org.junit.Assert.*;
25+
26+
/**
27+
* @author Stephane Nicoll
28+
*/
29+
public class SimpleTriggerFactoryBeanTests {
30+
31+
@Test
32+
public void createWithoutJobDetail() throws ParseException {
33+
SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
34+
factory.setName("myTrigger");
35+
factory.setRepeatCount(5);
36+
factory.setRepeatInterval(1000L);
37+
factory.afterPropertiesSet();
38+
SimpleTrigger trigger = factory.getObject();
39+
assertEquals(5, trigger.getRepeatCount());
40+
assertEquals(1000L, trigger.getRepeatInterval());
41+
}
42+
43+
}

0 commit comments

Comments
 (0)