简体   繁体   English

如何按顺序初始化步骤?

[英]How can I initialize a step in sequence?

I'm new in Spring Batch and I'm trying to execute two steps of a job. 我是Spring Batch的新手,正在尝试执行工作的两个步骤。 Both must initialize something before execute the read, process and write methods. 两者都必须在执行read,process和write方法之前初始化一些东西。 But I'm not getting how can I do it. 但是我不知道该怎么办。 Every time I launch the job, the two steps initialize at same time. 每次启动作业时,两个步骤都会同时初始化。 I want they initialize in the sequence of the job. 我希望它们按照工作顺序进行初始化。

To put it simply, I did something like that: 简单来说,我做了这样的事情:

public Job job() {
    return jobBuilderFactory.get("job")
            .incrementer(new RunIdIncrementer())
            .start(step1())
            .next(step2())
            .build();
}

public Step step1() {

    return stepBuilderFactory.get("step1")
            .<Model1, Model1>chunk(2)
            .reader(reader1())
            .processor(processor1())
            .writer(writer1())
            .build();
}

public Step step2() {

    return stepBuilderFactory.get("step2")
            .<Model2, Model2>chunk(2)
            .reader(reader2())
            .processor(processor2())
            .writer(writer2())
            .build();
}

@StepScope
public Reader1 reader1() {
    return new Reader1();
}


@StepScope
public Processor1 processor1() {
    return new Processor1();
}

@StepScope
public Writer1 writer1() {
    return new Writer1();
}


@StepScope
public Reader2 reader2() {
    return new Reader2();
}


@StepScope
public Processor2 processor2() {
    return new Processor2();
}

@StepScope
public Writer2 writer2() {
    return new Writer2();
}

That's my Reader2 class that I want to initialize after the first step. 这是我要在第一步之后初始化的Reader2类。 The Reader1 is the same thing. Reader1是同一件事。 Both "test" are printed and then steps begin to run. 将打印两个“测试”,然后开始运行步骤。

public class Reader2 implements ItemReader<Model2>{

    public Reader2() {
        initialize();
    }

    public void initialize() {
        System.out.println("test");
    }

    @Override
    public Model2 read() throws Exeption {
    .
    .
    .
    } 
}

I would use a StepExecutionListener#beforeStep for that matter. 我将为此使用StepExecutionListener#beforeStep It is more appropriate for step initialization rather than doing the initialization in the reader's constructor. 比起在读取器的构造函数中进行初始化,它更适合于步骤初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM