Skip to content

Instantly share code, notes, and snippets.

@cataphract
Created February 24, 2016 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cataphract/d4575729e7f57e226f02 to your computer and use it in GitHub Desktop.
Save cataphract/d4575729e7f57e226f02 to your computer and use it in GitHub Desktop.
import org.springframework.batch.core.Job
import org.springframework.batch.core.JobExecution
import org.springframework.batch.core.JobParameters
import org.springframework.batch.core.Step
import org.springframework.batch.core.StepContribution
@Grab('org.springframework.batch:spring-batch-core:3.0.6.RELEASE')
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.launch.JobLauncher
import org.springframework.batch.core.launch.support.SimpleJobLauncher
import org.springframework.batch.core.repository.JobRepository
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.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
@Configuration
@EnableBatchProcessing
class AppConfig {
@Autowired
private JobBuilderFactory jobs
@Autowired
private StepBuilderFactory steps
@Bean
JobLauncher jobLauncher(JobRepository jobRepository) {
new SimpleJobLauncher(
taskExecutor: threadPoolTaskExecutor(),
jobRepository: jobRepository)
}
@Bean
ThreadPoolTaskExecutor threadPoolTaskExecutor() {
new ThreadPoolTaskExecutor(daemon: true)
}
@Bean
public Job job(MyTasklet myTasklet) {
return jobs.get("myJob")
.start(this.steps.get('step1').tasklet(myTasklet).build())
.next(this.steps.get('step2').tasklet(myTasklet).build())
.next(this.steps.get('step3').tasklet(myTasklet).build())
.build()
}
@Bean
MyTasklet myTasklet() {
new MyTasklet()
}
}
class MyTasklet implements Tasklet {
int i
@Override
RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
println 'TEST!'
i++
if (i > 1 && i < 6) {
Thread.sleep(500)
RepeatStatus.CONTINUABLE
} else {
RepeatStatus.FINISHED
}
}
}
def status(JobExecution jobExec) {
println "Status is: ${jobExec.status}, " +
"job execution id ${jobExec.id}"
jobExec.stepExecutions.each {
println " #$it.id $it.stepName $it.status"
}
}
def ctx = new AnnotationConfigApplicationContext(AppConfig)
def launcher = ctx.getBean(JobLauncher)
def jobExecution = launcher.run(ctx.getBean(Job), new JobParameters([:]))
Thread.sleep(2000)
jobExecution.stop()
while (jobExecution.running) {
Thread.sleep 500
}
status jobExecution
jobExecution = launcher.run(ctx.getBean(Job), new JobParameters([:]))
while (jobExecution.running) {
Thread.sleep 500
}
status jobExecution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment