Refactored for CloudFoundry Support
This commit adds on an abstraction for the providing unique command line arugements for each worker. It also updates the partitioned job sample to be able to successfully be run on CloudFoundry. Resolves spring-cloud/spring-cloud-task#193 Updated based on comments
This commit is contained in:
committed by
Glenn Renfro
parent
c14d2efca3
commit
025c447b89
@@ -22,7 +22,7 @@ $ export spring_datasource_url=jdbc:mysql://localhost:3306/<your database>
|
||||
$ export spring_datasource_username=<your username>
|
||||
$ export spring_datasource_password=<your password>
|
||||
$ export spring_datasource_driverClassName=org.mariadb.jdbc.Driver
|
||||
$ java -jar -Dspring.profiles.active=master target/partitioned-batch-job-1.1.0.BUILD-SNAPSHOT.jar
|
||||
$ java -jar target/partitioned-batch-job-1.1.0.BUILD-SNAPSHOT.jar
|
||||
----
|
||||
|
||||
NOTE: This example will use require a MySql RDBMS repository and currently uses the mariadb jdbc driver to connect.
|
||||
|
||||
@@ -64,8 +64,8 @@
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||
@@ -77,7 +77,7 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
||||
@@ -15,8 +15,11 @@
|
||||
*/
|
||||
package io.spring;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -37,11 +40,11 @@ import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.deployer.resource.support.DelegatingResourceLoader;
|
||||
import org.springframework.cloud.deployer.spi.local.LocalDeployerProperties;
|
||||
import org.springframework.cloud.deployer.spi.local.LocalTaskLauncher;
|
||||
import org.springframework.cloud.deployer.spi.task.TaskLauncher;
|
||||
import org.springframework.cloud.task.batch.partition.DeployerPartitionHandler;
|
||||
import org.springframework.cloud.task.batch.partition.DeployerStepExecutionHandler;
|
||||
import org.springframework.cloud.task.batch.partition.NoOpEnvironmentVariablesProvider;
|
||||
import org.springframework.cloud.task.batch.partition.PassThroughCommandLineArgsProvider;
|
||||
import org.springframework.cloud.task.batch.partition.SimpleEnvironmentVariablesProvider;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -88,29 +91,20 @@ public class JobConfiguration {
|
||||
return jobExplorerFactoryBean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TaskLauncher taskLauncher() {
|
||||
LocalDeployerProperties localDeployerProperties = new LocalDeployerProperties();
|
||||
|
||||
localDeployerProperties.setDeleteFilesOnExit(false);
|
||||
|
||||
return new LocalTaskLauncher(localDeployerProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PartitionHandler partitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer) throws Exception {
|
||||
Resource resource = resourceLoader.getResource("maven://io.spring.cloud:partitioned-batch-job:1.1.0.BUILD-SNAPSHOT");
|
||||
|
||||
DeployerPartitionHandler partitionHandler = new DeployerPartitionHandler(taskLauncher, jobExplorer, resource, "workerStep");
|
||||
|
||||
Map<String, String> environmentProperties = new HashMap<>();
|
||||
environmentProperties.put("spring.profiles.active", "worker");
|
||||
|
||||
SimpleEnvironmentVariablesProvider environmentVariablesProvider = new SimpleEnvironmentVariablesProvider(this.environment);
|
||||
environmentVariablesProvider.setEnvironmentProperties(environmentProperties);
|
||||
partitionHandler.setEnvironmentVariablesProvider(environmentVariablesProvider);
|
||||
|
||||
partitionHandler.setMaxWorkers(2);
|
||||
List<String> commandLineArgs = new ArrayList<>(3);
|
||||
commandLineArgs.add("--spring.profiles.active=worker");
|
||||
commandLineArgs.add("--spring.cloud.task.initialize.enable=false");
|
||||
commandLineArgs.add("--spring.batch.initializer.enabled=false");
|
||||
partitionHandler.setCommandLineArgsProvider(new PassThroughCommandLineArgsProvider(commandLineArgs));
|
||||
partitionHandler.setEnvironmentVariablesProvider(new SimpleEnvironmentVariablesProvider(this.environment));
|
||||
partitionHandler.setMaxWorkers(1);
|
||||
partitionHandler.setApplicationName("PartitionedBatchJobTask");
|
||||
|
||||
return partitionHandler;
|
||||
}
|
||||
@@ -173,9 +167,10 @@ public class JobConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Profile("master")
|
||||
@Profile("!worker")
|
||||
public Job partitionedJob(PartitionHandler partitionHandler) throws Exception {
|
||||
return jobBuilderFactory.get("partitionedJob")
|
||||
Random random = new Random();
|
||||
return jobBuilderFactory.get("partitionedJob"+random.nextInt())
|
||||
.start(step1(partitionHandler))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
spring.application.name=Partitioned Batch Job Task
|
||||
spring.application.name=PartitionedBatchJobTask
|
||||
logging.level.org.springframework.cloud.task=DEBUG
|
||||
maven.remoteRepositories.springRepo.url=https://repo.spring.io/libs-snapshot
|
||||
|
||||
|
||||
Reference in New Issue
Block a user