Spring Batch๊ฐ 4.x ๋ฒ์ ์ ์ฌ์ฉํ๋ค๊ฐ Spring Boot 3.0๊ณผ ๋ฒ์ ์ ๋ง์ถ๊ฑด์ง 5.0์ผ๋ก ์
๋ฐ์ดํธ๋ฅผ ํ๋ค.
๊ธฐ์กด์ ๋ฐฉ์๋๋ก ์ฌ์ฉํ๋ค๋ฉด 4.x ๋ฒ์ ์ ์ฌ์ฉํ๋ฉด ๋๋ค. ํ์ง๋ง ํ ์ดํ๋ก์ ํธ๋ ์๋ ์๋ก์ด๊ฑธ ๋์ ํด ๋ณด๋ ์ฉ๋๋๊น 5.0์ ์ฌ์ฉํด๋ณด์!
spring:
profiles:
active: ${server.profile:local}
batch:
job:
name: ${job.name:NONE}
logging:
pattern:
console: "[%d{HH:mm:ss.SSS}][%-5level][%logger.%method:line%line] - %msg%n"
level:
root: info
com.juno.hyugibatch: debug
--- # local
spring:
config:
activate:
on-profile: local
h2:
console:
enabled: true
settings:
web-allow-others: true
path: /h2-console
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:hyugi
username: sa
password:
@Configuration
public class DataSourceConfig {
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Bean
public DataSource getDataSource() {
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName(driverClassName);
dataSourceBuilder.url(url);
dataSourceBuilder.username(username);
dataSourceBuilder.password(password);
return dataSourceBuilder.build();
}
@Bean
public PlatformTransactionManager transactionManagerApp(@Qualifier("getDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
package com.juno.hyugibatch.job;
import lombok.extern.slf4j.Slf4j;
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.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
@Slf4j
@Configuration
public class TestJob {
@Bean
public Job testSimpleJob(JobRepository jobRepository, Step testStep){
return new JobBuilder("testSimpleJob", jobRepository)
.start(testStep)
.build();
}
@Bean
public Step testStep(JobRepository jobRepository, Tasklet testTasklet, PlatformTransactionManager platformTransactionManager){
return new StepBuilder("testStep", jobRepository)
.tasklet(testTasklet, platformTransactionManager).build();
}
@Bean
public Tasklet testTasklet(){
return ((contribution, chunkContext) -> {
System.out.println("ํ
์คํธ1");
return RepeatStatus.FINISHED;
});
}
}
package com.juno.hyugibatch.job;
import lombok.extern.slf4j.Slf4j;
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.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
@Slf4j
@Configuration
public class TestJob2 {
@Bean
public Job testSimpleJob2(JobRepository jobRepository, Step testStep2){
return new JobBuilder("testSimpleJob2", jobRepository)
.start(testStep2)
.build();
}
@Bean
public Step testStep2(JobRepository jobRepository, Tasklet testTasklet2, PlatformTransactionManager platformTransactionManager){
return new StepBuilder("testStep2", jobRepository)
.tasklet(testTasklet2, platformTransactionManager).build();
}
@Bean
public Tasklet testTasklet2(){
return ((contribution, chunkContext) -> {
System.out.println("ํ
์คํธ2");
return RepeatStatus.FINISHED;
});
}
}
spring:
batch:
job:
name: ${job.name:NONE}
๋ค์๊ณผ ๊ฐ์ด ์ค์ ํด์ฃผ๋ฉด
java -jar --job.name=[job ์ด๋ฆ]
๋ช ๋ น์ด๋ฅผ ํตํด job ์ด๋ฆ์ ๋ฐ๋ผ ์คํํด์ค ์ ์๋ค.
์๋ ํ์ธ์ ์คํธ๋
์ ๋ ์ด๊ฑธ ๋ด๋ ๋ชจ๋ฅด๊ฒ ๋๋ฐ ... ํน์ ๊ฐ์ธ ์์ ํ์ github ์ฃผ์ ๊ณต๊ฐ ๊ฐ๋ฅํ ๊น์?