Skip to content

Commit ab9adb2

Browse files
Merge pull request #16 from jblayneyXpanxion/master
Adding overrides & flexibility to AdHocScheduler
2 parents 19a8407 + a7fdd4f commit ab9adb2

File tree

21 files changed

+698
-211
lines changed

21 files changed

+698
-211
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Maven:
8080
<dependency>
8181
<groupId>com.github.chrisgleissner</groupId>
8282
<artifactId>spring-batch-rest-api</artifactId>
83-
<version>1.4.1</version>
83+
<version>1.4.3</version>
8484
</dependency>
8585
```
8686

@@ -99,7 +99,7 @@ Maven:
9999
<dependency>
100100
<groupId>com.github.chrisgleissner</groupId>
101101
<artifactId>spring-batch-rest-quartz-api</artifactId>
102-
<version>1.4.1</version>
102+
<version>1.4.3</version>
103103
</dependency>
104104
```
105105

api/src/test/java/com/github/chrisgleissner/springbatchrest/api/core/RestTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import com.github.chrisgleissner.springbatchrest.api.core.jobexecution.JobExecution;
44
import com.github.chrisgleissner.springbatchrest.api.core.jobexecution.JobExecutionResource;
5-
import com.github.chrisgleissner.springbatchrest.util.core.AdHocBatchConfig;
65
import com.github.chrisgleissner.springbatchrest.util.core.JobBuilder;
76
import com.github.chrisgleissner.springbatchrest.util.core.JobConfig;
7+
import com.github.chrisgleissner.springbatchrest.util.core.config.AdHocBatchConfig;
8+
89
import org.junit.Before;
910
import org.junit.Test;
1011
import org.junit.runner.RunWith;
@@ -28,7 +29,6 @@
2829
import static com.github.chrisgleissner.springbatchrest.util.core.property.JobPropertyResolvers.JobProperties;
2930
import static org.assertj.core.api.Assertions.assertThat;
3031
import static org.springframework.batch.core.ExitStatus.COMPLETED;
31-
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
3232
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
3333

3434
@RunWith(SpringRunner.class)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
spring.main.banner-mode=off
22
spring.boot.admin.client.url: "http://localhost:8090"
33
management.endpoints.web.exposure.include: "*"
4-
spring.resources.add-mappings=true
4+
spring.resources.add-mappings=true
5+
server.port=9090

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>2.2.5.RELEASE</version>
9+
<version>2.2.6.RELEASE</version>
1010
</parent>
1111

1212
<groupId>com.github.chrisgleissner</groupId>

