[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]
This commit is contained in:
Dave Syer 2013-04-25 09:09:46 +01:00
parent 214b3a28db
commit c9a8cb9341
4 changed files with 98 additions and 2 deletions

View File

@ -0,0 +1,88 @@
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)
}
}

View File

@ -31,6 +31,7 @@ import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.bootstrap.cli.compiler.autoconfigure.SpringBatchCompilerAutoConfiguration;
import org.springframework.bootstrap.cli.compiler.autoconfigure.SpringBootstrapCompilerAutoConfiguration;
import org.springframework.bootstrap.cli.compiler.autoconfigure.SpringMvcCompilerAutoConfiguration;
@ -54,6 +55,7 @@ public class GroovyCompiler {
private static final CompilerAutoConfiguration[] COMPILER_AUTO_CONFIGURATIONS = {
new SpringBootstrapCompilerAutoConfiguration(),
new SpringMvcCompilerAutoConfiguration(),
new SpringBatchCompilerAutoConfiguration(),
new SpringBootstrapCompilerAutoConfiguration() };
private GroovyCompilerConfiguration configuration;

View File

@ -38,7 +38,9 @@ public class SpringBatchCompilerAutoConfiguration extends CompilerAutoConfigurat
@Override
public void applyDependencies(DependencyCustomizer dependencies) {
dependencies.ifAnyMissingClasses("org.springframework.batch.core.Job").add(
"org.springframework.batch", "spring-batch-core", "2.1.9.RELEASE");
"org.springframework.batch", "spring-batch-core", "2.2.0.RC1");
dependencies.ifAnyMissingClasses("org.springframework.jdbc.core.JdbcTemplate")
.add("org.springframework", "spring-jdbc", "4.0.0.BOOTSTRAP-SNAPSHOT");
}
@Override
@ -59,6 +61,7 @@ public class SpringBatchCompilerAutoConfiguration extends CompilerAutoConfigurat
"org.springframework.batch.core.JobParameter",
"org.springframework.batch.core.JobParameters",
"org.springframework.batch.core.launch.JobLauncher",
"org.springframework.batch.core.converter.JobParametersConverter",
"org.springframework.batch.core.converter.DefaultJobParametersConverter");
}
}

View File

@ -55,7 +55,8 @@ public class SpringBootstrapCompilerAutoConfiguration extends CompilerAutoConfig
@Override
public void applyImports(ImportCustomizer imports) {
imports.addImports("javax.sql.DataSource",
imports.addImports("javax.sql.DataSource", "javax.annotation.PostConstruct",
"javax.annotation.PreDestroy",
"org.springframework.stereotype.Controller",
"org.springframework.stereotype.Service",
"org.springframework.stereotype.Component",
@ -67,6 +68,8 @@ public class SpringBootstrapCompilerAutoConfiguration extends CompilerAutoConfig
"org.springframework.context.annotation.Scope",
"org.springframework.context.annotation.Configuration",
"org.springframework.context.annotation.Bean",
"org.springframework.core.io.ResourceLoader",
"org.springframework.bootstrap.CommandLineRunner",
"org.springframework.bootstrap.context.annotation.EnableAutoConfiguration");
imports.addStarImports("org.springframework.stereotype");
}