为什么从不调用tasklet的execute()方法

时间:2018-02-07 06:56:03

标签: java spring-batch tasklet

我需要在春季批量上写简单的应用程序。我有config + job + step结构。在内部步骤中我使用tasklet。 问题是:从未调用过tasklet的execute()方法。 该程序运行工作 - >步骤 - >创建tasklet,就是这样。发现了许多我用于代码编写的例子,我无法理解,我做错了什么。

我的代码:

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

Tasklet代码:

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

请说出错了,以及我应该如何解决它。

1 个答案:

答案 0 :(得分:0)

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

 SpringApplication.run(Main.class, args);

在大班,并且错过" 1"在job bean方法声明中。