From bcf86b320e0600dc889aba0287a74d4a48489132 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 26 Apr 2013 07:01:37 +0100 Subject: [PATCH] [bs-72], [bs-75] Add Batch plugin features * Auto config in main bootstrap jar * Compiler enhancements in groovy cli * Sample script in cli samples [Fixes #48716881], [Fixes #48788313] --- pom.xml | 20 ++++++ spring-bootstrap-cli/samples/job.groovy | 51 --------------- .../pom.xml | 4 +- spring-bootstrap/pom.xml | 10 +++ .../batch/BatchAutoConfiguration.java | 48 ++++++++++++++ .../batch/BatchDatabaseInitializer.java | 53 ++++++++++++++++ .../batch/JobLauncherCommandLineRunner.java | 62 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 1 + 8 files changed, 195 insertions(+), 54 deletions(-) create mode 100644 spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/BatchAutoConfiguration.java create mode 100644 spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/BatchDatabaseInitializer.java create mode 100644 spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/JobLauncherCommandLineRunner.java diff --git a/pom.xml b/pom.xml index b2bb687a96..1ffb241282 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,8 @@ UTF-8 4.0.0.BOOTSTRAP-SNAPSHOT 1.0.0.CI-SNAPSHOT + 2.2.3.RELEASE + 2.2.0.RC1 ${project.basedir} @@ -33,6 +35,14 @@ true + + spring-milestones + Spring Milestones + http://maven.springframework.org/milestone + + false + + @@ -356,6 +366,16 @@ spring-tx ${dependency.springframework.version} + + org.springframework.batch + spring-batch-core + ${dependency.spring.batch.version} + + + org.springframework.integration + spring-integration-core + ${dependency.spring.integration.version} + org.springframework.data spring-data-jpa diff --git a/spring-bootstrap-cli/samples/job.groovy b/spring-bootstrap-cli/samples/job.groovy index c2497ffdab..4daddca98d 100644 --- a/spring-bootstrap-cli/samples/job.groovy +++ b/spring-bootstrap-cli/samples/job.groovy @@ -32,55 +32,4 @@ class JobConfig { } } -import groovy.util.logging.Log -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-samples/spring-bootstrap-integration-sample/pom.xml b/spring-bootstrap-samples/spring-bootstrap-integration-sample/pom.xml index a6d8a37b75..98adc7973f 100644 --- a/spring-bootstrap-samples/spring-bootstrap-integration-sample/pom.xml +++ b/spring-bootstrap-samples/spring-bootstrap-integration-sample/pom.xml @@ -12,7 +12,6 @@ ${project.basedir}/../.. org.springframework.bootstrap.sample.service.IntegrationBootstrapApplication - 2.2.3.RELEASE @@ -23,12 +22,11 @@ org.springframework.integration spring-integration-core - ${spring.integration.version} org.springframework.integration spring-integration-stream - ${spring.integration.version} + ${dependency.spring.integration.version} ch.qos.logback diff --git a/spring-bootstrap/pom.xml b/spring-bootstrap/pom.xml index 7bd43d7e57..241684f7a1 100644 --- a/spring-bootstrap/pom.xml +++ b/spring-bootstrap/pom.xml @@ -97,6 +97,16 @@ spring-data-jpa true + + org.springframework.batch + spring-batch-core + true + + + org.springframework.integration + spring-integration-core + true + ch.qos.logback logback-classic diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/BatchAutoConfiguration.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/BatchAutoConfiguration.java new file mode 100644 index 0000000000..f5634d5bde --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/BatchAutoConfiguration.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.bootstrap.autoconfigure.batch; + +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.bootstrap.CommandLineRunner; +import org.springframework.bootstrap.context.annotation.ConditionalOnClass; +import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean; +import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for Spring Batch. + * + * @author Dave Syer + */ +@Configuration +@ConditionalOnClass({ JobLauncher.class }) +public class BatchAutoConfiguration { + + @Bean + // Harmless to always include this, but maybe could make it conditional as well + public BatchDatabaseInitializer batchDatabaseInitializer() { + return new BatchDatabaseInitializer(); + } + + @Bean + @ConditionalOnMissingBean({ CommandLineRunner.class }) + public JobLauncherCommandLineRunner jobLauncherCommandLineRunner() { + return new JobLauncherCommandLineRunner(); + } + +} diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/BatchDatabaseInitializer.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/BatchDatabaseInitializer.java new file mode 100644 index 0000000000..a11f3202e4 --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/BatchDatabaseInitializer.java @@ -0,0 +1,53 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.bootstrap.autoconfigure.batch; + +import javax.annotation.PostConstruct; +import javax.sql.DataSource; + +import org.springframework.batch.support.DatabaseType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ResourceLoader; +import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; +import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; +import org.springframework.stereotype.Component; + +@Component +public class BatchDatabaseInitializer { + + @Autowired + private DataSource dataSource; + + @Autowired + private ResourceLoader resourceLoader; + + @PostConstruct + protected void initialize() throws Exception { + String platform = DatabaseType.fromMetaData(this.dataSource).toString() + .toLowerCase(); + if ("hsql".equals(platform)) { + platform = "hsqldb"; + } + ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); + populator + .addScript(this.resourceLoader + .getResource("org/springframework/batch/core/schema-" + platform + + ".sql")); + populator.setContinueOnError(true); + DatabasePopulatorUtils.execute(populator, this.dataSource); + } + +} \ No newline at end of file diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/JobLauncherCommandLineRunner.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/JobLauncherCommandLineRunner.java new file mode 100644 index 0000000000..e0262907d9 --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/batch/JobLauncherCommandLineRunner.java @@ -0,0 +1,62 @@ +/* + * Copyright 2012-2013 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.bootstrap.autoconfigure.batch; + +import java.util.Arrays; +import java.util.Properties; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecutionException; +import org.springframework.batch.core.converter.DefaultJobParametersConverter; +import org.springframework.batch.core.converter.JobParametersConverter; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.bootstrap.CommandLineRunner; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +@Component +// FIXME: what to do with more than one Job? +public class JobLauncherCommandLineRunner implements CommandLineRunner { + + private static Log logger = LogFactory.getLog(JobLauncherCommandLineRunner.class); + + @Autowired(required = false) + private JobParametersConverter converter = new DefaultJobParametersConverter(); + + @Autowired + private JobLauncher jobLauncher; + + @Autowired + private Job job; + + public void run(String... args) { + logger.info("Running default command line with: " + Arrays.asList(args)); + launchJobFromProperties(StringUtils.splitArrayElementsIntoProperties(args, + "=")); + } + + protected void launchJobFromProperties(Properties properties) { + try { + this.jobLauncher.run(this.job, + this.converter.getJobParameters(properties)); + } catch (JobExecutionException e) { + throw new IllegalStateException("Could not run job", e); + } + } +} \ No newline at end of file diff --git a/spring-bootstrap/src/main/resources/META-INF/spring.factories b/spring-bootstrap/src/main/resources/META-INF/spring.factories index a294a9efb7..cbadd2cc47 100644 --- a/spring-bootstrap/src/main/resources/META-INF/spring.factories +++ b/spring-bootstrap/src/main/resources/META-INF/spring.factories @@ -2,6 +2,7 @@ org.springframework.bootstrap.context.annotation.EnableAutoConfiguration=\ org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration,\ org.springframework.bootstrap.autoconfigure.data.JpaRepositoriesAutoConfiguration,\ org.springframework.bootstrap.autoconfigure.jdbc.EmbeddedDatabaseAutoConfiguration,\ +org.springframework.bootstrap.autoconfigure.batch.BatchAutoConfiguration,\ org.springframework.bootstrap.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\ org.springframework.bootstrap.autoconfigure.web.EmbeddedJettyAutoConfiguration,\ org.springframework.bootstrap.autoconfigure.web.EmbeddedTomcatAutoConfiguration,\