[개발환경]

jdk 1.8

spring 2.0


콘트롤러에서 특정 데이터가 변경되면 특정 프로시저를 호출하여 데이터 맞추는 작업이 필요했다.

그래서 쓰레드를 사용하여 작업을 했었는데, 이렇게 하는게 맞는지 약간의 의구심이 들었고 

배치를 사용하되 단발성으로 사용하면 되지 않을까 하는 생각으로 배치를 만들게 되었다.


우선적으로는 정상적으로 잘 동작했다. 

response와 별개로 동작하며, 해당 배치 작업에 대해 몇 개의 batch 테이블들에 내용이 저장된다.



application.properties


# Spring Batch jobs will not run when Spring Boot starts

spring.batch.job.enabled=false

spring.batch.initialize-schema=always



@Slf4j

@Configuration

@EnableBatchProcessing

public class BatchConfig {

 

    @Autowired

    private JobBuilderFactory jobBuilderFactory;

 

    @Autowired

    private StepBuilderFactory stepBuilderFactory;

 

    @Autowired

    private JobRepository jobRepository;

 

    @Autowired

    private DataSource dataSource;

 

    @Bean

    public JobLauncher jobLauncher() throws Exception {

        // async

        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();

        jobLauncher.setJobRepository(jobRepository);

        jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());

        jobLauncher.afterPropertiesSet();

        return jobLauncher;

    }

 

    @Bean(name = "job1")

    public Job job1() {

        return jobBuilderFactory.get("job1").incrementer(new RunIdIncrementer())

                .start(stepExecProc("proc_name1")).build();

    }


    public Step stepExecProc(String procName) {

        return stepBuilderFactory.get("stepExecProc").tasklet((stepContribution, chunkContext) -> {

            log.debug("@@@@ batch call : {}", procName);

 

            JdbcTemplate template = new JdbcTemplate(dataSource);

            SimpleJdbcCall call = new SimpleJdbcCall(template).withProcedureName(procName);

            call.execute();

       

            log.debug("@@@@ batch call finish : {}", procName);

            return RepeatStatus.FINISHED;

        }).build();

    }

 

}

 



@Slf4j

@Controller

@RequestMapping("/admin")

public class AdminController {

 

    @Autowired

    JobLauncher jobLauncher;

   

    @Autowired

    Job job1;

   

    private JobParameters createInitialJobParameterMap() {

        Map<String, JobParameter> m = new HashMap<>();

        m.put("time", new JobParameter(System.currentTimeMillis()));

        JobParameters p = new JobParameters(m);

        return p;

    }

   

    @RequestMapping("/jobPeriodMake")

    @ResponseBody

    String requestJobPeriodMake() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException{

        jobLauncher.run(job1, createInitialJobParameterMap());

        return "Job1!";

    }

}










Posted by motolies
,