Created
February 24, 2016 13:43
-
-
Save cataphract/131635de24e21b1f905e 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
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