-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
exampleBatch/src/main/java/com/example/examplebatch/part2/SharedConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.example.examplebatch.part2; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.*; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.launch.support.RunIdIncrementer; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
@Profile("mysql") | ||
@Configuration | ||
@Slf4j | ||
public class SharedConfig { | ||
|
||
|
||
|
||
@Bean | ||
public Job shareJob(JobRepository jobRepository, PlatformTransactionManager transactionManager){ | ||
return new JobBuilder("shareJob") | ||
.incrementer(new RunIdIncrementer()) | ||
.repository(jobRepository) | ||
.start(this.step(jobRepository, transactionManager)) | ||
.next(this.step2(jobRepository, transactionManager)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step step(JobRepository jobRepository, PlatformTransactionManager transactionManager){ | ||
return new StepBuilder("step") | ||
.repository(jobRepository) | ||
.transactionManager(transactionManager) | ||
.tasklet((contribution, chunkContext) -> { | ||
|
||
StepExecution stepExecution = contribution.getStepExecution(); | ||
stepExecution.getExecutionContext().putString("stepKey", "step execution context"); | ||
|
||
JobExecution jobExecution = stepExecution.getJobExecution(); | ||
JobInstance jobInstance = jobExecution.getJobInstance(); | ||
jobExecution.getExecutionContext().putString("jobKey", "job execution context"); | ||
|
||
JobParameters jobParameters = jobExecution.getJobParameters(); | ||
jobParameters.getString("run.id"); | ||
|
||
log.info("jobName: {}", jobInstance.getJobName()); | ||
log.info("stepName: {}", stepExecution.getStepName()); | ||
log.info("parameter: {}", jobParameters.getString("run.id")); | ||
|
||
return RepeatStatus.FINISHED; | ||
}) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step step2(JobRepository jobRepository, PlatformTransactionManager transactionManager){ | ||
return new StepBuilder("step2") | ||
.repository(jobRepository) | ||
.transactionManager(transactionManager) | ||
.tasklet((contribution, chunkContext) -> { | ||
|
||
StepExecution stepExecution = contribution.getStepExecution(); | ||
ExecutionContext stepExecutionContext = stepExecution.getExecutionContext(); | ||
|
||
JobExecution jobExecution = stepExecution.getJobExecution(); | ||
ExecutionContext jobExecutionContext = jobExecution.getExecutionContext(); | ||
|
||
log.info("jobKey: {}", jobExecutionContext.getString("jobKey", "emptyJobKey")); | ||
log.info("stepKey: {}", stepExecutionContext.getString("stepKey", "emptyStepKey")); | ||
|
||
|
||
return RepeatStatus.FINISHED; | ||
}) | ||
.build(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
spring: | ||
datasource: | ||
hikari: | ||
jdbc-url: jdbc:mysql://localhost:3306/batchdb?serverTimezone=UTC&characterEncoding=UTF-8 | ||
username: root | ||
password: q1w2e3r4 | ||
driver-class-name: com.mysql.cj.jdbc.Driver | ||
batch: | ||
jdbc: | ||
initialize-schema: never | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ spring: | |
batch: | ||
job: | ||
names: ${JOB_NAME:NONE} | ||
jdbc: | ||
initialize-schema: never | ||
|