20
20
import java .util .Collection ;
21
21
import java .util .Collections ;
22
22
import java .util .HashMap ;
23
+ import java .util .LinkedHashMap ;
23
24
import java .util .Map ;
24
25
import java .util .Properties ;
25
26
33
34
import org .springframework .batch .core .JobParameter ;
34
35
import org .springframework .batch .core .JobParameters ;
35
36
import org .springframework .batch .core .JobParametersBuilder ;
36
- import org .springframework .batch .core .JobParametersIncrementer ;
37
37
import org .springframework .batch .core .JobParametersInvalidException ;
38
38
import org .springframework .batch .core .configuration .JobRegistry ;
39
39
import org .springframework .batch .core .converter .DefaultJobParametersConverter ;
51
51
import org .springframework .context .ApplicationEventPublisher ;
52
52
import org .springframework .context .ApplicationEventPublisherAware ;
53
53
import org .springframework .core .Ordered ;
54
+ import org .springframework .util .Assert ;
54
55
import org .springframework .util .PatternMatchUtils ;
55
56
import org .springframework .util .StringUtils ;
56
57
@@ -76,13 +77,13 @@ public class JobLauncherCommandLineRunner
76
77
77
78
private JobParametersConverter converter = new DefaultJobParametersConverter ();
78
79
79
- private JobLauncher jobLauncher ;
80
+ private final JobLauncher jobLauncher ;
80
81
81
- private JobRegistry jobRegistry ;
82
+ private final JobExplorer jobExplorer ;
82
83
83
- private JobExplorer jobExplorer ;
84
+ private final JobRepository jobRepository ;
84
85
85
- private JobRepository jobRepository ;
86
+ private JobRegistry jobRegistry ;
86
87
87
88
private String jobNames ;
88
89
@@ -96,17 +97,17 @@ public class JobLauncherCommandLineRunner
96
97
* Create a new {@link JobLauncherCommandLineRunner}.
97
98
* @param jobLauncher to launch jobs
98
99
* @param jobExplorer to check the job repository for previous executions
99
- * @deprecated This constructor is deprecated in favor of
100
- * {@link JobLauncherCommandLineRunner#JobLauncherCommandLineRunner(JobLauncher, JobExplorer, JobRepository)}.
101
- * A job repository is required to check if a job instance exists with the given
102
- * parameters when running a job (which is not possible with the job explorer). This
103
- * constructor will be removed in a future version.
100
+ * @deprecated since 2.0.7 in favor of
101
+ * {@link #JobLauncherCommandLineRunner(JobLauncher, JobExplorer, JobRepository)}. A
102
+ * job repository is required to check if a job instance exists with the given
103
+ * parameters when running a job (which is not possible with the job explorer).
104
104
*/
105
105
@ Deprecated
106
106
public JobLauncherCommandLineRunner (JobLauncher jobLauncher ,
107
107
JobExplorer jobExplorer ) {
108
108
this .jobLauncher = jobLauncher ;
109
109
this .jobExplorer = jobExplorer ;
110
+ this .jobRepository = null ;
110
111
}
111
112
112
113
/**
@@ -118,6 +119,9 @@ public JobLauncherCommandLineRunner(JobLauncher jobLauncher,
118
119
*/
119
120
public JobLauncherCommandLineRunner (JobLauncher jobLauncher , JobExplorer jobExplorer ,
120
121
JobRepository jobRepository ) {
122
+ Assert .notNull (jobLauncher , "JobLauncher must not be null" );
123
+ Assert .notNull (jobExplorer , "JobExplorer must not be null" );
124
+ Assert .notNull (jobRepository , "JobRepository must not be null" );
121
125
this .jobLauncher = jobLauncher ;
122
126
this .jobExplorer = jobExplorer ;
123
127
this .jobRepository = jobRepository ;
@@ -169,6 +173,20 @@ protected void launchJobFromProperties(Properties properties)
169
173
executeRegisteredJobs (jobParameters );
170
174
}
171
175
176
+ private void executeLocalJobs (JobParameters jobParameters )
177
+ throws JobExecutionException {
178
+ for (Job job : this .jobs ) {
179
+ if (StringUtils .hasText (this .jobNames )) {
180
+ String [] jobsToRun = this .jobNames .split ("," );
181
+ if (!PatternMatchUtils .simpleMatch (jobsToRun , job .getName ())) {
182
+ logger .debug ("Skipped job: " + job .getName ());
183
+ continue ;
184
+ }
185
+ }
186
+ execute (job , jobParameters );
187
+ }
188
+ }
189
+
172
190
private void executeRegisteredJobs (JobParameters jobParameters )
173
191
throws JobExecutionException {
174
192
if (this .jobRegistry != null && StringUtils .hasText (this .jobNames )) {
@@ -192,76 +210,56 @@ protected void execute(Job job, JobParameters jobParameters)
192
210
throws JobExecutionAlreadyRunningException , JobRestartException ,
193
211
JobInstanceAlreadyCompleteException , JobParametersInvalidException ,
194
212
JobParametersNotFoundException {
195
- String jobName = job .getName ();
196
- JobParameters parameters = jobParameters ;
197
- boolean jobInstanceExists = this .jobRepository .isJobInstanceExists (jobName ,
198
- parameters );
199
- if (jobInstanceExists ) {
200
- JobExecution lastJobExecution = this .jobRepository
201
- .getLastJobExecution (jobName , jobParameters );
202
- if (lastJobExecution != null && isStoppedOrFailed (lastJobExecution )
203
- && job .isRestartable ()) {
204
- // Retry a failed or stopped execution with previous parameters
205
- JobParameters previousParameters = lastJobExecution .getJobParameters ();
206
- /*
207
- * remove Non-identifying parameters from the previous execution's
208
- * parameters since there is no way to remove them programmatically. If
209
- * they are required (or need to be modified) on a restart, they need to
210
- * be (re)specified.
211
- */
212
- JobParameters previousIdentifyingParameters = removeNonIdentifying (
213
- previousParameters );
214
- // merge additional parameters with previous ones (overriding those with
215
- // the same key)
216
- parameters = merge (previousIdentifyingParameters , jobParameters );
217
- }
218
- }
219
- else {
220
- JobParametersIncrementer incrementer = job .getJobParametersIncrementer ();
221
- if (incrementer != null ) {
222
- JobParameters nextParameters = new JobParametersBuilder (jobParameters ,
223
- this .jobExplorer ).getNextJobParameters (job ).toJobParameters ();
224
- parameters = merge (nextParameters , jobParameters );
225
- }
226
- }
213
+ JobParameters parameters = getNextJobParameters (job , jobParameters );
227
214
JobExecution execution = this .jobLauncher .run (job , parameters );
228
215
if (this .publisher != null ) {
229
216
this .publisher .publishEvent (new JobExecutionEvent (execution ));
230
217
}
231
218
}
232
219
233
- private void executeLocalJobs (JobParameters jobParameters )
234
- throws JobExecutionException {
235
- for (Job job : this .jobs ) {
236
- if (StringUtils .hasText (this .jobNames )) {
237
- String [] jobsToRun = this .jobNames .split ("," );
238
- if (!PatternMatchUtils .simpleMatch (jobsToRun , job .getName ())) {
239
- logger .debug ("Skipped job: " + job .getName ());
240
- continue ;
241
- }
242
- }
243
- execute (job , jobParameters );
220
+ private JobParameters getNextJobParameters (Job job , JobParameters jobParameters ) {
221
+ if (this .jobRepository != null
222
+ && this .jobRepository .isJobInstanceExists (job .getName (), jobParameters )) {
223
+ return getNextJobParametersForExisting (job , jobParameters );
224
+ }
225
+ if (job .getJobParametersIncrementer () == null ) {
226
+ return jobParameters ;
244
227
}
228
+ JobParameters nextParameters = new JobParametersBuilder (jobParameters ,
229
+ this .jobExplorer ).getNextJobParameters (job ).toJobParameters ();
230
+ return merge (nextParameters , jobParameters );
245
231
}
246
232
247
- private JobParameters removeNonIdentifying (JobParameters parameters ) {
248
- Map <String , JobParameter > parameterMap = parameters .getParameters ();
249
- HashMap <String , JobParameter > copy = new HashMap <>(parameterMap );
250
- for (Map .Entry <String , JobParameter > parameter : copy .entrySet ()) {
251
- if (!parameter .getValue ().isIdentifying ()) {
252
- parameterMap .remove (parameter .getKey ());
253
- }
233
+ private JobParameters getNextJobParametersForExisting (Job job ,
234
+ JobParameters jobParameters ) {
235
+ JobExecution lastExecution = this .jobRepository .getLastJobExecution (job .getName (),
236
+ jobParameters );
237
+ if (isStoppedOrFailed (lastExecution ) && job .isRestartable ()) {
238
+ JobParameters previousIdentifyingParameters = getGetIdentifying (
239
+ lastExecution .getJobParameters ());
240
+ return merge (previousIdentifyingParameters , jobParameters );
254
241
}
255
- return new JobParameters ( parameterMap ) ;
242
+ return jobParameters ;
256
243
}
257
244
258
245
private boolean isStoppedOrFailed (JobExecution execution ) {
259
- BatchStatus status = execution .getStatus ();
246
+ BatchStatus status = ( execution != null ) ? execution .getStatus () : null ;
260
247
return (status == BatchStatus .STOPPED || status == BatchStatus .FAILED );
261
248
}
262
249
250
+ private JobParameters getGetIdentifying (JobParameters parameters ) {
251
+ HashMap <String , JobParameter > nonIdentifying = new LinkedHashMap <>(
252
+ parameters .getParameters ().size ());
253
+ parameters .getParameters ().forEach ((key , value ) -> {
254
+ if (value .isIdentifying ()) {
255
+ nonIdentifying .put (key , value );
256
+ }
257
+ });
258
+ return new JobParameters (nonIdentifying );
259
+ }
260
+
263
261
private JobParameters merge (JobParameters parameters , JobParameters additionals ) {
264
- Map <String , JobParameter > merged = new HashMap <>();
262
+ Map <String , JobParameter > merged = new LinkedHashMap <>();
265
263
merged .putAll (parameters .getParameters ());
266
264
merged .putAll (additionals .getParameters ());
267
265
return new JobParameters (merged );
0 commit comments