From c9a8cb9341e6b5247d153c4e6a16a4d5008806f9 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 25 Apr 2013 09:09:46 +0100 Subject: [PATCH] [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] --- spring-bootstrap-cli/samples/job.groovy | 88 +++++++++++++++++++ .../cli/compiler/GroovyCompiler.java | 2 + .../SpringBatchCompilerAutoConfiguration.java | 5 +- ...ingBootstrapCompilerAutoConfiguration.java | 5 +- 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 spring-bootstrap-cli/samples/job.groovy diff --git a/spring-bootstrap-cli/samples/job.groovy b/spring-bootstrap-cli/samples/job.groovy new file mode 100644 index 0000000000..6c917d764b --- /dev/null +++ b/spring-bootstrap-cli/samples/job.groovy @@ -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) + } +} + diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java index bfd76d94c9..b553956c27 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/GroovyCompiler.java @@ -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; diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java index 54aaa9cc89..1dd8d43d9e 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBatchCompilerAutoConfiguration.java @@ -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"); } } diff --git a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java index 8dc1dcf0a1..aeb5af56b1 100644 --- a/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java +++ b/spring-bootstrap-cli/src/main/java/org/springframework/bootstrap/cli/compiler/autoconfigure/SpringBootstrapCompilerAutoConfiguration.java @@ -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"); }