728x90
반응형
전체 코드 입니다.
package com.rsn.dic.RsnDictionaryBatch.datamailsend;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Date;
@Configuration
@EnableBatchProcessing
public class DataMailSendConfig extends DefaultBatchConfiguration {
@Bean
public CommandLineRunner commandLineRunner(JobLauncher jobLauncher, Job job) {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
System.out.println("Job Started !!");
JobParametersBuilder paramsBuilder = new JobParametersBuilder();
paramsBuilder.addString("currentTime", String.valueOf(new Date()));
JobParameters parameters = paramsBuilder.toJobParameters();
JobExecution jobExecution = jobLauncher.run(job, parameters);
System.out.println("Job Status : " + jobExecution.getStatus());
}
};
}
@Bean
public Job job(JobRepository jobRepository, PlatformTransactionManager transactionManager){
return new JobBuilder("job", jobRepository)
.start(step(jobRepository, transactionManager))
.build();
}
@Bean
public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager){
return new StepBuilder("step",jobRepository)
.<String, String>chunk(10, transactionManager)
.reader(itemReader())
.writer(itemWriter())
.build();
}
private int cnt = 0;
@Bean
public ItemReader<String> itemReader(){
return new ItemReader<String>() {
@Override
public String read() throws Exception{
if (cnt < 10) {
cnt++;
return "Read OK " + cnt;
} else {
return null;
}
}
};
}
@Bean
public ItemWriter<String> itemWriter(){
return strList -> {
strList.forEach(
str -> System.out.println(str)
);
};
}
@Override
protected Charset getCharset() {
return StandardCharsets.UTF_8;
}
}
1. JobParameters 의 값이 같은 값이 들어가게 설정한다면 ?
- could not increment id for batch_job_seq sequence table 에러가 발생합니다.
2. test 지만 itemReader 에서 계속 " Read OK " 라는 문자열을 반환하기 때문에 cnt 변수 이용하지 않으면 무한반복입니다.
- DataBase 연결해서 데이터 진짜 조회할 때는 cnt 필요 없습니다.
728x90
반응형