Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Last active May 24, 2018 20:41
Show Gist options
  • Save fmbenhassine/256b7c6a29e3d0c111e0c0518a53480f to your computer and use it in GitHub Desktop.
Save fmbenhassine/256b7c6a29e3d0c111e0c0518a53480f to your computer and use it in GitHub Desktop.
Example of how to use a custom JobParametersConverter with Spring Batch #SpringBatch
package com.example.helloworldjob;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@SpringBootApplication
@EnableBatchProcessing
@Configuration
public class HelloWorldJobApplication {
@Value("${prefix}")
private String prefix;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public JobParametersConverter jobParametersConverter() {
return new JobParametersConverter() {
@Override
public JobParameters getJobParameters(Properties properties) {
JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
String property = properties.getProperty("--name");
jobParametersBuilder.addString("name", property, true);
return jobParametersBuilder.toJobParameters();
}
@Override
public Properties getProperties(JobParameters jobParameters) {
Properties properties = new Properties();
String name = jobParameters.getString("name");
properties.setProperty("name", name);
return properties;
}
};
}
@Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.tasklet(tasklet())
.build();
}
@Bean
public Tasklet tasklet() {
return (contribution, chunkContext) -> {
String name = (String) chunkContext.getStepContext().getJobParameters().get("name");
System.out.println(prefix + name);
return RepeatStatus.FINISHED;
};
}
@Bean
public Job job() {
return jobBuilderFactory.get("hello-world-job")
.start(step1())
.build();
}
// run with --prefix="hello " --name=world => prints "hello world"
// comment the jobParametersConverter bean and run again => prints "hello null"
public static void main(String[] args) {
SpringApplication.run(HelloWorldJobApplication.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment