Skip to content
This repository was archived by the owner on Nov 19, 2021. It is now read-only.

Commit e9f836f

Browse files
committed
Merge branch 'develop' of ssh://github.com/oasp/oasp4j into develop
2 parents 5aceb28 + 9b230b5 commit e9f836f

File tree

7 files changed

+233
-67
lines changed

7 files changed

+233
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package io.oasp.module.batch.common.impl;
2+
3+
import java.util.List;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.batch.core.ItemProcessListener;
8+
import org.springframework.batch.core.ItemReadListener;
9+
import org.springframework.batch.core.ItemWriteListener;
10+
import org.springframework.batch.core.SkipListener;
11+
12+
/**
13+
* Spring Batch listener that logs exceptions together with the item(s) being processed at the time the exceptions
14+
* occurred.
15+
*
16+
* @author Ludger Overbeck
17+
*/
18+
public class ChunkLoggingListener<T, S> implements SkipListener<T, S>, ItemReadListener<T>, ItemProcessListener<T, S>,
19+
ItemWriteListener<S> {
20+
21+
private static final Logger LOG = LoggerFactory.getLogger(ChunkLoggingListener.class);
22+
23+
protected String itemToString(Object item) {
24+
25+
return item.toString();
26+
}
27+
28+
@Override
29+
public void onReadError(Exception e) {
30+
31+
LOG.error("Failed to read item.", e);
32+
}
33+
34+
@Override
35+
public void onProcessError(T item, Exception e) {
36+
37+
LOG.error("Failed to process item: " + itemToString(item), e);
38+
}
39+
40+
@Override
41+
public void onWriteError(Exception e, List<? extends S> items) {
42+
43+
LOG.error("Failed to write items: " + itemToString(items), e);
44+
}
45+
46+
@Override
47+
public void onSkipInRead(Throwable t) {
48+
49+
LOG.warn("Skipped item in read.", t);
50+
}
51+
52+
@Override
53+
public void onSkipInProcess(T item, Throwable t) {
54+
55+
LOG.warn("Skipped item in process: " + itemToString(item), t);
56+
}
57+
58+
@Override
59+
public void onSkipInWrite(S item, Throwable t) {
60+
61+
LOG.warn("Skipped item in write: " + itemToString(item), t);
62+
}
63+
64+
@Override
65+
public void beforeRead() {
66+
67+
}
68+
69+
@Override
70+
public void afterRead(T item) {
71+
72+
}
73+
74+
@Override
75+
public void beforeProcess(T item) {
76+
77+
}
78+
79+
@Override
80+
public void afterProcess(T item, S result) {
81+
82+
}
83+
84+
@Override
85+
public void beforeWrite(List<? extends S> items) {
86+
87+
}
88+
89+
@Override
90+
public void afterWrite(List<? extends S> items) {
91+
92+
}
93+
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.oasp.module.batch.common.impl;
2+
3+
import org.springframework.batch.core.BatchStatus;
4+
import org.springframework.batch.core.Job;
5+
import org.springframework.batch.core.JobExecution;
6+
import org.springframework.batch.core.JobParameters;
7+
import org.springframework.batch.core.JobParametersIncrementer;
8+
import org.springframework.batch.core.JobParametersInvalidException;
9+
import org.springframework.batch.core.launch.JobLauncher;
10+
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
11+
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
12+
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
13+
import org.springframework.batch.core.repository.JobRepository;
14+
import org.springframework.batch.core.repository.JobRestartException;
15+
16+
/**
17+
* {@link JobLauncher} that extends the functionality provided by the standard {@link SimpleJobLauncher}:
18+
* <p>
19+
* For batches, which always restart from scratch (i.e. those marked with restartable="false"), the parameter's are
20+
* 'incremented' automatically using the {@link JobParametersIncrementer} from the job (usually by adding or modifying
21+
* the 'run.id' parameter). It is actually just a convenience functionality so that the one starting batches does not
22+
* have to change the parameters manually.
23+
*
24+
* @author Ludger Overbeck
25+
*/
26+
public class JobLauncherWithAdditionalRestartCapabilities extends SimpleJobLauncher {
27+
28+
private JobRepository jobRepository;
29+
30+
@Override
31+
public JobExecution run(final Job job, JobParameters jobParameters) throws JobExecutionAlreadyRunningException,
32+
JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
33+
34+
if (!job.isRestartable()) {
35+
36+
JobParameters originalParameters = jobParameters;
37+
while (this.jobRepository.isJobInstanceExists(job.getName(), jobParameters)) {
38+
39+
// check if batch job is still running or was completed already
40+
// analogous to SimpleJobRepository#createJobExecution
41+
JobExecution jobExecution = this.jobRepository.getLastJobExecution(job.getName(), jobParameters);
42+
if (jobExecution.isRunning()) {
43+
throw new JobExecutionAlreadyRunningException("A job execution for this job is already running: "
44+
+ jobExecution.getJobInstance());
45+
}
46+
BatchStatus status = jobExecution.getStatus();
47+
if (status == BatchStatus.COMPLETED || status == BatchStatus.ABANDONED) {
48+
throw new JobInstanceAlreadyCompleteException("A job instance already exists and is complete for parameters="
49+
+ originalParameters + ". If you want to run this job again, change the parameters.");
50+
}
51+
52+
// if there is a NullPointerException executing the following statement
53+
// there has not been a JobParametersIncrementer set for the job
54+
jobParameters = job.getJobParametersIncrementer().getNext(jobParameters);
55+
}
56+
}
57+
58+
return super.run(job, jobParameters);
59+
}
60+
61+
@Override
62+
public void setJobRepository(JobRepository jobRepository) {
63+
64+
super.setJobRepository(jobRepository);
65+
this.jobRepository = jobRepository;
66+
}
67+
68+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
<version>2.9.1</version>
179179
</plugin>
180180
<plugin>
181+
181182
<groupId>org.apache.maven.plugins</groupId>
182183
<artifactId>maven-antrun-plugin</artifactId>
183184
<version>1.8</version>

0 commit comments

Comments
 (0)