diff --git a/pom.xml b/pom.xml index 38d49b1..d040688 100644 --- a/pom.xml +++ b/pom.xml @@ -14,23 +14,22 @@ spring-batch-lecture Demo project for Spring Boot - 11 + 1.8 org.springframework.boot spring-boot-starter-batch - mysql mysql-connector-java runtime - org.springframework.boot - spring-boot-configuration-processor - true + com.h2database + h2 + runtime org.projectlombok @@ -47,6 +46,10 @@ spring-batch-test test + + org.springframework.boot + spring-boot-starter-web + diff --git a/src/batch.http b/src/batch.http new file mode 100644 index 0000000..a243bed --- /dev/null +++ b/src/batch.http @@ -0,0 +1,7 @@ +### Send POST request with json body +POST http://localhost:8080/batch +Content-Type: application/json + +{ + "id": "leaven" +} \ No newline at end of file diff --git a/src/main/java/io/springbatch/springbatchlecture/JobLauncherConfiguration.java b/src/main/java/io/springbatch/springbatchlecture/JobLauncherConfiguration.java new file mode 100644 index 0000000..0f86bef --- /dev/null +++ b/src/main/java/io/springbatch/springbatchlecture/JobLauncherConfiguration.java @@ -0,0 +1,50 @@ +package io.springbatch.springbatchlecture; + +import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.launch.support.RunIdIncrementer; +import org.springframework.batch.core.scope.context.ChunkContext; +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; + +@RequiredArgsConstructor +@Configuration +public class JobLauncherConfiguration { + + private final JobBuilderFactory jobBuilderFactory; + private final StepBuilderFactory stepBuilderFactory; + + @Bean + public Job BatchJob() { + return this.jobBuilderFactory.get("Job") + .start(step1()) + .next(step2()) + .incrementer(new RunIdIncrementer()) + .build(); + } + + @Bean + public Step step1() { + return stepBuilderFactory.get("step1") + .tasklet(new Tasklet() { + @Override + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { + Thread.sleep(3000); + return RepeatStatus.FINISHED; + } + }) + .build(); + } + @Bean + public Step step2() { + return stepBuilderFactory.get("step2") + .tasklet((contribution, chunkContext) -> null) + .build(); + } +} diff --git a/src/main/java/io/springbatch/springbatchlecture/JobLaunchingController.java b/src/main/java/io/springbatch/springbatchlecture/JobLaunchingController.java new file mode 100644 index 0000000..03fee76 --- /dev/null +++ b/src/main/java/io/springbatch/springbatchlecture/JobLaunchingController.java @@ -0,0 +1,61 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.springbatch.springbatchlecture; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer; +import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; + +@RestController +public class JobLaunchingController { + + @Autowired + private Job job; + + @Autowired + private JobLauncher simpleLauncher; + + @Autowired + private BasicBatchConfigurer basicBatchConfigurer; + + @PostMapping(value = "/batch") + public String launch(@RequestBody Member member) throws Exception { + + JobParameters jobParameters = new JobParametersBuilder() + .addString("id", member.getId()) + .addDate("date", new Date()) + .toJobParameters(); + +// SimpleJobLauncher jobLauncher = (SimpleJobLauncher)simpleLauncher; + SimpleJobLauncher jobLauncher = (SimpleJobLauncher)basicBatchConfigurer.getJobLauncher(); + jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor()); + jobLauncher.run(job, jobParameters); + + System.out.println("Job is completed"); + + return "batch completed"; + } +} diff --git a/src/main/java/io/springbatch/springbatchlecture/Member.java b/src/main/java/io/springbatch/springbatchlecture/Member.java new file mode 100644 index 0000000..8522109 --- /dev/null +++ b/src/main/java/io/springbatch/springbatchlecture/Member.java @@ -0,0 +1,9 @@ +package io.springbatch.springbatchlecture; + +import lombok.Data; + +@Data +public class Member { + private String id; + +} diff --git a/src/main/java/io/springbatch/springbatchlecture/SpringBatchLectureApplication.java b/src/main/java/io/springbatch/springbatchlecture/SpringBatchLectureApplication.java index 0d8b2d4..8930a69 100644 --- a/src/main/java/io/springbatch/springbatchlecture/SpringBatchLectureApplication.java +++ b/src/main/java/io/springbatch/springbatchlecture/SpringBatchLectureApplication.java @@ -1,9 +1,11 @@ package io.springbatch.springbatchlecture; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@EnableBatchProcessing public class SpringBatchLectureApplication { public static void main(String[] args) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..e3fbc5a --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,36 @@ +spring: + profiles: + active: local + batch: + job: + enabled: false + +--- +spring: + config: + activate: + on-profile: local + datasource: + hikari: + jdbc-url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE + username: sa + password: + driver-class-name: org.h2.Driver + batch: + jdbc: + initialize-schema: embedded + +--- +spring: + config: + activate: + on-profile: mysql + datasource: + hikari: + jdbc-url: jdbc:mysql://localhost:3306/springbatch?useUnicode=true&characterEncoding=utf8 + username: root + password: pass + driver-class-name: com.mysql.jdbc.Driver + batch: + jdbc: + initialize-schema: always \ No newline at end of file