2023-01 spring-28-spring-batch updated 2

This commit is contained in:
stvort
2023-05-20 10:24:28 +04:00
parent ff04831a9d
commit 101c5a77e8
4 changed files with 18 additions and 52 deletions
+5 -11
View File
@@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
<version>3.1.0</version>
<relativePath/>
</parent>
@@ -25,20 +25,14 @@
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
@@ -1,27 +1,11 @@
package ru.otus.example.springbatch.config;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.batch.BatchDataSource;
import org.springframework.boot.autoconfigure.batch.BatchDataSourceScriptDatabaseInitializer;
import org.springframework.boot.autoconfigure.batch.BatchProperties;
import org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
@EnableBatchProcessing
@EnableConfigurationProperties(BatchProperties.class)
@SuppressWarnings("unused")
@Configuration
public class BatchConfig {
@Bean
@@ -30,24 +14,4 @@ public class BatchConfig {
processor.setJobRegistry(jobRegistry);
return processor;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.batch.job", name = "enabled", havingValue = "true", matchIfMissing = true)
public JobLauncherApplicationRunner jobLauncherApplicationRunner(JobLauncher jobLauncher, JobExplorer jobExplorer,
JobRepository jobRepository, BatchProperties properties) {
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(jobLauncher, jobExplorer, jobRepository);
String jobNames = properties.getJob().getName();
if (StringUtils.hasText(jobNames)) {
runner.setJobName(jobNames);
}
return runner;
}
@Bean
@ConditionalOnMissingBean(BatchDataSourceScriptDatabaseInitializer.class)
public BatchDataSourceScriptDatabaseInitializer batchDataSourceInitializer(DataSource dataSource,
@BatchDataSource ObjectProvider<DataSource> batchDataSource, BatchProperties properties) {
return new BatchDataSourceScriptDatabaseInitializer(batchDataSource.getIfAvailable(() -> dataSource),
properties.getJdbc());
}
}
@@ -7,6 +7,7 @@ import org.springframework.stereotype.Service;
@Service
public class CleanUpService {
@SuppressWarnings("unused")
public void cleanUp() throws Exception {
log.info("Выполняю завершающие мероприятия...");
Thread.sleep(1000);
@@ -11,6 +11,8 @@ import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import ru.otus.example.springbatch.config.AppProps;
import java.util.Properties;
import static ru.otus.example.springbatch.config.JobConfig.IMPORT_USER_JOB_NAME;
import static ru.otus.example.springbatch.config.JobConfig.INPUT_FILE_NAME;
import static ru.otus.example.springbatch.config.JobConfig.OUTPUT_FILE_NAME;
@@ -28,6 +30,8 @@ public class BatchCommands {
//http://localhost:8080/h2-console/
@SuppressWarnings("unused")
@ShellMethod(value = "startMigrationJobWithJobLauncher", key = "sm-jl")
public void startMigrationJobWithJobLauncher() throws Exception {
JobExecution execution = jobLauncher.run(importUserJob, new JobParametersBuilder()
@@ -37,15 +41,18 @@ public class BatchCommands {
System.out.println(execution);
}
@SuppressWarnings("unused")
@ShellMethod(value = "startMigrationJobWithJobOperator", key = "sm-jo")
public void startMigrationJobWithJobOperator() throws Exception {
Long executionId = jobOperator.start(IMPORT_USER_JOB_NAME,
INPUT_FILE_NAME + "=" + appProps.getInputFile() + "\n" +
OUTPUT_FILE_NAME + "=" + appProps.getOutputFile()
);
Properties properties = new Properties();
properties.put(INPUT_FILE_NAME, appProps.getInputFile());
properties.put(OUTPUT_FILE_NAME, appProps.getOutputFile());
Long executionId = jobOperator.start(IMPORT_USER_JOB_NAME, properties);
System.out.println(jobOperator.getSummary(executionId));
}
@SuppressWarnings("unused")
@ShellMethod(value = "showInfo", key = "i")
public void showInfo() {
System.out.println(jobExplorer.getJobNames());