spring-boot/spring-bootstrap-cli/samples/job.groovy
Dave Syer c9a8cb9341 [bs-72] Add Batch compiler by default
We could take this out again when we decide what to do about plugins
but it's good to have it in for now because it shows how to do it,
and exposes some holes.  The job.groovy script now works, but
a lot of it should be defaulted through auto-configuration.

(See also [bs-73] for a non-Batch related bug.)

[#48716881]
2013-04-25 09:36:32 +01:00

89 lines
2.1 KiB
Groovy

package org.test
@GrabResolver(name='spring-milestone', root='http://repo.springframework.org/milestone')
@Grab("org.hsqldb:hsqldb-j5:2.0.0")
@Configuration
@EnableBatchProcessing
class JobConfig {
@Autowired
private JobBuilderFactory jobs
@Autowired
private StepBuilderFactory steps
@Bean
protected Tasklet tasklet() {
return new SampleTasklet()
}
@Bean
Job job() throws Exception {
return jobs.get("job").start(step1()).build()
}
@Bean
protected Step step1() throws Exception {
return steps.get("step1").tasklet(tasklet()).build()
}
}
class SampleTasklet implements Tasklet {
@Override
RepeatStatus execute(StepContribution contribution, ChunkContext context) {
return RepeatStatus.FINISHED
}
}
import org.springframework.util.StringUtils
import groovy.util.logging.Log
@Component
@Log
class JobRunner implements CommandLineRunner {
@Autowired(required=false)
private JobParametersConverter converter = new DefaultJobParametersConverter()
@Autowired
private JobLauncher jobLauncher
@Autowired
private Job job
void run(String... args) {
log.info("Running default command line with: ${args}")
launchJobFromProperties(StringUtils.splitArrayElementsIntoProperties(args, "="))
}
protected void launchJobFromProperties(Properties properties) {
jobLauncher.run(job, converter.getJobParameters(properties))
}
}
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
@Component
class DatabaseInitializer {
@Autowired
private DataSource dataSource
@Autowired
private ResourceLoader resourceLoader
@PostConstruct
protected void initialize() {
String platform = org.springframework.batch.support.DatabaseType.fromMetaData(dataSource).toString().toLowerCase()
if (platform=="hsql") {
platform = "hsqldb"
}
ResourceDatabasePopulator populator = new ResourceDatabasePopulator()
populator.addScript(resourceLoader.getResource("org/springframework/batch/core/schema-${platform}.sql"))
populator.setContinueOnError(true)
DatabasePopulatorUtils.execute(populator, dataSource)
}
}