为什么执行任务()任务方法从未调用



我需要在春季批处理上写简单的应用。我有config job 步骤结构。在内部步骤我使用任务。问题是:Tasklet的Execute((方法从未调用。该程序运行作业 -> step->创建任务,仅此而已。一个找到了许多我用于该代码写作的示例,我不明白,我在做错了什么。

我的代码:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .tasklet(new DemoTasklet())
                .build();
    }
    @Bean
    public Job job() throws Exception {
        return jobBuilderFactory.get("job1")
                .incrementer(new RunIdIncrementer())
                .flow(step1())
                .end()
                .build();
    }
}

任务代码:

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.StepExecutionListener;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class DemoTasklet implements Tasklet, StepExecutionListener {
    public DemoTasklet() {
        System.out.println("demo tasklet constructor");
    }
    @Override
    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
        System.out.println("hello");
        return RepeatStatus.FINISHED;
    }
    @Override
    public void beforeStep(StepExecution stepExecution) {
        System.out.println("before");
    }
    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        System.out.println("after");
        return stepExecution.getExitStatus();
    }
}

以及主要类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String args[]) {
        System.out.println(3);
        SpringApplication.run(BatchConfiguration.class, args);
        System.out.println(4);
    }
}

请说出什么问题,以及我应该如何修复它。

请参阅@luca basso ricci评论,我只是不考虑。我应该使用:

 SpringApplication.run(Main.class, args);

在主班上,在Job Bean方法声明中错过了" 1"。

最新更新