Files
spring-boot-rest/spring-batch/src/main/java/org/baeldung/batch/App.java
Shubhra Srivastava 9c56746eda BAEL-3298 Spring batch retry job (#8690)
* BAEL-3298 Spring batch retry job

* BAEL-3298 Adding tests and refactoring

* BAEL-3298 Some more refactoring

* Review comments

* Some refactoring

* Refactoring RetryItemProcessor

* Minor refactoring in test class

* BAEL-3298 Some more refactoring

* BAEL-3298 Using @MockBean

* BAEL-3298 minor update

* BAEL-3298 Updating names of testcases

* updating id to 9999

* Updating id to 9999

* Updating id to 9999
2020-02-22 10:20:14 +00:00

51 lines
2.0 KiB
Java

package org.baeldung.batch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static void main(final String[] args) {
// Spring Java config
final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(SpringConfig.class);
context.register(SpringBatchConfig.class);
context.register(SpringBatchRetryConfig.class);
context.refresh();
// Spring xml config
// ApplicationContext context = new ClassPathXmlApplicationContext("spring-batch.xml");
runJob(context, "firstBatchJob");
runJob(context, "skippingBatchJob");
runJob(context, "skipPolicyBatchJob");
runJob(context, "retryBatchJob");
}
private static void runJob(AnnotationConfigApplicationContext context, String batchJobName) {
final JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
final Job job = (Job) context.getBean(batchJobName);
LOGGER.info("Starting the batch job: {}", batchJobName);
try {
// To enable multiple execution of a job with the same parameters
JobParameters jobParameters = new JobParametersBuilder().addString("jobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters();
final JobExecution execution = jobLauncher.run(job, jobParameters);
LOGGER.info("Job Status : {}", execution.getStatus());
} catch (final Exception e) {
e.printStackTrace();
LOGGER.error("Job failed {}", e.getMessage());
}
}
}