From 8cb96474f7ebf00aa51b76624c8a932d593aa9c2 Mon Sep 17 00:00:00 2001 From: haerong22 Date: Thu, 13 May 2021 16:43:35 +0900 Subject: [PATCH] spring batch : batch api --- spring-batch/build.gradle | 2 ++ spring-batch/http/job.http | 1 + .../application/controller/JobController.java | 20 ++++++++++++++++ .../application/scheduler/JobScheduler.java | 16 ++++--------- .../application/service/JobService.java | 23 +++++++++++++++++++ 5 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 spring-batch/http/job.http create mode 100644 spring-batch/src/main/java/com/example/springbatch/application/controller/JobController.java create mode 100644 spring-batch/src/main/java/com/example/springbatch/application/service/JobService.java diff --git a/spring-batch/build.gradle b/spring-batch/build.gradle index c7ea1b13..72b6704f 100644 --- a/spring-batch/build.gradle +++ b/spring-batch/build.gradle @@ -22,6 +22,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-batch' implementation 'org.springframework.boot:spring-boot-starter-data-jdbc' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-web' + compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'mysql:mysql-connector-java' diff --git a/spring-batch/http/job.http b/spring-batch/http/job.http new file mode 100644 index 00000000..d5441cfb --- /dev/null +++ b/spring-batch/http/job.http @@ -0,0 +1 @@ +http://localhost:8080//jobs/create-articles \ No newline at end of file diff --git a/spring-batch/src/main/java/com/example/springbatch/application/controller/JobController.java b/spring-batch/src/main/java/com/example/springbatch/application/controller/JobController.java new file mode 100644 index 00000000..60cefab4 --- /dev/null +++ b/spring-batch/src/main/java/com/example/springbatch/application/controller/JobController.java @@ -0,0 +1,20 @@ +package com.example.springbatch.application.controller; + +import com.example.springbatch.application.service.JobService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/jobs") +@RequiredArgsConstructor +public class JobController { + + private final JobService jobService; + + @GetMapping("/create-articles") + public void runCreateArticleJob() throws Exception { + jobService.runCreateArticleJob(); + } +} diff --git a/spring-batch/src/main/java/com/example/springbatch/application/scheduler/JobScheduler.java b/spring-batch/src/main/java/com/example/springbatch/application/scheduler/JobScheduler.java index 7755d6ec..b00ee310 100644 --- a/spring-batch/src/main/java/com/example/springbatch/application/scheduler/JobScheduler.java +++ b/spring-batch/src/main/java/com/example/springbatch/application/scheduler/JobScheduler.java @@ -1,25 +1,17 @@ package com.example.springbatch.application.scheduler; +import com.example.springbatch.application.service.JobService; import lombok.RequiredArgsConstructor; -import org.springframework.batch.core.Job; -import org.springframework.batch.core.JobParametersBuilder; -import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.annotation.Configuration; -import org.springframework.scheduling.annotation.Scheduled; - -import java.util.Date; @Configuration @RequiredArgsConstructor public class JobScheduler { - private final Job createArticleJob; - private final JobLauncher jobLauncher; + private final JobService jobService; - @Scheduled(fixedDelay = 5000) +// @Scheduled(fixedDelay = 5000) public void runCreateArticleJob() throws Exception{ - jobLauncher.run(createArticleJob, new JobParametersBuilder() - .addDate("date", new Date()) - .toJobParameters()); + jobService.runCreateArticleJob(); } } diff --git a/spring-batch/src/main/java/com/example/springbatch/application/service/JobService.java b/spring-batch/src/main/java/com/example/springbatch/application/service/JobService.java new file mode 100644 index 00000000..92466508 --- /dev/null +++ b/spring-batch/src/main/java/com/example/springbatch/application/service/JobService.java @@ -0,0 +1,23 @@ +package com.example.springbatch.application.service; + +import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.stereotype.Service; + +import java.util.Date; + +@Service +@RequiredArgsConstructor +public class JobService { + + private final Job createArticleJob; + private final JobLauncher jobLauncher; + + public void runCreateArticleJob() throws Exception{ + jobLauncher.run(createArticleJob, new JobParametersBuilder() + .addDate("date", new Date()) + .toJobParameters()); + } +}