quartz-api/src/test/java/com/github/chrisgleissner/springbatchrest/api/quartz/RestTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import com.github.chrisgleissner.springbatchrest.api.core.jobexecution.JobExecution;
44
import com.github.chrisgleissner.springbatchrest.api.core.jobexecution.JobExecutionResource;
5-
import com.github.chrisgleissner.springbatchrest.util.core.AdHocBatchConfig;
65
import com.github.chrisgleissner.springbatchrest.util.core.JobBuilder;
76
import com.github.chrisgleissner.springbatchrest.util.core.JobConfig;
7+
import com.github.chrisgleissner.springbatchrest.util.core.config.AdHocBatchConfig;
88
import com.github.chrisgleissner.springbatchrest.util.quartz.AdHocScheduler;
99
import org.junit.Before;
1010
import org.junit.Test;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
spring.main.banner-mode=off
22
spring.boot.admin.client.url: "http://localhost:8090"
33
management.endpoints.web.exposure.include: "*"
4-
spring.resources.add-mappings=true
4+
spring.resources.add-mappings=true
5+
server.port=9090
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.github.chrisgleissner.springbatchrest.util;
2+
3+
import static java.util.Collections.emptyMap;
4+
import static java.util.stream.Collectors.toMap;
5+
6+
import java.util.Date;
7+
import java.util.Map;
8+
import java.util.Optional;
9+
10+
import org.springframework.batch.core.JobParameter;
11+
import org.springframework.batch.core.JobParameters;
12+
13+
public class JobParamUtil {
14+
15+
public static JobParameters convertRawToJobParams(Map<String, Object> properties) {
16+
return new JobParameters(convertRawToParamMap(properties));
17+
}
18+
19+
public static Map<String, JobParameter> convertRawToParamMap(Map<String, Object> properties) {
20+
return Optional.ofNullable(properties).orElse(emptyMap()).entrySet().stream()
21+
.collect(toMap(Map.Entry::getKey, e -> createJobParameter(e.getValue())));
22+
}
23+
24+
public static JobParameter createJobParameter(Object value) {
25+
if (value instanceof Date)
26+
return new JobParameter((Date) value);
27+
else if (value instanceof Long)
28+
return new JobParameter((Long) value);
29+
else if (value instanceof Double)
30+
return new JobParameter((Double) value);
31+
else
32+
return new JobParameter("" + value);
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.github.chrisgleissner.springbatchrest.util;
2+
3+
import static org.quartz.TriggerBuilder.newTrigger;
4+
5+
import java.util.Date;
6+
7+
import org.quartz.CronScheduleBuilder;
8+
import org.quartz.Trigger;
9+
10+
public class TriggerUtil {
11+
12+
// Defined for clarity - passing in null is not obvious to what is happening
13+
// under the covers
14+
public static final String QUARTZ_DEFAULT_GROUP = null;
15+
16+
public static Trigger triggerFor(String cronExpression, String jobName) {
17+
return triggerFor(cronExpression, jobName, QUARTZ_DEFAULT_GROUP);
18+
}
19+
20+
public static Trigger triggerFor(String cronExpression, String jobName, String groupName) {
21+
CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule(cronExpression);
22+
return newTrigger().withIdentity(jobName, groupName).withSchedule(builder).forJob(jobName, groupName).build();
23+
}
24+
25+
public static Trigger triggerFor(Date dateToRun, String jobName) {
26+
return triggerFor(dateToRun, jobName, QUARTZ_DEFAULT_GROUP);
27+
}
28+
29+
public static Trigger triggerFor(Date dateToRun, String jobName, String groupName) {
30+
return newTrigger().withIdentity(jobName, groupName).startAt(dateToRun).forJob(jobName, groupName).build();
31+
}
32+
}

util/src/main/java/com/github/chrisgleissner/springbatchrest/util/core/AdHocStarter.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.chrisgleissner.springbatchrest.util.core;
22

3+
import com.github.chrisgleissner.springbatchrest.util.JobParamUtil;
34
import com.github.chrisgleissner.springbatchrest.util.core.JobConfig.JobConfigBuilder;
45
import com.github.chrisgleissner.springbatchrest.util.core.property.JobPropertyResolvers;
56
import lombok.extern.slf4j.Slf4j;
@@ -17,15 +18,11 @@
1718
import org.springframework.stereotype.Component;
1819

1920
import javax.batch.operations.BatchRuntimeException;
20-
import java.util.Date;
2121
import java.util.HashMap;
2222
import java.util.Map;
23-
import java.util.Optional;
2423
import java.util.UUID;
2524

2625
import static java.lang.String.format;
27-
import static java.util.Collections.emptyMap;
28-
import static java.util.stream.Collectors.toMap;
2926

3027
@Slf4j
3128
@Component
@@ -70,7 +67,7 @@ public JobExecution start(Job job, Boolean async, Map<String, Object> properties
7067
}
7168
JobConfigBuilder builder = new JobConfigBuilder();
7269
JobConfig jobConfig = builder
73-
.asynchronous(true)
70+
.asynchronous(async)
7471
.properties(properties == null ? new HashMap<>() : properties)
7572
.name(job.getName()).build();
7673
JobBuilder.registerJob(jobRegistry, existingJob == null ? job : existingJob);
@@ -82,8 +79,7 @@ public JobExecution start(JobConfig jobConfig) {
8279
Job job = jobLocator.getJob(jobConfig.getName());
8380
jobPropertyResolvers.started(jobConfig);
8481

85-
Map<String, JobParameter> params = Optional.ofNullable(jobConfig.getProperties()).orElse(emptyMap()).entrySet().stream()
86-
.collect(toMap(Map.Entry::getKey, e -> createJobParameter(e.getValue())));
82+
Map<String, JobParameter> params = JobParamUtil.convertRawToParamMap(jobConfig.getProperties());
8783
if (addUniqueJobParameter)
8884
params.put("uuid", new JobParameter(UUID.randomUUID().toString()));
8985
JobParameters jobParameters = new JobParameters(params);
@@ -99,15 +95,4 @@ public JobExecution start(JobConfig jobConfig) {
9995
jobConfig.getName(), jobConfig, e.getMessage()), e);
10096
}
10197
}
102-
103-
private JobParameter createJobParameter(Object value) {
104-
if (value instanceof Date)
105-
return new JobParameter((Date) value);
106-
else if (value instanceof Long)
107-
return new JobParameter((Long) value);
108-
else if (value instanceof Double)
109-
return new JobParameter((Double) value);
110-
else
111-
return new JobParameter("" + value);
112-
}
11398
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.github.chrisgleissner.springbatchrest.util.core;
2+
3+
import java.util.Map;
4+
5+
import org.quartz.JobDetail;
6+
import org.quartz.JobKey;
7+
import org.quartz.impl.JobDetailImpl;
8+
import org.quartz.utils.Key;
9+
10+
import lombok.Data;
11+
import lombok.EqualsAndHashCode;
12+
13+
/**
14+
* Extension class to add JobParameters to a JobDetail for scheduled execution
15+
* with JobParameters.
16+
*
17+
* @author theJeff77
18+
*/
19+
20+
@Data
21+
@EqualsAndHashCode(callSuper = false)
22+
public class JobParamsDetail extends JobDetailImpl {
23+
24+
private static final long serialVersionUID = -4813776846767160965L;
25+
private Map<String, Object> rawJobParameters;
26+
27+
// Constructor to do a deep copy all data from JobDetail into this subclass.
28+
// Based off of JobBuilder's construction code.
29+
public JobParamsDetail(JobDetail jobDetail) {
30+
31+
this.setJobClass(jobDetail.getJobClass());
32+
this.setDescription(jobDetail.getDescription());
33+
if (jobDetail.getKey() == null)
34+
this.setKey(new JobKey(Key.createUniqueName(null), null));
35+
this.setKey(jobDetail.getKey());
36+
this.setDurability(jobDetail.isDurable());
37+
this.setRequestsRecovery(jobDetail.requestsRecovery());
38+
39+
if (!jobDetail.getJobDataMap().isEmpty())
40+
this.setJobDataMap(jobDetail.getJobDataMap());
41+
}
42+
43+
public JobParamsDetail(JobDetail jobDetail, Map<String, Object> rawJobParameters) {
44+
this(jobDetail);
45+
this.rawJobParameters = rawJobParameters;
46+
}
47+
}

0 commit comments

Comments
 (0)