Created
May 2, 2020 12:55
-
-
Save kmb385/ca9fad539a52f6ec059d11a500b26ee5 to your computer and use it in GitHub Desktop.
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
package com.linkedin.batch; | |
import org.springframework.batch.core.Job; | |
import org.springframework.batch.core.Step; | |
import org.springframework.batch.core.StepContribution; | |
import org.springframework.batch.core.StepExecutionListener; | |
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; | |
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | |
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | |
import org.springframework.batch.core.job.builder.FlowBuilder; | |
import org.springframework.batch.core.job.flow.Flow; | |
import org.springframework.batch.core.job.flow.JobExecutionDecider; | |
import org.springframework.batch.core.job.flow.support.SimpleFlow; | |
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.beans.factory.annotation.Autowired; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.core.task.SimpleAsyncTaskExecutor; | |
import org.springframework.core.task.SyncTaskExecutor; | |
@SpringBootApplication | |
@EnableBatchProcessing | |
public class LinkedInBatchApplication { | |
@Autowired | |
public JobBuilderFactory jobBuilderFactory; | |
@Autowired | |
public StepBuilderFactory stepBuilderFactory; | |
@Bean | |
public JobExecutionDecider decider() { | |
return new DeliveryDecider(); | |
} | |
@Bean | |
public JobExecutionDecider receiptDecider() { | |
return new ReceiptDecider(); | |
} | |
@Bean | |
public StepExecutionListener selectFlowerListener() { | |
return new FlowerSelectionStepExecutionListener(); | |
} | |
@Bean | |
public Flow billingFlow() { | |
return new FlowBuilder<SimpleFlow>("billingFlow").start(sendInvoiceStep()).build(); | |
} | |
@Bean | |
public Step nestedBillingJobStep() { | |
return this.stepBuilderFactory.get("nestedBillingJobStep").job(billingJob()).build(); | |
} | |
@Bean | |
public Step sendInvoiceStep() { | |
return this.stepBuilderFactory.get("invoiceStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Invoice is sent to the customer."); | |
return RepeatStatus.FINISHED; | |
} | |
}).build(); | |
} | |
@Bean | |
public Job billingJob() { | |
return this.jobBuilderFactory.get("billingJob").start(sendInvoiceStep()).build(); | |
} | |
@Bean | |
public Flow deliveryFlow() { | |
return new FlowBuilder<SimpleFlow>("deliveryFlow").start(driveToAddressStep()) | |
.on("FAILED").fail() | |
.from(driveToAddressStep()).on("*").to(decider()) | |
.on("PRESENT").to(givePackageToCustomerStep()) | |
.next(receiptDecider()).on("CORRECT").to(thankCustomerStep()) | |
.from(receiptDecider()).on("INCORRECT").to(refundStep()) | |
.from(decider()).on("NOT_PRESENT").to(leaveAtDoorStep()) | |
.build(); | |
} | |
@Bean | |
public Step arrangeFlowersStep() { | |
return this.stepBuilderFactory.get("arrangeFlowersStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Arranging flowers for order."); | |
return RepeatStatus.FINISHED; | |
} | |
}).build(); | |
} | |
@Bean | |
public Step removeThornsStep() { | |
return this.stepBuilderFactory.get("removeThornsStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Remove thorns from roses."); | |
return RepeatStatus.FINISHED; | |
}}).build(); | |
} | |
@Bean | |
public Step selectFlowersStep() { | |
return this.stepBuilderFactory.get("selectFlowersStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Gathering flowers for the order."); | |
return RepeatStatus.FINISHED; | |
}}) | |
.listener(selectFlowerListener()) | |
.build(); | |
} | |
@Bean | |
public Job prepareFlowersJob() { | |
return this.jobBuilderFactory.get("prepareFlowersJob") | |
.start(selectFlowersStep()) | |
.on("TRIM_REQUIRED").to(removeThornsStep()) | |
.next(arrangeFlowersStep()) | |
.from(selectFlowersStep()) | |
.on("NO_TRIM_REQUIRED").to(arrangeFlowersStep()) | |
.from(arrangeFlowersStep()).on("*").to(deliveryFlow()) | |
.end() | |
.build(); | |
} | |
@Bean | |
public Step thankCustomerStep() { | |
return stepBuilderFactory.get("thankCustomerStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Customer has been thanked."); | |
return RepeatStatus.FINISHED; | |
}}).taskExecutor(new SyncTaskExecutor()).build(); | |
} | |
@Bean | |
public Step refundStep() { | |
return stepBuilderFactory.get("refundStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Giving the customer a refund."); | |
return RepeatStatus.FINISHED; | |
}}).taskExecutor(new SyncTaskExecutor()).build(); | |
} | |
@Bean | |
public Step leaveAtDoorStep() { | |
return this.stepBuilderFactory.get("leaveAtDoorStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Leaving the package at the door."); | |
return RepeatStatus.FINISHED; | |
} | |
}).taskExecutor(new SyncTaskExecutor()).build(); | |
} | |
@Bean | |
public Step storePackageStep() { | |
return this.stepBuilderFactory.get("storePackageStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Storing the package while customer address is researched."); | |
return RepeatStatus.FINISHED; | |
} | |
}).build(); | |
} | |
@Bean | |
public Step givePackageToCustomerStep() { | |
return this.stepBuilderFactory.get("givePackageToCustomerStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
System.out.println("Package given to the customer."); | |
return RepeatStatus.FINISHED; | |
} | |
}).taskExecutor(new SyncTaskExecutor()).build(); | |
} | |
@Bean | |
public Step driveToAddressStep() { | |
return this.stepBuilderFactory.get("driveToAddressStep").tasklet(new Tasklet() { | |
boolean GOT_LOST = false; | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
if (GOT_LOST == true) { | |
throw new RuntimeException(); | |
} | |
System.out.println("Successfully arrived at the address."); | |
return RepeatStatus.FINISHED; | |
} | |
}).taskExecutor(new SyncTaskExecutor()).build(); | |
} | |
@Bean | |
public Step packageItemStep() { | |
return this.stepBuilderFactory.get("packageItemStep").tasklet(new Tasklet() { | |
@Override | |
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { | |
String item = chunkContext.getStepContext().getJobParameters().get("item").toString(); | |
String date = chunkContext.getStepContext().getJobParameters().get("run.date").toString(); | |
System.out.println(String.format("The %s has been packaged on %s", item, date)); | |
return RepeatStatus.FINISHED; | |
} | |
}).build(); | |
} | |
@Bean | |
public Job deliverPackageJob() { | |
return this.jobBuilderFactory.get("deliverPackageJob") | |
.start(packageItemStep()) | |
.split(new SimpleAsyncTaskExecutor()) | |
.add(billingFlow(), deliveryFlow()) | |
.end() | |
.build(); | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(LinkedInBatchApplication.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment