Spring Batch를 통해 1~100까지 로그로 출력하기
package batch.practice.job;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j //log 사용을 위한 lombok annotation
@RequiredArgsConstructor //생성자 DI를 위한 lombok annotation
@Configuration //Spring batch의 Job은 @Configuration annotation으로 등록 후 사용
public class SimpleJobConfiguration002 {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
@Bean
public Job simpleJob1() {
log.info("========== START simpleJob");
return jobBuilderFactory.get("simpleJob1") //Batch Job을 생성하고 builder를 통해 이름 지정
.start(simpleStep1()) //execute 할 step or sequence of steps
.build();
};
@Bean
public Step simpleStep1() {
return stepBuilderFactory.get("simpleStep1") //Batch Step을 생성하고 builder를 통해 이름 지정
.tasklet((contribution, chunkContext) -> { //Step 안에서 수행될 기능을 명시
for(int i = 0; i < 100 ; i ++) { // - Tasklet은 Step 안에서 단일로 수행될 커스텀 기능 선언
log.info(">>>>> "+(i+1)+" <<<<<");
}
return RepeatStatus.FINISHED;
})
.build();
};
};
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.5)
2021-04-27 13:42:11.560 INFO 27484 --- [ restartedMain] b.p.BatchExample210427001Application : Starting BatchExample210427001Application using Java 15.0.2 on DESKTOP-C7P9M2O with PID 27484 (-)
2021-04-27 13:42:11.564 INFO 27484 --- [ restartedMain] b.p.BatchExample210427001Application : No active profile set, falling back to default profiles: default
2021-04-27 13:42:11.642 INFO 27484 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-04-27 13:42:12.367 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : ========== START simpleJob
2021-04-27 13:42:12.583 INFO 27484 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-04-27 13:42:12.605 INFO 27484 --- [ restartedMain] b.p.BatchExample210427001Application : Started BatchExample210427001Application in 1.483 seconds (JVM running for 3.024)
2021-04-27 13:42:12.607 INFO 27484 --- [ restartedMain] o.s.b.a.b.JobLauncherApplicationRunner : Running default command line with: []
2021-04-27 13:42:12.608 WARN 27484 --- [ restartedMain] o.s.b.c.c.a.DefaultBatchConfigurer : No datasource was provided...using a Map based JobRepository
2021-04-27 13:42:12.608 WARN 27484 --- [ restartedMain] o.s.b.c.c.a.DefaultBatchConfigurer : No transaction manager was provided, using a ResourcelessTransactionManager
2021-04-27 13:42:12.628 INFO 27484 --- [ restartedMain] o.s.b.c.l.support.SimpleJobLauncher : No TaskExecutor has been set, defaulting to synchronous executor.
2021-04-27 13:42:12.662 INFO 27484 --- [ restartedMain] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=simpleJob1]] launched with the following parameters: [{}]
2021-04-27 13:42:12.700 INFO 27484 --- [ restartedMain] o.s.batch.core.job.SimpleStepHandler : Executing step: [simpleStep1]
2021-04-27 13:42:12.711 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 1 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 2 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 3 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 4 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 5 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 6 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 7 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 8 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 9 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 10 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 11 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 12 <<<<<
2021-04-27 13:42:12.712 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 13 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 14 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 15 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 16 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 17 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 18 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 19 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 20 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 21 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 22 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 23 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 24 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 25 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 26 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 27 <<<<<
2021-04-27 13:42:12.713 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 28 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 29 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 30 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 31 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 32 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 33 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 34 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 35 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 36 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 37 <<<<<
2021-04-27 13:42:12.714 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 38 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 39 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 40 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 41 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 42 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 43 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 44 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 45 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 46 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 47 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 48 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 49 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 50 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 51 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 52 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 53 <<<<<
2021-04-27 13:42:12.715 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 54 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 55 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 56 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 57 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 58 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 59 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 60 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 61 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 62 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 63 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 64 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 65 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 66 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 67 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 68 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 69 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 70 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 71 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 72 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 73 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 74 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 75 <<<<<
2021-04-27 13:42:12.716 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 76 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 77 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 78 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 79 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 80 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 81 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 82 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 83 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 84 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 85 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 86 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 87 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 88 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 89 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 90 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 91 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 92 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 93 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 94 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 95 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 96 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 97 <<<<<
2021-04-27 13:42:12.717 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 98 <<<<<
2021-04-27 13:42:12.718 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 99 <<<<<
2021-04-27 13:42:12.718 INFO 27484 --- [ restartedMain] b.p.job.SimpleJobConfiguration002 : >>>>> 100 <<<<<
2021-04-27 13:42:12.727 INFO 27484 --- [ restartedMain] o.s.batch.core.step.AbstractStep : Step: [simpleStep1] executed in 27ms
2021-04-27 13:42:12.735 INFO 27484 --- [ restartedMain] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=simpleJob1]] completed with the following parameters: [{}] and the following status: [COMPLETED] in 42ms
사실 1개의 step에서 for문을 이용하여 1~100까지 출력하는 방식이 아니라
job이 작동하면 step을 100개 생성해서 각각 1~100까지 출력하게 만들고 싶었다.
이 생각이 틀렸는지 더 공부해서 만약 가능하다면 시도해봐야겠다!!
'SpringBoot' 카테고리의 다른 글
[SpringBoot] Spring Boot Batch 001 (0) | 2021.04.29 |
---|---|
[SpringBoot] Batch study...! (0) | 2021.04.28 |
[MyBatis] update에서 selectKey가 안돼요! :: No setter found for the keyProperty '' in java.lang.String. (0) | 2020.07.01 |
[mybatis] multiple select key (0) | 2020.06.23 |