From 3e2e86e8703e15c2623195b76f08d5331d685047 Mon Sep 17 00:00:00 2001 From: kimjihun Date: Wed, 17 Jul 2024 07:07:46 +0900 Subject: [PATCH] * job launcher register --- .../controller/MainController.java | 41 +++++++++++++++++++ src/main/resources/application.properties | 4 +- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/springbatch/controller/MainController.java b/src/main/java/com/example/springbatch/controller/MainController.java index 03cf7d1..cc80969 100644 --- a/src/main/java/com/example/springbatch/controller/MainController.java +++ b/src/main/java/com/example/springbatch/controller/MainController.java @@ -1,4 +1,45 @@ package com.example.springbatch.controller; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.JobParametersInvalidException; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; +import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; +import org.springframework.batch.core.repository.JobRestartException; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@ResponseBody public class MainController { + + private final JobLauncher jobLauncher; + private final Job jobFirst; + + public MainController(JobLauncher jobLauncher, Job jobFirst) { + this.jobLauncher = jobLauncher; + this.jobFirst = jobFirst; + } + + @GetMapping("/") + public String mainApi(@RequestParam("value") String value) { + + JobParameters jobParameters = new JobParametersBuilder() + .addString("value", value) + .toJobParameters(); + + try { + jobLauncher.run(jobFirst, jobParameters); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return "ok"; + } + + //https://docs.spring.io/spring-batch/reference/job/configuring-launcher.html } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 1761425..06cb2eb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -13,7 +13,7 @@ spring.datasource-data.username=user1 spring.datasource-data.password=vmfhaltmskdls123 -spring.batch.job.enabled=true -spring.batch.job.name=firstJob +spring.batch.job.enabled=false +#spring.batch.job.name=firstJob spring.batch.jdbc.initialize-schema=always