Java/SpringBatch
[SpringBatch] Chunk 기반 실행
고기고기물고기
2023. 9. 14. 17:10
jconn4, log4jdbc-log4j2-jdbc4.1 사용했습니다.
SpringBatchApplication
public class SpringBatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchApplication.class, args);
}
}
config/MybatisConfig
@Configuration
public class MybatisConfig {
@Bean
@BatchDataSource
@Qualifier("datasource")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSourceDB() { return DataSourceBuilder.create().build(); }
@Bean
public SqlSessionFactory dbSqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSession = new SqlSessionFactoryBean();
sqlSession.setDataSource(dataSourceDB());
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resource = resolver.getResources("classpath:Mapper/Mapper.xml*");
sqlSession.setMapperLocation(resource);
return sqlSession.getObject();
}
@Bean(name ="dbSqlSessionTemplate")
public SqlSessionTemplate dbSqlSessionTemplate(@Qualifier("dbSqlSessionFactory")SqlSessionFactory
SqlSessionFactory)
{
return new SqlSessionTemplate(SqlSessionFactory);
}
@Bean
public PlatformTransactionManager dbTranscationManager(){
return new DataSourceTransactionManager(dataSourceKISDB());
}
}
application.yml
spring:
config:
activate:
local:
on-profile:local
datasource:
jdbc-url: jdbc:log4jdbc:sybase:Tds:ip:port
driver-class-name: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
databaseType: sybase
username: id
password: password
spring:
batch:
job:
names: ${job.name:NONE}
jdbc:
initialize-schema: never
mybatis:
mapper-locations: classpath:Mapper/*.xml
Processor/CustomItemProcessor
@Slf4j
@Component
public class CustomItemProcessor implements ItemProcesor<SelectKey, SelectKey> {
@Override
public SelectKey process(SelectKey list) throws Exception {
log.info("실행됨");
return list;
}
}
Reader/CustomItemReader
@Slf4j
public class CustomItemReader implements ItemReader<SelectKey> {
@Autowired
private SpringBatchDAO springBatchDAO;
private List<SelectKey> list;
private Iterator<selectKey> dataIterator;
@Override
public SelectKey read() throws Exceptions, UnexpectedInputException, ParseException,
NonTransientResourceException {
if(dataIteratir == null) {
list = springBatchDao.SearchKey();
dataIterator = list.iterator();
}
if(dataIterator.hasNext()) {
return dataIterator.next();
} else {
return null;
}
}
}
Writer/CustomItemWriter
@Slf4j
@NoArgsConstructor
public class CustomItemWriter implements ItemWriter<SelectKey> {
@Autowired
private SpringBatchDAO springBatchDAO;
@Override
public void write(List<? extends SelectKey> items) throws Exception{
for(SelectKey item : items)
{
if(item.getBizno.length() == 10)
{
....
}
}
}
}
Configuration
@Slf4j
@RequiredArgsConstructor
@Configuration
public class RetryConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final SeptBuilderFactory stepBuilderFactory;
@Bean
public Job job() throws Exception{
return jobBuilderFactory.get("job")
.start(step1())
.build();
}
@Bean
public Step step1() throws Exception{
log.info("step1 실행됨");
return stepBuilderFactory.get("step1")
,<SelectKey, SelectKey>chunk(1) // 청크사이즈
.reader(ItemReader())
.writer(writer())
.build();
}
@Bean
public ItemReader<SelectKey> ItemReader() throws Exception {
return new CustomItemReader();
}
@Bean
public ItemProcessor<SelectKey, SelectKey> processor() {
return new CustomItemProcessor();
}
@Bean
public ItemWriter<? super SelectKey> writer() {
return new CusomItemWriter();
}
}