mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
[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]
This commit is contained in:
parent
83e0ea22c1
commit
bcf86b320e
20
pom.xml
20
pom.xml
|
@ -22,6 +22,8 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<dependency.springframework.version>4.0.0.BOOTSTRAP-SNAPSHOT</dependency.springframework.version>
|
||||
<dependency.security.javaconfig.version>1.0.0.CI-SNAPSHOT</dependency.security.javaconfig.version>
|
||||
<dependency.spring.integration.version>2.2.3.RELEASE</dependency.spring.integration.version>
|
||||
<dependency.spring.batch.version>2.2.0.RC1</dependency.spring.batch.version>
|
||||
<main.basedir>${project.basedir}</main.basedir>
|
||||
</properties>
|
||||
<repositories>
|
||||
|
@ -33,6 +35,14 @@
|
|||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>http://maven.springframework.org/milestone</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
|
@ -356,6 +366,16 @@
|
|||
<artifactId>spring-tx</artifactId>
|
||||
<version>${dependency.springframework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
<artifactId>spring-batch-core</artifactId>
|
||||
<version>${dependency.spring.batch.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-core</artifactId>
|
||||
<version>${dependency.spring.integration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
<properties>
|
||||
<main.basedir>${project.basedir}/../..</main.basedir>
|
||||
<start-class>org.springframework.bootstrap.sample.service.IntegrationBootstrapApplication</start-class>
|
||||
<spring.integration.version>2.2.3.RELEASE</spring.integration.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -23,12 +22,11 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-core</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-stream</artifactId>
|
||||
<version>${spring.integration.version}</version>
|
||||
<version>${dependency.spring.integration.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
|
|
|
@ -97,6 +97,16 @@
|
|||
<artifactId>spring-data-jpa</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.batch</groupId>
|
||||
<artifactId>spring-batch-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.integration</groupId>
|
||||
<artifactId>spring-integration-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,\
|
||||
|
|
Loading…
Reference in New Issue
Block a user