Skip to content

Commit

Permalink
feat : add spring batch file
Browse files Browse the repository at this point in the history
  • Loading branch information
namhyeop authored and namhyeop committed Jul 23, 2022
1 parent ec70612 commit 0936043
Show file tree
Hide file tree
Showing 19 changed files with 506 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 8.Spring_Batch/.idea/8.Spring_Batch.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions 8.Spring_Batch/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 8.Spring_Batch/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 8.Spring_Batch/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 8.Spring_Batch/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions 8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/batch.http
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"
}
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";
}
}
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;
}
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);
}
}
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;
}
}
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;
}
}
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;
}
}
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;
}
}
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();
}
}
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);
}
}
}
}
2 changes: 2 additions & 0 deletions 8.Spring_Batch/spring-batch/src/DB/Query.SQL
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use
springbatch;
Loading

0 comments on commit 0936043

Please sign in to comment.