Skip to content

Instantly share code, notes, and snippets.

@cataphract
Last active February 24, 2016 12:26
Show Gist options
  • Save cataphract/c1a3031880557fefb9da to your computer and use it in GitHub Desktop.
Save cataphract/c1a3031880557fefb9da to your computer and use it in GitHub Desktop.
import org.springframework.batch.core.*
import org.springframework.batch.core.configuration.annotation.BatchConfigurer
@Grab('org.springframework.batch:spring-batch-core:3.0.1.RELEASE')
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
@Grab('org.springframework:spring-jdbc:4.0.5.RELEASE')
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory
import org.springframework.batch.core.explore.JobExplorer
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean
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.repository.support.MapJobRepositoryFactoryBean
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.batch.support.transaction.ResourcelessTransactionManager
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
import org.springframework.transaction.PlatformTransactionManager
import sun.nio.ch.ThreadPool
import java.util.concurrent.ThreadPoolExecutor
@Configuration
@EnableBatchProcessing
class AppConfig {
@Autowired
private JobBuilderFactory jobs
@Autowired
private StepBuilderFactory steps
@Bean
public Job job() {
return jobs.get("myJob").start(step1()).build()
}
@Bean
Step step1() {
this.steps.get('step1')
.tasklet(new MyTasklet())
.build()
}
@Bean
BatchConfigurer batchConfigurer() {
final JobLauncher jl = jobLauncher(null)
new BatchConfigurer() {
final PlatformTransactionManager transactionManager =
new ResourcelessTransactionManager()
final MapJobRepositoryFactoryBean mapJobRepositoryFactoryBean =
new MapJobRepositoryFactoryBean(transactionManager).with {
afterPropertiesSet()
it
}
JobRepository getJobRepository() throws Exception {
mapJobRepositoryFactoryBean.object
}
// Groovy 2.3.3 fails with AppConfig.this.jobLauncher(null):
// BUG! exception in phase 'semantic analysis' in source unit
final JobLauncher jobLauncher = jl
JobExplorer getJobExplorer() throws Exception {
new MapJobExplorerFactoryBean(mapJobRepositoryFactoryBean).with {
afterPropertiesSet()
object
}
}
}
}
@Bean
JobLauncher jobLauncher(JobRepository jobRepository) {
new SimpleJobLauncher(
taskExecutor: threadPoolTaskExecutor(),
jobRepository: jobRepository)
}
@Bean
ThreadPoolTaskExecutor threadPoolTaskExecutor() {
new ThreadPoolTaskExecutor(daemon: true)
}
}
class MyTasklet implements Tasklet {
int i = 5
@Override
RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
Thread.sleep 1000
i-- ? RepeatStatus.CONTINUABLE : 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.stepExecutions
.find { it.status.toString() == 'FAILED' }
.each {
println "Step $it.stepName"
it.failureExceptions*.printStackTrace()
}
import org.springframework.batch.core.*
import org.springframework.batch.core.configuration.annotation.BatchConfigurer
@Grab('org.springframework.batch:spring-batch-core:3.0.1.RELEASE')
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing
@Grab('org.springframework:spring-jdbc:4.0.5.RELEASE')
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory
import org.springframework.batch.core.explore.JobExplorer
import org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean
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.repository.support.MapJobRepositoryFactoryBean
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.batch.support.transaction.ResourcelessTransactionManager
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
import org.springframework.transaction.PlatformTransactionManager
@Configuration
@EnableBatchProcessing
class AppConfig {
@Autowired
private JobBuilderFactory jobs
@Autowired
private StepBuilderFactory steps
@Bean
public Job job() {
return jobs.get("myJob").start(step1()).build()
}
@Bean
Step step1() {
this.steps.get('step1')
.tasklet(new MyTasklet())
.build()
}
@Bean
BatchConfigurer batchConfigurer() {
final JobLauncher jl = jobLauncher(null)
new BatchConfigurer() {
final PlatformTransactionManager transactionManager =
new ResourcelessTransactionManager()
final MapJobRepositoryFactoryBean mapJobRepositoryFactoryBean =
new MapJobRepositoryFactoryBean(transactionManager).with {
afterPropertiesSet()
it
}
JobRepository getJobRepository() throws Exception {
mapJobRepositoryFactoryBean.object
}
// Groovy 2.3.3 fails with AppConfig.this.jobLauncher(null):
// BUG! exception in phase 'semantic analysis' in source unit
final JobLauncher jobLauncher = jl
JobExplorer getJobExplorer() throws Exception {
new MapJobExplorerFactoryBean(mapJobRepositoryFactoryBean).with {
afterPropertiesSet()
object
}
}
}
}
@Bean
JobLauncher jobLauncher(JobRepository jobRepository) {
new SimpleJobLauncher(
taskExecutor: threadPoolTaskExecutor(),
jobRepository: jobRepository)
}
@Bean
ThreadPoolTaskExecutor threadPoolTaskExecutor() {
new ThreadPoolTaskExecutor(daemon: true)
}
}
class MyTasklet implements Tasklet {
@Override
RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
throw new RuntimeException('My exception')
}
}
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([:]))
while (jobExecution.running) {
Thread.sleep 500
}
status jobExecution
jobExecution.stepExecutions
.find { it.status.toString() == 'FAILED' }
.each {
println "Step $it.stepName"
it.failureExceptions*.printStackTrace()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment