Spring Cloud Task 集成Spring Cloud Task Batch(二)

2023-04-17 10:40:04 浏览数 (2)

创建Spring Cloud Task

下一步是创建Spring Cloud Task,它将用于运行我们的Spring Batch作业。为此,我们需要定义一个TaskConfigurer和一个TaskLauncher:

代码语言:javascript复制
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.task.configuration.DefaultTaskConfigurer;
import org.springframework.cloud.task.configuration.EnableTask;
import org.springframework.cloud.task.listener.TaskExecutionListener;
import org.springframework.cloud.task.repository.TaskExecution;
import org.springframework.cloud.task.repository.TaskExplorer;
import org.springframework.cloud.task.repository.TaskRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
@EnableTask
public class TaskConfiguration {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private TaskExplorer taskExplorer;

    @Autowired
    private JobRegistry jobRegistry;

    @Bean
    public TaskExecutionListener taskExecutionListener() {
        return new CustomTaskExecutionListener();
    }

    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor() {
        JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor();
        postProcessor.setJobRegistry(jobRegistry);
        return postProcessor;
    }

    @Bean
    public JobLauncher jobLauncher(JobRepository jobRepository) {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        return jobLauncher;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public DefaultTaskConfigurer defaultTaskConfigurer() {
        return new DefaultTaskConfigurer(dataSource);
    }

}

在这个类中,我们定义了一个DataSource属性,该属性将用于配置作业存储库。我们还注入了jobBuilderFactory,stepBuilderFactory,taskRepository,taskExplorer和jobRegistry,这些属性将用于在任务执行期间启动Spring Batch作业。

我们定义了一个taskExecutionListener,它将用于在任务执行期间添加我们还定义了一个jobRegistryBeanPostProcessor,用于将作业注册表添加到应用程序上下文中。我们还定义了一个jobLauncher,它将用于在任务执行期间启动作业。最后,我们定义了一个transactionManager和defaultTaskConfigurer,它们将用于配置任务存储库和事务管理。

0 人点赞