dev-resources.site
for different kinds of informations.
how to run run synchronously two batch in spring batch?
Published at
1/17/2024
Categories
springbatch
springboot
Author
ehaque95
Author
8 person written this
ehaque95
open
Hi sir,
I am Enamul working spring batch 5. In my case Spring batch running in spring schedule. But my deployment server has multiple instances. When it is running, it make double transaction in database. That means it occurs multiple transaction which occurs miss mass of my transaction.
I have two batch. I need processes thousand of transaction using batch.I need to run both batches synchronous. One batch is started it won't get any request until complete. If both batch completed then start again.
Here is my batch configuration:
public class RtgsJob {
public Job createEft(){
String id = UUID.randomUUID().toString();
id = "RTGS-STEP1-"+id;
return new JobBuilder(id, jobRepository)
.incrementer(new RunIdIncrementer())
.start(step1())
.build();
}
public Job createEftTransaction(){
String id = UUID.randomUUID().toString();
id = "RTGS-STEP2-"+id;
return new JobBuilder(id, jobRepository)
.incrementer(new RunIdIncrementer())
.flow(step2())
.end()
.build();
}
private Step step1(){
return new StepBuilder("step1", jobRepository)
.<mydata, mydata> chunk(10, transactionManager)
.reader(myReader)
.processor(myProcessor)
.writer(myWritter)
.build();
}
private Step step2(){
return new StepBuilder("step2", jobRepository)
.<mydata, mydata> chunk(10, transactionManager)
.reader(myReader)
.processor(myProcessor)
.writer(myWritter)
.build();
}
}
Here is my schedule code:
JobExecution execution = null;
JobExecution executionTransaction = null;
if(execution == null) {
// rtgs batch step1 {} execution {} Started:
try {
execution = jobLauncher.run(eftJob.createEft(), new JobParameters());
// rtgs batch {} Step1 existStatus: "+execution.getExitStatus()
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException |
JobParametersInvalidException ex) {
ex.printStackTrace();
}catch (Exception ex) {
ex.printStackTrace();
}
if(execution.getExitStatus().getExitCode().equals("COMPLETED")){
if(executionTransaction == null){
log.info(Constants.batchTag + "{} rtgs batch step2 {} executionTransaction {} Started: ");
try {
executionTransaction = jobLauncher.run(eftJob.createEftTransaction(),new JobParameters());
//{} rtgs batch step2 {} executionTransaction {} Completed;
} catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException |
JobParametersInvalidException ex) {
ex.printStackTrace();
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
}else{
//{} rtgs batch step1 {} execution {} isRunning: ;
}
How to solve the problem?
Please help me..
springbatch Article's
20 articles in total
Spring Batch: Handling Large-Scale Batch Processing
read article
Commencer la programmation avec SPRING BATCH
read article
π οΈ Understanding the Relationship Between chunk() Size and fetchSize in Spring Batch π
read article
Importing CSV Data into PostgreSQL using Spring Batch
read article
Chunk vs Tasklet, which one should I use?
read article
how to run run synchronously two batch in spring batch?
currently reading
DataSource issue in spring Batch 5 - mongodb
read article
A Comparative Analysis of Worker Systems: Spring Batch vs. Java Batch Processing
read article
Monitoring SQL Server Agent Jobs with Spring Batch
read article
Spring Batch Part 2 for Beginners- The Domain Language of SpringΒ Batch
read article
Best practice in SpringBatch
read article
How to use Spring Batch Restartable Function
read article
How to use Spring Batch Late Binding β Step Scope & Job Scope
read article
Spring Batch XML Config by Spring Boot
read article
Spring Batch β Programmatic Flow Decision
read article
Spring Batch Job with Parallel Steps
read article
How to use Intercepting Job Execution in Spring Batch
read article
How to start with Spring Batch using Spring Boot β Java Config
read article
How to import CSV data to PostgreSQL Database using Spring Batch Job
read article
Spring Boot Batch Restart Job Example
read article
Featured ones: