BAEL-3299 - Testing a Spring Batch Job (#7982)

* BAEL-3299: First version. Broken tests

* BAEL-3299: Fix tests

* BAEL-3299: Include gitignore for output files

* BAEL-3299: Example of writer unit test

* BAEL-3299: Cleaned up and included more tests

* BAEL-3299: Updated to use JobParameters

* BAEL-3299: Fixed broken startup and included cleanup for tests

* BAEL-3299: Fine tuned version. Fixed formatting.

* BAEL-3299: Cleaned up redundant stuff

* BAEL-3299: Fixed formatting

* BAEL-3299: Moved source code in spring-batch module

* BAEL-3299: Fixed broken tests
This commit is contained in:
Sorin Zamfir
2019-10-15 05:32:04 +03:00
committed by maibin
parent 3e4c964e6a
commit 9199d0c895
25 changed files with 750 additions and 32 deletions

View File

@@ -1,5 +1,7 @@
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;
@@ -8,6 +10,9 @@ 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();
@@ -27,19 +32,16 @@ public class App {
final JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
final Job job = (Job) context.getBean(batchJobName);
System.out.println("----------------------------------------");
System.out.println("Starting the batch job: " + 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();
// 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);
System.out.println("Job Status : " + execution.getStatus());
System.out.println("Job succeeded");
LOGGER.info("Job Status : {}", execution.getStatus());
} catch (final Exception e) {
e.printStackTrace();
System.out.println("Job failed");
LOGGER.error("Job failed {}", e.getMessage());
}
}
}