* firstJob에 대한 launcher 등록

This commit is contained in:
kimjihun
2024-07-26 07:19:06 +09:00
parent 807c1b10bb
commit c4a9519100
3 changed files with 68 additions and 19 deletions

View File

@@ -20,18 +20,18 @@ public class SecondBatch {
this.platformTransactionManager = platformTransactionManager; this.platformTransactionManager = platformTransactionManager;
} }
@Bean // @Bean
public Job secondJob() { // public Job secondJob() {
//
return new JobBuilder("secondJob", jobRepository) // return new JobBuilder("secondJob", jobRepository)
.start() // .start()
.build(); // .build();
} // }
//
@Bean // @Bean
public Step secondStep() { // public Step secondStep() {
//
return new StepBuilder("secondStep", jobRepository) // return new StepBuilder("secondStep", jobRepository)
.build(); // .build();
} // }
} }

View File

@@ -4,6 +4,7 @@ import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@@ -14,20 +15,22 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class MainController { public class MainController {
private final JobLauncher jobLauncher; private final JobLauncher jobLauncher;
private final Job jobFirst; private final ApplicationContext applicationContext;
public MainController(JobLauncher jobLauncher, Job jobFirst) { public MainController(JobLauncher jobLauncher, ApplicationContext applicationContext) {
this.jobLauncher = jobLauncher; this.jobLauncher = jobLauncher;
this.jobFirst = jobFirst; this.applicationContext = applicationContext;
} }
@GetMapping("/") @GetMapping("/first")
public String mainApi(@RequestParam("value") String value) { public String mainApi(@RequestParam("value") String value) {
JobParameters jobParameters = new JobParametersBuilder() JobParameters jobParameters = new JobParametersBuilder()
.addString("value", value) .addString("date", value)
.toJobParameters(); .toJobParameters();
Job jobFirst = applicationContext.getBean("firstJob", Job.class);
try { try {
jobLauncher.run(jobFirst, jobParameters); jobLauncher.run(jobFirst, jobParameters);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -0,0 +1,46 @@
package com.example.springbatch.schedule;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.SimpleDateFormat;
import java.util.Date;
@Configuration
public class FirstSchedule {
private final JobLauncher jobLauncher;
private final ApplicationContext applicationContext;
public FirstSchedule(JobLauncher jobLauncher, ApplicationContext applicationContext) {
this.jobLauncher = jobLauncher;
this.applicationContext = applicationContext;
}
@Scheduled(cron = "1 * * * * *", zone = "Asia/Seoul")
public void runFirstJob() throws JobInstanceAlreadyCompleteException,
JobExecutionAlreadyRunningException,
JobParametersInvalidException,
JobRestartException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
String date = dateFormat.format(new Date());
JobParameters jobParameters = new JobParametersBuilder()
.addString("date", date)
.toJobParameters();
Job firstJob = applicationContext.getBean("FirstJob", Job.class);
jobLauncher.run(firstJob, jobParameters);
}
}