인프런 커뮤니티 질문&답변

edu3님의 프로필 이미지
edu3

작성한 질문수

스프링 배치

Hello Spring Batch 시작하기

v5는 많은 것이 바뀌어서 test해보려는데 왜 Tasklet에 있는 System.out.println는 실행되지 않을까요??

작성

·

3.1K

1

v5로 test해보려는데 왜 Tasklet의 System.out.println는 찍히지 않는 걸까요??

package me.victorsung.demobatch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.scope.context.ChunkContext;
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;

@Configuration
public class HelloJobConfiguration extends DefaultBatchConfiguration {

    @Bean
    public Job myJob(JobRepository jobRepository, Step myStep1, Step myStep2) {
        System.out.println("this is job");
        return new JobBuilder("myJob", jobRepository)
                .start(myStep1)
                .next(myStep2)
                .build();
    }

    @Bean
    public Step myStep1(JobRepository jobRepository, Tasklet myTasklet1, PlatformTransactionManager transactionManager) {
        System.out.println("this is step1");
        return new StepBuilder("myStep1", jobRepository)
                .tasklet(myTasklet1, transactionManager)
                .build();
    }

    @Bean
    public Step myStep2(JobRepository jobRepository, Tasklet myTasklet2, PlatformTransactionManager transactionManager) {
        System.out.println("this is step2");
        return new StepBuilder("myStep2", jobRepository)
                .tasklet(myTasklet2, transactionManager)
                .build();
    }

    @Bean
    public Tasklet myTasklet1() {
        System.out.println(
                """
                        this is myTasklet1
                        """
        );
        // Step에서는 Tasklet을 무한 반복 시킨다. 그래서 RepeatStatus을 null || RepeatStatus.FINISHED로 주어야 한번 실행하고 끝난다.
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                System.out.println(
                        """
                                myTasklet1
                                ==============================================
                                >> contribution = %s
                                >> chunkContext = %s
                                ==============================================
                                """.formatted(contribution, chunkContext)
                );
                return RepeatStatus.FINISHED;
            }
        };
    }

    @Bean
    public Tasklet myTasklet2() {
        System.out.println("this is taskLet2");
        // Step에서는 Tasklet을 무한 반복 시킨다. 그래서 RepeatStatus을 null || RepeatStatus.FINISHED로 주어야 한번 실행하고 끝난다.
        return (contribution, chunkContext) -> {
            System.out.println("test2");
            System.out.println(
                    """
                            myTasklet2
                            ==============================================
                            >> contribution = %s
                            >> chunkContext = %s
                            ==============================================
                            """.formatted(contribution, chunkContext)
            );
            return RepeatStatus.FINISHED;
        };
    }
}

 

 

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.0)

2022-12-06T11:11:34.542+09:00  INFO 87250 --- [           main] m.v.demobatch.DemoBatchApplication       : Starting DemoBatchApplication using Java 17.0.4.1 with PID 87250 (/Users/victor/Documents/thecommerce/demo-batch/out/production/classes started by victor in /Users/victor/Documents/thecommerce/demo-batch)
2022-12-06T11:11:34.544+09:00  INFO 87250 --- [           main] m.v.demobatch.DemoBatchApplication       : No active profile set, falling back to 1 default profile: "default"
2022-12-06T11:11:34.671+09:00  INFO 87250 --- [           main] o.s.b.c.c.annotation.BatchRegistrar      : Bean jobRepository already defined in the application context, skipping the registration of a jobRepository
2022-12-06T11:11:34.671+09:00  INFO 87250 --- [           main] o.s.b.c.c.annotation.BatchRegistrar      : Bean jobExplorer already defined in the application context, skipping the registration of a jobExplorer
2022-12-06T11:11:34.671+09:00  INFO 87250 --- [           main] o.s.b.c.c.annotation.BatchRegistrar      : Bean jobLauncher already defined in the application context, skipping the registration of a jobLauncher
2022-12-06T11:11:34.671+09:00  INFO 87250 --- [           main] o.s.b.c.c.annotation.BatchRegistrar      : Bean jobRegistry already defined in the application context, skipping the registration of a jobRegistry
2022-12-06T11:11:34.671+09:00  INFO 87250 --- [           main] o.s.b.c.c.annotation.BatchRegistrar      : Bean jobOperator already defined in the application context, skipping the registration of a jobOperator
2022-12-06T11:11:34.671+09:00  INFO 87250 --- [           main] o.s.b.c.c.annotation.BatchRegistrar      : Finished Spring Batch infrastructure beans configuration in 0 ms.
2022-12-06T11:11:34.784+09:00  WARN 87250 --- [           main] ocalVariableTableParameterNameDiscoverer : Using deprecated '-debug' fallback for parameter name resolution. Compile the affected code with '-parameters' instead or avoid its introspection: me.victorsung.demobatch.HelloJobConfiguration
2022-12-06T11:11:34.811+09:00  INFO 87250 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-12-06T11:11:34.891+09:00  INFO 87250 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:06a2d6ef-e350-45b8-8468-33c43eb10333 user=SA
2022-12-06T11:11:34.891+09:00  INFO 87250 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
this is myTasklet1

this is step1
2022-12-06T11:11:34.922+09:00  INFO 87250 --- [           main] .c.a.BatchObservabilityBeanPostProcessor : No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
this is taskLet2
this is step2
2022-12-06T11:11:34.923+09:00  INFO 87250 --- [           main] .c.a.BatchObservabilityBeanPostProcessor : No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
this is job
2022-12-06T11:11:34.925+09:00  INFO 87250 --- [           main] .c.a.BatchObservabilityBeanPostProcessor : No Micrometer observation registry found, defaulting to ObservationRegistry.NOOP
2022-12-06T11:11:34.985+09:00  INFO 87250 --- [           main] m.v.demobatch.DemoBatchApplication       : Started DemoBatchApplication in 0.659 seconds (process running for 0.901)
2022-12-06T11:11:34.988+09:00  INFO 87250 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-12-06T11:11:34.990+09:00  INFO 87250 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.

Process finished with exit code 0

답변 1

4

edu3님의 프로필 이미지
edu3
질문자

 자체해결입니당
https://github.com/spring-projects/spring-batch/issues/4232

EnableBatchProcessing is no longer required. I tried your sample with Spring Boot 3.0.0-RC2 (Spring Batch 5.0.0-RC2) by removing @EnableBatchProcessing and the sample works as expected.

@Configuration
public class HelloJobConfiguration{

    @Bean
    public DataSource batchDataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
                .addScript("/org/springframework/batch/core/schema-h2.sql")
                .generateUniqueName(true).build();
    }

    @Bean
    public JdbcTransactionManager batchTransactionManager(DataSource dataSource) {
        return new JdbcTransactionManager(dataSource);
    }

    @Bean
    public Job job(JobRepository jobRepository, JdbcTransactionManager transactionManager) {
        return new JobBuilder("job", jobRepository)
                .start(step(jobRepository, transactionManager))
                .build();
    }

    @Bean
    public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder("step", jobRepository)
                .tasklet((StepContribution contribution, ChunkContext chunkContext) -> {
                    System.out.println("Hello, world!");
                    return RepeatStatus.FINISHED;
                }, transactionManager)
                .build();
    }

덕분에 해결했습니다 감사합니다

감사합니다

edu3님의 프로필 이미지
edu3

작성한 질문수

질문하기