Creating A Spring Batch Application: Michael Hoffman
Creating A Spring Batch Application: Michael Hoffman
Michael Hoffman
DEVELOPER AND PLURALSIGHT AUTHOR
@mhi_inc github.com/michaelhoffmantech
dependencies {
Build.gradle
Found in root folder of the project
Demo
DEMOS.md file in project root directory
Demo 2 – Gradle dependencies
@Component
@EnableBatchProcessing
public class BatchConfiguration implements BatchConfigurer {
BatchConfiguration.java
New class to add in the package
com.pluralsight.springbatch.patientbatchloader.config
Batch Configuration Features
JobRepository Batch_Job_Execution_Context
@Autowired
private JobBuilderFactory jobBuilderFactory;
BatchJobConfiguration.java
New class to add in the package
com.pluralsight.springbatch.patientbatchloader.config
Job Configuration Classes
JobBuilderFactory SimpleJobBuilder
Factory for getting the type of Provides a DSL for defining how the
builder required for job job is constructed
configuration
public class BatchJobConfiguration {
@Bean
public Job job(Step step) throws Exception {
return this.jobBuilderFactory
.get(Constants.JOB_NAME)
.validator(validator())
.start(step)
.build();
}
}
BatchJobConfiguration.java
New class to add in the package
com.pluralsight.springbatch.patientbatchloader.config
JobParametersValidator
@Bean
public JobParametersValidator validator() {
return new JobParametersValidator() {
@Override public void validate(JobParameters parameters) throws JobParametersInvalidException {
String fileName = parameters.getString(Constants.JOB_PARAM_FILE_NAME);
if (StringUtils.isBlank(fileName)) { throw new JobParametersInvalidException(…); }
try {
Path file = Paths.get(applicationProperties.getBatch().getInputPath() +
File.separator + fileName);
if (Files.notExists(file) || !Files.isReadable(file)) { throw new Exception(…); }
} catch (Exception e) { … }
}
};
}
BatchJobConfiguration.java
New class to add in the package
com.pluralsight.springbatch.patientbatchloader.config
JobRegistryBeanPostProcessor
Demo
@Autowired
private StepBuilderFactory stepBuilderFactory;
BatchJobConfiguration.java
Package com.pluralsight.springbatch.patientbatchloader.config
Step Configuration
BatchJobConfiguration.java
Package com.pluralsight.springbatch.patientbatchloader.config
Demo
Demo 6 – Spring Batch step
configuration
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PatientBatchLoaderApp.class)
@ActiveProfiles("dev")
public class BatchJobConfigurationTest {
@Autowired
private Job job;
@Test
public void test() {
assertNotNull(job);
assertEquals(Constants.JOB_NAME, job.getName());
}
}
BatchJobConfigurationTest.java
New class in the test folder under the package
com.pluralsight.springbatch.patientbatchloader.config
Demo
Demo 7 – Spring Batch job unit test
configuration
@RestController
@RequestMapping("/job")
public class JobResource {
private final JobLauncher jobLauncher;
private final Job job;
…
}
JobResource.java
New class in the package com.pluralsight.springbatch.patientbatchloader.web.rest
@GetMapping("/{fileName:.+}")
public ResponseEntity<String> runJob(@PathVariable String fileName) {
Map<String, JobParameter> parameterMap = new HashMap<>();
parameterMap.put(Constants.JOB_PARAM_FILE_NAME, new JobParameter(fileName));
try {
jobLauncher.run(job, new JobParameters(parameterMap));
} catch (Exception e) {
return new ResponseEntity<String>("Failure: " + e.getMessage(),
HttpStatus.INTERNAL_SERVER_ERROR);
}
return new ResponseEntity<String>("Success", HttpStatus.OK);
}
JobResource.java
New class in the package com.pluralsight.springbatch.patientbatchloader.web.rest
Demo