23 Haziran 2020 Salı

SpringBatch SimpleJobLauncher Sınıfı - Job Nesnesini Belirtilen JobParameters İle Çalıştırır

Giriş
run() metodu aracılığıyla belirtilen Job nesnesini belirtilen JobParameters ile çalıştırır.

Tanımlama
Örnek
Şöyle yaparız.
<bean id="jobLauncher"
     class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor">
        <bean class="org.springframework.core.task.SimpleAsyncTaskExecutor" />
    </property>
</bean>
Örnek
Şöyle yaparız.
@Bean
public JobLauncher simpleJobLauncher() throws Exception {
  SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
  ...
  return jobLauncher;
}
Kullanım
Örnek
Elimizde bir tane Job bean olsun. Job nesnesi normalde bean olmak zorunda değil.
@Bean
public Step step1() {
  return stepBuilderFactory.get("step1")
    .<Customer, Customer> chunk(1000)
    .reader(customerItemReader())
    .writer(customerItemWriter())
    .build();
}


@Bean
public Job job() {
  return jobBuilderFactory.get("job")
    .start(step1())
    .build();
}  
Şöyle yaparız
@EnableBatchProcessing
public class SpringBatchApplication implements CommandLineRunner {

  @Autowired
  private JobLauncher jobLauncher;

  @Autowired
  private Job job;

  public static void main(String[] args) {
    SpringApplication.run(SpringBatchApplication.class, args);
  }

  @Override
  public void run(String... args) throws Exception {
    JobParameters jobParameters = new JobParametersBuilder()
      .addString("UUID", UUID.randomUUID().toString())
      .toJobParameters();

    JobExecution execution = jobLauncher.run(job, jobParameters);
    System.out.println("STATUS :: "+execution.getStatus());
   }
}
constructor
Şöyle yaparız.
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
afterPropertiesSet metodu
Şöyle yaparız.
jobLauncher.afterPropertiesSet();
run metodu
Metodun imzası şöyle. JobExecution nesnesi döner.
public JobExecution run(Job job, JobParameters jobParameters)
  throws JobExecutionAlreadyRunningException, JobRestartException,
         JobInstanceAlreadyCompleteException, JobParametersInvalidException
Örnek
Şöyle yaparız
Job job =  ...;
JobParameters jobParameters = ...;
try {
  JobExecution jobExecution = jobLauncher.run(job, jobParameters);
} catch (JobExecutionAlreadyRunningException | JobRestartException
   | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
  ...
}
Örnek
Elimizde şöyle bir kod olsun
JobParametersBuilder paramsBuilder = new JobParametersBuilder();
paramsBuilder.addString("delimiter", "yourCustomDelimiter");
Şöyle yaparız.
jobLauncher.run(job, paramsBuilder.toJobParameters());
Örnek
Controller içinde şöyle yaparız.
@Autowired
JobLauncher jobLauncher;

@RequestMapping(value="/trigger-job", method = RequestMethod.GET)
public Long workHard() throws Exception {
  JobParameters jobParameters = new JobParametersBuilder().
    addLong("time", System.currentTimeMillis())
    .toJobParameters();
  JobExecution jobExecution = jobLauncher.run(myJob, jobParameters);
  ...
}
setJobRepository metodu
Örnek
Şöyle yaparız.
@Configuration
public class BatchConfig {

  @Autowired
  JobRepository jobRepository;


  @Bean
  public JobLauncher simpleJobLauncher() throws Exception {
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository);
    ...
    return jobLauncher;
  }
}
setTaskExecutor metodu
Örnek
Şöyle yaparız.
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
Örnek
Şöyle yaparız.
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
  ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
  taskExecutor.setCorePoolSize(15);
  taskExecutor.setMaxPoolSize(20);
  taskExecutor.setQueueCapacity(30);
  return taskExecutor;
}

@Bean
public JobLauncher jobLauncher(ThreadPoolTaskExecutor taskExecutor,
  JobRepository jobRepository) {
  SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
  jobLauncher.setTaskExecutor(taskExecutor);
  jobLauncher.setJobRepository(jobRepository);
  return jobLauncher;
}


Hiç yorum yok:

Yorum Gönder