Skip to content

Commit ce16ad2

Browse files
committed
BATCH-2007: Added a JobOperator implementation that complies with JSR-352.
* Added JsrJobOperator * Added a ParametersConverter (and JSR-352 implementation) that converts parameters as specified by the JSR to JobParameters and back * Added a method to the JobRepository to allow the direct creation of a JobInstance (since the JSR requires a new instance for each call to JobOperator#start * Added a base context to be bootstrapped when the JobOperator is first referenced. It provides things like a JobRepository, etc.
1 parent c7176a7 commit ce16ad2

File tree

64 files changed

+2099
-290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2099
-290
lines changed

spring-batch-core-tests/src/test/java/org/springframework/batch/core/test/step/StepExecutionSerializationUtilsTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class StepExecutionSerializationUtilsTests {
4141
@Test
4242
public void testCycle() throws Exception {
4343
StepExecution stepExecution = new StepExecution("step", new JobExecution(new JobInstance(123L,
44-
"job"), 321L, new JobParameters()), 11L);
44+
"job"), 321L, new JobParameters(), null), 11L);
4545
stepExecution.getExecutionContext().put("foo.bar.spam", 123);
4646
StepExecution result = getCopy(stepExecution);
4747
assertEquals(stepExecution, result);
@@ -58,9 +58,10 @@ public void testMultipleCycles() throws Throwable {
5858
CompletionService<StepExecution> completionService = new ExecutorCompletionService<StepExecution>(executor);
5959

6060
for (int i = 0; i < repeats; i++) {
61-
final JobExecution jobExecution = new JobExecution(new JobInstance(123L, "job"), 321L, new JobParameters());
61+
final JobExecution jobExecution = new JobExecution(new JobInstance(123L, "job"), 321L, new JobParameters(), null);
6262
for (int j = 0; j < threads; j++) {
6363
completionService.submit(new Callable<StepExecution>() {
64+
@Override
6465
public StepExecution call() throws Exception {
6566
final StepExecution stepExecution = jobExecution.createStepExecution("step");
6667
stepExecution.getExecutionContext().put("foo.bar.spam", 123);

spring-batch-core/src/main/java/org/springframework/batch/core/JobExecution.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,27 @@ public class JobExecution extends Entity {
6262

6363
private transient volatile List<Throwable> failureExceptions = new CopyOnWriteArrayList<Throwable>();
6464

65+
private final String jobConfigurationName;
66+
6567
/**
6668
* Because a JobExecution isn't valid unless the job is set, this
6769
* constructor is the only valid one from a modeling point of view.
6870
*
6971
* @param job the job of which this execution is a part
7072
*/
71-
public JobExecution(JobInstance job, Long id, JobParameters jobParameters) {
73+
public JobExecution(JobInstance job, Long id, JobParameters jobParameters, String jobConfigurationName) {
7274
super(id);
7375
this.jobInstance = job;
7476
this.jobParameters = jobParameters == null ? new JobParameters() : jobParameters;
77+
this.jobConfigurationName = jobConfigurationName;
78+
}
79+
80+
public JobExecution(JobInstance job, JobParameters jobParameters, String jobConfigurationName) {
81+
this(job, null, jobParameters, jobConfigurationName);
82+
}
83+
84+
public JobExecution(Long id, JobParameters jobParameters, String jobConfigurationName) {
85+
this(null, id, jobParameters, jobConfigurationName);
7586
}
7687

7788
/**
@@ -80,15 +91,15 @@ public JobExecution(JobInstance job, Long id, JobParameters jobParameters) {
8091
* @param job the enclosing {@link JobInstance}
8192
*/
8293
public JobExecution(JobInstance job, JobParameters jobParameters) {
83-
this(job, null, jobParameters);
94+
this(job, null, jobParameters, null);
8495
}
8596

8697
public JobExecution(Long id, JobParameters jobParameters) {
87-
this(null, id, jobParameters);
98+
this(null, id, jobParameters, null);
8899
}
89100

90101
public JobExecution(Long id) {
91-
this(null, id, null);
102+
this(null, id, null, null);
92103
}
93104

94105
public JobParameters getJobParameters() {
@@ -256,6 +267,10 @@ public void setCreateTime(Date createTime) {
256267
this.createTime = createTime;
257268
}
258269

270+
public String getJobConfigurationName() {
271+
return this.jobConfigurationName;
272+
}
273+
259274
/**
260275
* Package private method for re-constituting the step executions from
261276
* existing instances.

spring-batch-core/src/main/java/org/springframework/batch/core/explore/JobExplorer.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2007 the original author or authors.
2+
* Copyright 2006-2013 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.
@@ -21,23 +21,25 @@
2121
import org.springframework.batch.core.JobExecution;
2222
import org.springframework.batch.core.JobInstance;
2323
import org.springframework.batch.core.StepExecution;
24+
import org.springframework.batch.core.launch.NoSuchJobException;
2425
import org.springframework.batch.item.ExecutionContext;
2526

2627
/**
2728
* Entry point for browsing executions of running or historical jobs and steps.
2829
* Since the data may be re-hydrated from persistent storage, it may not contain
2930
* volatile fields that would have been present when the execution was active.
30-
*
31+
*
3132
* @author Dave Syer
32-
*
33+
* @author Michael Minella
34+
*
3335
* @since 2.0
3436
*/
3537
public interface JobExplorer {
3638

3739
/**
3840
* Fetch {@link JobInstance} values in descending order of creation (and
3941
* therefore usually of first execution).
40-
*
42+
*
4143
* @param jobName the name of the job to query
4244
* @param start the start index of the instances to return
4345
* @param count the maximum number of instances to return
@@ -51,7 +53,7 @@ public interface JobExplorer {
5153
* the parent {@link JobInstance} and associated {@link ExecutionContext}
5254
* and {@link StepExecution} instances (also including their execution
5355
* contexts).
54-
*
56+
*
5557
* @param executionId the job execution id
5658
* @return the {@link JobExecution} with this id, or null if not found
5759
*/
@@ -62,11 +64,11 @@ public interface JobExplorer {
6264
* {@link JobExecution} id. The execution context for the step should be
6365
* available in the result, and the parent job execution should have its
6466
* primitive properties, but may not contain the job instance information.
65-
*
67+
*
6668
* @param jobExecutionId the parent job execution id
6769
* @param stepExecutionId the step execution id
6870
* @return the {@link StepExecution} with this id, or null if not found
69-
*
71+
*
7072
* @see #getJobExecution(Long)
7173
*/
7274
StepExecution getStepExecution(Long jobExecutionId, Long stepExecutionId);
@@ -82,7 +84,7 @@ public interface JobExplorer {
8284
* executions may not be fully hydrated (e.g. their execution context may be
8385
* missing), depending on the implementation. Use
8486
* {@link #getStepExecution(Long, Long)} to hydrate them in that case.
85-
*
87+
*
8688
* @param jobInstance the {@link JobInstance} to query
8789
* @return the set of all executions for the specified {@link JobInstance}
8890
*/
@@ -93,7 +95,7 @@ public interface JobExplorer {
9395
* not be fully hydrated (e.g. their execution context may be missing),
9496
* depending on the implementation. Use
9597
* {@link #getStepExecution(Long, Long)} to hydrate them in that case.
96-
*
98+
*
9799
* @param jobName the name of the job
98100
* @return the set of running executions for jobs with the specified name
99101
*/
@@ -102,9 +104,20 @@ public interface JobExplorer {
102104
/**
103105
* Query the repository for all unique {@link JobInstance} names (sorted
104106
* alphabetically).
105-
*
107+
*
106108
* @return the set of job names that have been executed
107109
*/
108110
List<String> getJobNames();
109111

112+
/**
113+
* Query the repository for the number of unique {@link JobInstance}s
114+
* associated with the supplied job name.
115+
*
116+
* @param jobName the name of the job to query for
117+
* @return the number of {@link JobInstance}s that exist within the
118+
* associated job repository
119+
* @throws NoSuchJobException
120+
*/
121+
int getJobInstanceCount(String jobName) throws NoSuchJobException;
122+
110123
}

spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.batch.core.JobInstance;
2424
import org.springframework.batch.core.StepExecution;
2525
import org.springframework.batch.core.explore.JobExplorer;
26+
import org.springframework.batch.core.launch.NoSuchJobException;
2627
import org.springframework.batch.core.repository.dao.ExecutionContextDao;
2728
import org.springframework.batch.core.repository.dao.JobExecutionDao;
2829
import org.springframework.batch.core.repository.dao.JobInstanceDao;
@@ -33,6 +34,7 @@
3334
*
3435
* @author Dave Syer
3536
* @author Lucas Ward
37+
* @author Michael Minella
3638
*
3739
* @see JobExplorer
3840
* @see JobInstanceDao
@@ -180,6 +182,14 @@ public List<String> getJobNames() {
180182
return jobInstanceDao.getJobNames();
181183
}
182184

185+
/* (non-Javadoc)
186+
* @see org.springframework.batch.core.explore.JobExplorer#getJobInstanceCount(java.lang.String)
187+
*/
188+
@Override
189+
public int getJobInstanceCount(String jobName) throws NoSuchJobException {
190+
return jobInstanceDao.getJobInstanceCount(jobName);
191+
}
192+
183193
/*
184194
* Find all dependencies for a JobExecution, including JobInstance (which
185195
* requires JobParameters) plus StepExecutions
@@ -198,5 +208,4 @@ private void getStepExecutionDependencies(StepExecution stepExecution) {
198208
stepExecution.setExecutionContext(ecDao.getExecutionContext(stepExecution));
199209
}
200210
}
201-
202211
}

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/JobContext.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,56 +35,86 @@ public class JobContext implements javax.batch.runtime.context.JobContext {
3535

3636
private JobExecution jobExecution;
3737
private Object transientUserData;
38+
private ParametersConverter jobParametersConverter;
3839

3940
/**
4041
* @param jobExecution for the related job
4142
*/
42-
public JobContext(JobExecution jobExecution) {
43+
public JobContext(JobExecution jobExecution, ParametersConverter jobParametersConverter) {
4344
Assert.notNull(jobExecution, "A JobExecution is required");
45+
Assert.notNull(jobParametersConverter, "A ParametersConverter is required");
4446

4547
this.jobExecution = jobExecution;
48+
this.jobParametersConverter = jobParametersConverter;
4649
}
4750

51+
/* (non-Javadoc)
52+
* @see javax.batch.runtime.context.JobContext#getJobName()
53+
*/
4854
@Override
4955
public String getJobName() {
5056
return jobExecution.getJobInstance().getJobName();
5157
}
5258

59+
/* (non-Javadoc)
60+
* @see javax.batch.runtime.context.JobContext#getTransientUserData()
61+
*/
5362
@Override
5463
public Object getTransientUserData() {
5564
return transientUserData;
5665
}
5766

67+
/* (non-Javadoc)
68+
* @see javax.batch.runtime.context.JobContext#setTransientUserData(java.lang.Object)
69+
*/
5870
@Override
5971
public void setTransientUserData(Object data) {
6072
transientUserData = data;
6173
}
6274

75+
/* (non-Javadoc)
76+
* @see javax.batch.runtime.context.JobContext#getInstanceId()
77+
*/
6378
@Override
6479
public long getInstanceId() {
6580
return jobExecution.getJobInstance().getId();
6681
}
6782

83+
/* (non-Javadoc)
84+
* @see javax.batch.runtime.context.JobContext#getExecutionId()
85+
*/
6886
@Override
6987
public long getExecutionId() {
7088
return jobExecution.getId();
7189
}
7290

91+
/* (non-Javadoc)
92+
* @see javax.batch.runtime.context.JobContext#getProperties()
93+
*/
7394
@Override
7495
public Properties getProperties() {
75-
return jobExecution.getJobParameters().toProperties();
96+
return jobParametersConverter.convert(this.jobExecution.getJobParameters());
7697
}
7798

99+
/* (non-Javadoc)
100+
* @see javax.batch.runtime.context.JobContext#getBatchStatus()
101+
*/
78102
@Override
79103
public BatchStatus getBatchStatus() {
80104
return jobExecution.getStatus().getBatchStatus();
81105
}
82106

107+
/* (non-Javadoc)
108+
* @see javax.batch.runtime.context.JobContext#getExitStatus()
109+
*/
83110
@Override
84111
public String getExitStatus() {
85112
return jobExecution.getExitStatus().getExitCode();
86113
}
87114

115+
/* (non-Javadoc)
116+
* @see javax.batch.runtime.context.JobContext#setExitStatus(java.lang.String)
117+
*/
88118
@Override
89119
public void setExitStatus(String status) {
90120
jobExecution.setExitStatus(new ExitStatus(status));

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/JobExecution.java

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,57 +32,87 @@
3232
public class JobExecution implements javax.batch.runtime.JobExecution {
3333

3434
private org.springframework.batch.core.JobExecution execution;
35+
private ParametersConverter parametersConverter;
3536

3637
/**
3738
* @param execution for all information to be delegated from
3839
*/
39-
public JobExecution(org.springframework.batch.core.JobExecution execution) {
40+
public JobExecution(org.springframework.batch.core.JobExecution execution, ParametersConverter parametersConverter) {
4041
Assert.notNull(execution, "A JobExecution is required");
4142
this.execution = execution;
43+
44+
this.parametersConverter = parametersConverter;
4245
}
4346

47+
/* (non-Javadoc)
48+
* @see javax.batch.runtime.JobExecution#getExecutionId()
49+
*/
4450
@Override
4551
public long getExecutionId() {
4652
return this.execution.getId();
4753
}
4854

55+
/* (non-Javadoc)
56+
* @see javax.batch.runtime.JobExecution#getJobName()
57+
*/
4958
@Override
5059
public String getJobName() {
5160
return this.execution.getJobInstance().getJobName();
5261
}
5362

63+
/* (non-Javadoc)
64+
* @see javax.batch.runtime.JobExecution#getBatchStatus()
65+
*/
5466
@Override
5567
public BatchStatus getBatchStatus() {
5668
return this.execution.getStatus().getBatchStatus();
5769
}
5870

71+
/* (non-Javadoc)
72+
* @see javax.batch.runtime.JobExecution#getStartTime()
73+
*/
5974
@Override
6075
public Date getStartTime() {
6176
return this.execution.getStartTime();
6277
}
6378

79+
/* (non-Javadoc)
80+
* @see javax.batch.runtime.JobExecution#getEndTime()
81+
*/
6482
@Override
6583
public Date getEndTime() {
6684
return this.execution.getEndTime();
6785
}
6886

87+
/* (non-Javadoc)
88+
* @see javax.batch.runtime.JobExecution#getExitStatus()
89+
*/
6990
@Override
7091
public String getExitStatus() {
7192
return this.execution.getExitStatus().getExitCode();
7293
}
7394

95+
/* (non-Javadoc)
96+
* @see javax.batch.runtime.JobExecution#getCreateTime()
97+
*/
7498
@Override
7599
public Date getCreateTime() {
76100
return this.execution.getCreateTime();
77101
}
78102

103+
/* (non-Javadoc)
104+
* @see javax.batch.runtime.JobExecution#getLastUpdatedTime()
105+
*/
79106
@Override
80107
public Date getLastUpdatedTime() {
81108
return this.execution.getLastUpdated();
82109
}
83110

111+
/* (non-Javadoc)
112+
* @see javax.batch.runtime.JobExecution#getJobParameters()
113+
*/
84114
@Override
85115
public Properties getJobParameters() {
86-
return this.execution.getJobParameters().toProperties();
116+
return parametersConverter.convert(this.execution.getJobParameters());
87117
}
88118
}

0 commit comments

Comments
 (0)