spring batch : batch api

This commit is contained in:
haerong22
2021-05-13 16:43:35 +09:00
parent 85f35be99b
commit 8cb96474f7
5 changed files with 50 additions and 12 deletions

View File

@@ -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'

View File

@@ -0,0 +1 @@
http://localhost:8080//jobs/create-articles

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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());
}
}