0

我需要在 Spring Batch 上编写简单的应用程序。我有配置+工作+步骤结构。在里面我使用tasklet。问题是:tasklet 的 execute() 方法从未被调用过。该程序运行作业-> 步骤-> 创建小任务,仅此而已。A 发现了很多示例,我用于编写代码,但我无法理解,我做错了什么。

我的代码:

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);
    }
}

请说出什么是错的,以及我应该如何解决它。

4

1 回答 1

0

请参阅@Luca Basso Ricci 评论,我只是不体谅。我应该使用:

 SpringApplication.run(Main.class, args);

在 Main 类中,并且在作业 bean 方法声明中错过了“1”。

于 2018-02-07T10:17:37.850 回答