Skip to content

Instantly share code, notes, and snippets.

@cataphract
Created February 24, 2016 13:43
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/131635de24e21b1f905e to your computer and use it in GitHub Desktop.
Save cataphract/131635de24e21b1f905e to your computer and use it in GitHub Desktop.
import org.springframework.batch.core.*
@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.JobScope
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory
import org.springframework.batch.core.launch.JobLauncher
import org.springframework.batch.item.ItemWriter
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.AnnotationConfigApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
@EnableBatchProcessing
class AppConfig {
@Autowired
ApplicationContext applicationContext
@Autowired
private JobBuilderFactory jobs
@Autowired
private StepBuilderFactory steps
@Bean
Job job() {
jobs.get("myJob").start(step1(null)).build()
}
@Bean
@JobScope
Step step1(@Value('#{jobParameters["commitInterval"]}') commitInterval) {
steps.get('step1')
.chunk((int) commitInterval)
.reader(new IterableItemReader(iterable: [1, 2, 3, 4], name: 'foo'))
.writer(writer(null))
.build()
}
@Bean
@JobScope
ItemWriter writer(@Value('#{jobParameters["writerClass"]}') writerClass) {
applicationContext.classLoader.loadClass(writerClass).newInstance()
}
}
class IterableItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> {
Iterable<T> iterable
private Iterator<T> iterator
@Override
protected T doRead() throws Exception {
if (iterator.hasNext()) {
iterator.next()
} // else null
}
@Override
protected void doOpen() throws Exception {
iterator = iterable.iterator()
}
@Override
protected void doClose() throws Exception {
if (iterator instanceof Closeable) {
((Closeable) iterator).close()
}
}
}
class MyWriter implements ItemWriter<Integer> {
@Override
void write(List<? extends Integer> items) throws Exception {
println "Write $items"
}
}
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([
commitInterval: new JobParameter(3),
writerClass: new JobParameter('MyWriter'),
]))
status jobExecution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment