如何在我的 spring 批处理应用程序中显式配置 TaskBatchExecutionListener

How to explicitly configure TaskBatchExecutionListener in my spring batch application

我的 spring 批处理应用程序未在 TASK_TASK_BATCH table 中插入任务和作业之间的关系。

Spring 医生说:

Associating A Job Execution To The Task In Which It Was Executed Spring Boot provides facilities for the execution of batch jobs easily within an über-jar. Spring Boot’s support of this functionality allows for a developer to execute multiple batch jobs within that execution. Spring Cloud Task provides the ability to associate the execution of a job (a job execution) with a task’s execution so that one can be traced back to the other.

此功能是通过使用 TaskBatchExecutionListener 实现的。默认情况下,此侦听器在任何上下文中自动配置,该上下文同时配置了 Spring 批处理作业 (通过在上下文中定义了类型为 Job 的 bean)和 spring-cloud -task-batch jar 在类路径中可用。侦听器将被注入到所有作业中。"

我的 classpath.It 中有所有必需的 jar,只是我正在动态创建作业和 tasklet,因此不使用任何注释。根据文档,TaskBatchExecutionListener 负责通过调用 taskBatchDao's saveRelationship 方法在 TASK_TASK_BATCH table 中创建映射。

我只是无法弄清楚如何在我的 spring 批处理应用程序中明确配置 TaskBatchExecutionListener

如果您有 org.springframework.cloud:spring-cloud-task-batch 依赖项,并且存在注解 @EnableTask,那么您的应用程序上下文包含一个 TaskBatchExecutionListener bean,您可以将其注入 class动态创建作业和微线程。

这看起来可能与此类似:

    @Autowired
    JobBuilderFactory jobBuilderFactory;

    @Autowired
    TaskBatchExecutionListener taskBatchExecutionListener;

    Job createJob() throws Exception {
        return jobBuilderFactory
                .get("myJob")
                .start(createStep())
                .listener(taskBatchExecutionListener)
                .build();
    }

希望对您有所帮助。否则请分享一些最小的代码示例来演示您正在尝试做什么。