BackEnd/Spring Batch
16. ItemReaderAdapter
hanseom
2021. 12. 31. 00:50
반응형
배치 Job 안에서 이미 존재하는 DAO나 다른 서비스를 ItemReader 안에서 사용하고자 할 때 위임 역할을 합니다. setTargetObject에 존재하는 서비스를 setTargetMethod에 호출하려는 메서드명을 설정하면 됩니다.
package io.springbatch.springbatchlecture;
import lombok.RequiredArgsConstructor;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.adapter.ItemReaderAdapter;
import org.springframework.batch.item.database.*;
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
import org.springframework.batch.item.database.builder.JdbcPagingItemReaderBuilder;
import org.springframework.batch.item.database.builder.JpaCursorItemReaderBuilder;
import org.springframework.batch.item.database.builder.JpaPagingItemReaderBuilder;
import org.springframework.batch.item.database.support.SqlPagingQueryProviderFactoryBean;
import org.springframework.batch.item.json.JacksonJsonObjectReader;
import org.springframework.batch.item.json.JsonItemReader;
import org.springframework.batch.item.json.builder.JsonItemReaderBuilder;
import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.batch.item.xml.builder.StaxEventItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.CustomAutowireConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.oxm.xstream.XStreamMarshaller;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.sql.Types;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RequiredArgsConstructor
@Configuration
public class ItemReaderAdapterConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job job() throws Exception {
return jobBuilderFactory.get("batchJob")
.incrementer(new RunIdIncrementer())
.start(step1())
.build();
}
@Bean
public Step step1() throws Exception {
return stepBuilderFactory.get("step1")
.<String,String>chunk(10)
.reader(customItemReader())
.writer(customItemWriter())
.build();
}
@Bean
public ItemReaderAdapter customItemReader() {
ItemReaderAdapter reader = new ItemReaderAdapter();
reader.setTargetObject(customService());
reader.setTargetMethod("joinCustomer");
return reader;
}
private CustomService<String> customService() {
return new CustomService<>();
}
@Bean
public ItemWriter<String> customItemWriter() {
return items -> {
System.out.println(items);
};
}
}
CustomService.java
package io.springbatch.springbatchlecture;
public class CustomService<T> {
private int cnt = 0;
public T joinCustomer(){
return (T)("item" + cnt++);
}
}
[참고자료]
반응형