(Spring boot + Spring Batch + tasklet +JobParameter) How to pass jobParameter to Tasklet
I tried to pass jobParameter which user(anonymous) would set using tasklet(not itemReader or itemWriter, there are a lots of examples about how to pass jobParameter using itemReader & itemWriter) but I encountered below error code.
error code “no job configuration with the name was registered”
Then I found out how to fix it.
just Add the below code in your batch configuration code. The below code will register your job name. after then they can read job parameter in tasklet
@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor( JobRegistry jobRegistry) {
JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
return jobRegistryBeanPostProcessor;
}
my whole BatchConfiguration code is like
@PropertySource("classpath:application.yml")
@AutoConfigureAfter(value = { BatchAutoConfiguration.class })
@EnableBatchProcessing
@Configuration
public class SeleniumBatchConfiguration {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
return jobRegistryBeanPostProcessor;
}
@Bean
public Job job() {
return jobBuilderFactory.get("your job name") .start(requestForSignUpSMT3rdPartyStep()).build();
}
@Bean
public Step step() {
return stepBuilderFactory.get("your step name")
.allowStartIfComplete(true)
.tasklet(new RequestForSignUpSMT3rdPartyTasklet())
.build();
}
(sorry that my code look bad in blog)
Set jobParameter using jobParameterBuilder
It is just example. I use this jobParameter to run job using JobLauncher.
User user = new User();
user.setComment("This is Comment");
user.setEmail("sooyoung32@gmail.com");
user.setFirstName("NARASIMHAN");
user.setLastName("NARASIMHAN");
user.setEsiid("10443720003427889");
JobParameters parameters = new JobParametersBuilder()
.addDate("date", new Date()) //date will ba a key for jobInstance
.addString("firstName", user.getFirstName())
.addString("lastName", user.getLastName())
.addString("esiid", user.getEsiid())
.addString("comment", user.getComment())
.addString("email", user.getEmail())
.toJobParameters();
how to get JobParameter(which we set above) in Tasklek
When you make tasklet class you should implement Tasklet. Then you must implement “execute” method that have SetpContribution and ChunkContext as parameters. We use ChunkContext to get jobParameter.
String date = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("date");
String email = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("email");
String firstName = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("firstName");
String lastName = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("lastName");
String esiid = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("esiid");
String comment = chunkContext.getStepContext().getStepExecution().getJobParameters().getString("comment");
댓글 없음:
댓글 쓰기