December 23, 2019
Read more ...
It has been observed that the key_value under metadata does not accept space. It should be Alphanumeric characters only. Though, Empty values are allowed.
@Configuration
public class BatchConfiguration {
@Bean
public JobLauncher jobLauncher(JobRepository jobRepo) {
SimpleJobLauncher simpleJobLauncher = new SimpleJobLauncher();
simpleJobLauncher.setJobRepository(jobRepo);
return simpleJobLauncher;
}
@Bean
public Job departmentProcessingJob() {
return jobBuilderFactory.get("departmentProcessingJob")
.flow(step1())
.end()
.build();
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Employee, Employee>chunk(1)
.reader(reader())
.processor(processor())
.writer(writer())
.build();
}
@Bean
public Job job2() {
return this.jobBuilderFactory.get("job2")
.start(this.stepBuilderFactory.get("job2step1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute
(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
logger.info("Job2 was run");
return RepeatStatus.FINISHED;
}
})
.build())
.build();
}
}
Let's discuss first job departmentProcessingJob in little more detail. This job has step1 which
does read the file, process it and then print it.
public class DepartmentReader {
public FlatFileItemReader<Employee> reader() {
FlatFileItemReader<Employee> reader = new FlatFileItemReader<Employee>();
reader.setResource(new ClassPathResource("employee_data.txt"));
reader.setLineMapper(new DefaultLineMapper<Employee>() {{
setLineTokenizer(new DelimitedLineTokenizer() {{
setNames(new String[]{"id", "employeenumber", "salary"});
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<Employee>() {{
setTargetType(Employee.class);
}});
}});
return reader;
}
}
public class DepartmentProcessor implements ItemProcessor<Employee, Employee> {
@Override
public Employee process(Employee item) throws Exception {
if ("1001".equalsIgnoreCase(item.getEmployeeNumber())) {
item.setDepartment("Sales");
} else if ("1002".equalsIgnoreCase(item.getEmployeeNumber())) {
item.setDepartment("IT");
} else {
item.setDepartment("Staff");
}
System.out.println("Employee Details --> " + item.toString());
return item;
}
}
public class DepartmentWriter implements ItemWriter<Employee> {
@Override
public void write(List<? extends Employee> items) throws Exception {
List<String> employeeList = new ArrayList<>();
items.forEach(item -> {
String enrichedTxn = String.join(",", item.getId(),
item.getEmployeeNumber(), item.getSalary(),
item.getDepartment());
employeeList.add(enrichedTxn);
});
employeeList.forEach(System.out::println);
}
}
@Component
public class BatchCommandLineRunner implements CommandLineRunner {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job departmentProcessingJob;4.
public void run(String... args) throws Exception {
JobParameters param = new JobParametersBuilder()
.addString("JobID", String.valueOf(System.currentTimeMillis()))
.toJobParameters(); jobLauncher.run(departmentProcessingJob, param);
jobLauncher.run(job2, param);
}
}
@SpringBootApplication
@EnableTask
@EnableBatchProcessing
public class EmployeeProcessingBatch {
public static void main(String[] args) {
SpringApplication.run(EmployeeProcessingBatch.class, args);
}
}
@Configuration
public class SCTJobConfiguration {
private static final Log logger = LogFactory.getLog(SCTJobConfiguration.class);
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Bean
public Job job1() {
return this.jobBuilderFactory.get("job1")
.start(this.stepBuilderFactory.get("job1step1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute
(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
logger.info("Job1 ran successfully");
return RepeatStatus.FINISHED;
}
})
.build())
.build();
}
@Bean
public Job job2() {
return this.jobBuilderFactory.get("job2")
.start(this.stepBuilderFactory.get("job2step1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute
(StepContribution contribution, ChunkContext chunkContext)
throws Exception {
logger.info("Job2 ran successfully");
return RepeatStatus.FINISHED;
}
})
.build())
.build();
}
}