-
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
namhyeop
authored and
namhyeop
committed
Jul 23, 2022
1 parent
ec70612
commit 0936043
Showing
19 changed files
with
506 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,7 @@ | ||
### Send POST request with json body | ||
POST http://localhost:8080/batch | ||
Content-Type: application/json | ||
|
||
{ | ||
"id":"leaven" | ||
} |
47 changes: 47 additions & 0 deletions
47
...bLauncher/src/main/java/com/example/springbatch_3_1_3_instance/JobLauncherController.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,47 @@ | ||
package com.example.springbatch_3_1_3_instance; | ||
|
||
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.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Date; | ||
|
||
@RestController | ||
public class JobLauncherController { | ||
@Autowired | ||
private Job job; | ||
|
||
@Autowired | ||
private JobLauncher jobLauncher; | ||
|
||
@Autowired | ||
private BasicBatchConfigurer basicBatchConfigurer; | ||
|
||
@PostMapping("/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) basicBatchConfigurer.getJobLauncher(); | ||
/** | ||
* 아래 주석처리한 방식은 불가능함. Spring이 proxy객체로 생성하기 때문에 타입캐스팅이 불가능함 | ||
*/ | ||
// SimpleJobLauncher jobLauncher = (SimpleJobLauncher) basicBatchConfigurer; | ||
/** | ||
* 비동기 설정 옵션. 기본 값은 동기로 실행된다. | ||
*/ | ||
// jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor()); | ||
jobLauncher.run(job, jobParameters); | ||
return "batch completed"; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...Batch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/Member.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,8 @@ | ||
package com.example.springbatch_3_1_3_instance; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Member { | ||
private String id; | ||
} |
32 changes: 32 additions & 0 deletions
32
...3_JobParameter/src/main/java/com/example/springbatch_3_1_3_instance/JobParameterTest.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,32 @@ | ||
package com.example.springbatch_3_1_3_instance; | ||
|
||
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.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Date; | ||
|
||
@Component | ||
public class JobParameterTest implements ApplicationRunner { | ||
|
||
@Autowired | ||
JobLauncher jobLauncher; | ||
@Autowired | ||
Job job; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
JobParameters jobParameters = new JobParametersBuilder() | ||
.addString("name", "user1") | ||
.addLong("seq", 1L) | ||
.addDate("date", new Date()) | ||
.addDouble("age", 16.5) | ||
.toJobParameters(); | ||
jobLauncher.run(job, jobParameters); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...rc/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet1.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,37 @@ | ||
package com.example.springbatch_3_1_8_ExecutionContext; | ||
|
||
import org.springframework.batch.core.JobInstance; | ||
import org.springframework.batch.core.StepContribution; | ||
import org.springframework.batch.core.scope.context.ChunkContext; | ||
import org.springframework.batch.core.step.tasklet.Tasklet; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ExecutionContextTasklet1 implements Tasklet { | ||
@Override | ||
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | ||
|
||
System.out.println("Step1 was executed"); | ||
ExecutionContext jobExecutionContext = contribution.getStepExecution().getJobExecution().getExecutionContext(); | ||
ExecutionContext stepExecutionContext = contribution.getStepExecution().getExecutionContext(); | ||
|
||
String jobName = chunkContext.getStepContext().getStepExecution().getJobExecution().getJobInstance().getJobName(); | ||
String stepName = chunkContext.getStepContext().getStepExecution().getStepName(); | ||
|
||
//한번도 실행되지 않은 경우 | ||
if (jobExecutionContext.get("jobName") == null) { | ||
jobExecutionContext.put("jobName", jobName); | ||
} | ||
|
||
if (stepExecutionContext.get("stepName") == null) { | ||
stepExecutionContext.put("stepName", stepName); | ||
} | ||
|
||
System.out.println("jobName : " + jobExecutionContext.get("jobName")); | ||
System.out.println("stepName : " + stepExecutionContext.get("stepName")); | ||
|
||
return RepeatStatus.FINISHED; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...rc/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet2.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,30 @@ | ||
package com.example.springbatch_3_1_8_ExecutionContext; | ||
|
||
import org.springframework.batch.core.StepContribution; | ||
import org.springframework.batch.core.scope.context.ChunkContext; | ||
import org.springframework.batch.core.step.tasklet.Tasklet; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class ExecutionContextTasklet2 implements Tasklet { | ||
@Override | ||
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { | ||
System.out.println("Step2 was executed"); | ||
|
||
ExecutionContext jobExecutionContext = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext(); | ||
ExecutionContext stepExecutionContext = chunkContext.getStepContext().getStepExecution().getExecutionContext(); | ||
|
||
System.out.println("jobName = " + jobExecutionContext.get("jobName")); | ||
System.out.println("stepName = " + stepExecutionContext.get("stepName")); | ||
|
||
String stepName = chunkContext.getStepContext().getStepExecution().getStepName(); | ||
|
||
//한번도 실행되지 않은 경우 | ||
if (jobExecutionContext.get("stepName") == null) { | ||
jobExecutionContext.put("stepName", stepName); | ||
} | ||
return RepeatStatus.FINISHED; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...rc/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet3.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,21 @@ | ||
package com.example.springbatch_3_1_8_ExecutionContext; | ||
|
||
import org.springframework.batch.core.StepContribution; | ||
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.stereotype.Component; | ||
|
||
@Component | ||
public class ExecutionContextTasklet3 implements Tasklet { | ||
@Override | ||
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { | ||
System.out.println("Step3 was executed"); | ||
Object name = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("name"); | ||
if (name == null) { | ||
chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("name", "user1"); | ||
throw new RuntimeException("step2 was failed"); | ||
} | ||
return RepeatStatus.FINISHED; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...rc/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet4.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,18 @@ | ||
package com.example.springbatch_3_1_8_ExecutionContext; | ||
|
||
import org.springframework.batch.core.StepContribution; | ||
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.stereotype.Component; | ||
|
||
@Component | ||
public class ExecutionContextTasklet4 implements Tasklet { | ||
@Override | ||
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { | ||
|
||
System.out.println("name : " + chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("name")); | ||
System.out.println("step4 was executed"); | ||
return RepeatStatus.FINISHED; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...epository/src/main/java/com/example/springbatch_3_1_3_instance/CustomBatchConfigurer.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,33 @@ | ||
package com.example.springbatch_3_1_3_instance; | ||
|
||
import org.springframework.batch.core.configuration.annotation.BatchConfigurer; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; | ||
import org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer; | ||
import org.springframework.boot.autoconfigure.batch.BatchProperties; | ||
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.sql.DataSource; | ||
|
||
@Configuration | ||
public class CustomBatchConfigurer extends BasicBatchConfigurer { | ||
private final DataSource dataSource; | ||
|
||
public CustomBatchConfigurer(BatchProperties properties, DataSource dataSource, TransactionManagerCustomizers transactionManagerCustomizers) { | ||
super(properties, dataSource, transactionManagerCustomizers); | ||
this.dataSource = dataSource; | ||
} | ||
|
||
@Override | ||
protected JobRepository createJobRepository() throws Exception { | ||
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); | ||
factory.setDataSource(dataSource); | ||
factory.setTransactionManager(getTransactionManager()); | ||
factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED"); | ||
factory.setTablePrefix("SYSTEM_"); | ||
|
||
return factory.getObject(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...epository/src/main/java/com/example/springbatch_3_1_3_instance/JobRepositoryListener.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,40 @@ | ||
package com.example.springbatch_3_1_3_instance; | ||
|
||
import org.springframework.batch.core.*; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Job이 끝난 경우의 값을 확인하기 위해 JobRepository를 커스터마이징함 | ||
*/ | ||
@Component | ||
public class JobRepositoryListener implements JobExecutionListener { | ||
@Autowired | ||
JobRepository jobRepository; | ||
|
||
@Override | ||
public void beforeJob(JobExecution jobExecution) { | ||
|
||
} | ||
|
||
@Override | ||
public void afterJob(JobExecution jobExecution) { | ||
String jobName = jobExecution.getJobInstance().getJobName(); | ||
JobParameters jobParameters = new JobParametersBuilder().addString("requestDate", "20220714").toJobParameters(); | ||
|
||
JobExecution lastJobExecution = jobRepository.getLastJobExecution(jobName, jobParameters); | ||
if (lastJobExecution != null) { | ||
for (StepExecution execution : lastJobExecution.getStepExecutions()) { | ||
BatchStatus status = execution.getStatus(); | ||
System.out.println("status = " + status); | ||
|
||
ExitStatus exitStatus = execution.getExitStatus(); | ||
System.out.println("exitStatus = " + exitStatus); | ||
|
||
String stepName = execution.getStepName(); | ||
System.out.println("stepName = " + stepName); | ||
} | ||
} | ||
} | ||
} |
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,2 @@ | ||
use | ||
springbatch; |
Oops, something went wrong.