* first 배치 정립화

This commit is contained in:
xxxjjhhh
2024-07-27 21:42:13 +09:00
parent 51412d0e09
commit 298616e381
4 changed files with 104 additions and 21 deletions

View File

@@ -2,12 +2,13 @@ package com.example.springbatch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SpringBatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchApplication.class, args);
}
}

View File

@@ -0,0 +1,69 @@
package com.example.springbatch.batch;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import java.util.Arrays;
@Configuration
public class ThirdBatch {
private final JobRepository jobRepository;
private final PlatformTransactionManager platformTransactionManager;
public ThirdBatch(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager) {
this.jobRepository = jobRepository;
this.platformTransactionManager = platformTransactionManager;
}
@Bean
public Job thirdJob() {
System.out.println("third job");
return new JobBuilder("thirdJob", jobRepository)
.start(thirdStep())
.build();
}
@Bean
public Step thirdStep() {
System.out.println("third step");
return new StepBuilder("thirdStep", jobRepository)
.<String, String> chunk(3, platformTransactionManager)
.reader(thirdReader())
.processor(upperProcessor())
.writer(thirdWriter())
.build();
}
@Bean
public ItemReader<String> thirdReader() {
return new ListItemReader<>(Arrays.asList("kim", "lee", "park", "choi", "jeong", "ha", "jo"));
}
@Bean
public ItemProcessor<String, String> upperProcessor() {
return item -> item.toUpperCase();
}
@Bean
public ItemWriter<String> thirdWriter() {
return item -> item.forEach(System.out::println);
}
}

View File

@@ -3,6 +3,7 @@ package com.example.springbatch.controller;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
@@ -16,23 +17,42 @@ public class MainController {
private final JobLauncher jobLauncher;
private final ApplicationContext applicationContext;
private final JobRegistry jobRegistry;
public MainController(JobLauncher jobLauncher, ApplicationContext applicationContext) {
public MainController(JobLauncher jobLauncher, ApplicationContext applicationContext, JobRegistry jobRegistry) {
this.jobLauncher = jobLauncher;
this.applicationContext = applicationContext;
this.jobRegistry = jobRegistry;
}
@GetMapping("/first")
public String mainApi(@RequestParam("value") String value) {
public String firstApi(@RequestParam("value") String value) {
JobParameters jobParameters = new JobParametersBuilder()
.addString("date", value)
.toJobParameters();
Job jobFirst = applicationContext.getBean("firstJob", Job.class);
try {
jobLauncher.run(jobRegistry.getJob("firstJob"), jobParameters);
} catch (Exception e) {
throw new RuntimeException(e);
}
return "ok";
}
@GetMapping("/third")
public String thirdApi(@RequestParam("value") String value) {
System.out.println(value);
JobParameters jobParameters = new JobParametersBuilder()
.addString("date", value)
// .addLong()
.toJobParameters();
try {
jobLauncher.run(jobFirst, jobParameters);
jobLauncher.run(jobRegistry.getJob("thirdJob"), jobParameters);
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@@ -1,14 +1,9 @@
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.configuration.JobRegistry;
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;
@@ -19,18 +14,18 @@ import java.util.Date;
public class FirstSchedule {
private final JobLauncher jobLauncher;
private final ApplicationContext applicationContext;
private final JobRegistry jobRegistry;
//private final ApplicationContext applicationContext;
public FirstSchedule(JobLauncher jobLauncher, ApplicationContext applicationContext) {
public FirstSchedule(JobLauncher jobLauncher, JobRegistry jobRegistry) {
this.jobLauncher = jobLauncher;
this.applicationContext = applicationContext;
this.jobRegistry = jobRegistry;
}
@Scheduled(cron = "10 * * * * *", zone = "Asia/Seoul")
public void runFirstJob() throws JobInstanceAlreadyCompleteException,
JobExecutionAlreadyRunningException,
JobParametersInvalidException,
JobRestartException {
public void runFirstJob() throws Exception {
System.out.println("first schedule start");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
String date = dateFormat.format(new Date());
@@ -39,8 +34,6 @@ public class FirstSchedule {
.addString("date", date)
.toJobParameters();
Job firstJob = applicationContext.getBean("firstJob", Job.class);
jobLauncher.run(firstJob, jobParameters);
jobLauncher.run(jobRegistry.getJob("firstJob"), jobParameters);
}
}