Skip to content

Commit 67a65ea

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 35f0fce commit 67a65ea

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

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

Lines changed: 4 additions & 2 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() throws ParseException {
237237
CronTriggerImpl cti = new CronTriggerImpl();
238238
cti.setName(this.name);
239239
cti.setGroup(this.group);
240-
cti.setJobKey(this.jobDetail.getKey());
240+
if (this.jobDetail != null) {
241+
cti.setJobKey(this.jobDetail.getKey());
242+
}
241243
cti.setJobDataMap(this.jobDataMap);
242244
cti.setStartTime(this.startTime);
243245
cti.setCronExpression(this.cronExpression);

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

Lines changed: 4 additions & 2 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.
@@ -228,7 +228,9 @@ public void afterPropertiesSet() {
228228
SimpleTriggerImpl sti = new SimpleTriggerImpl();
229229
sti.setName(this.name);
230230
sti.setGroup(this.group);
231-
sti.setJobKey(this.jobDetail.getKey());
231+
if (this.jobDetail != null) {
232+
sti.setJobKey(this.jobDetail.getKey());
233+
}
232234
sti.setJobDataMap(this.jobDataMap);
233235
sti.setStartTime(this.startTime);
234236
sti.setRepeatInterval(this.repeatInterval);
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)