Spring Batch:在读写器之间传递数据

时间:2015-09-23 09:55:44

标签: spring spring-boot spring-batch

我想在Writer中获取我在步骤的Reader中设置的数据。我通过http://docs.spring.io/spring-batch/trunk/reference/html/patterns.html#passingDataToFutureSteps了解ExecutionContexts(步骤和工作)以及ExecutionContextPromotionListener

问题是在Writer中我正在检索'npag'的空值。

ItemWriter上的行:

LOG.info("INSIDE WRITE, NPAG: " + nPag);

我正在做一些没有运气的解决方法,寻找其他similar questions的答案......任何帮助?谢谢!

这是我的代码:

READER

@Component
public class LCItemReader implements ItemReader<String> {

private StepExecution stepExecution;

private int nPag = 1;

@Override
public String read() throws CustomItemReaderException {

    ExecutionContext stepContext = this.stepExecution.getExecutionContext();
    stepContext.put("npag", nPag);
    nPag++;
    return "content";
}

@BeforeStep
public void saveStepExecution(StepExecution stepExecution) {
    this.stepExecution = stepExecution;
}
}

WRITER

@Component
@StepScope
public class LCItemWriter implements ItemWriter<String> {

private String nPag;

@Override
public void write(List<? extends String> continguts) throws Exception {
    try {
        LOG.info("INSIDE WRITE, NPAG: " + nPag);
    } catch (Throwable ex) {
        LOG.error("Error: " + ex.getMessage());
    }
}

@BeforeStep
public void retrieveInterstepData(StepExecution stepExecution) {
    JobExecution jobExecution = stepExecution.getJobExecution();
    ExecutionContext jobContext = jobExecution.getExecutionContext();
    this.nPag = jobContext.get("npag").toString();
}
}

JOB / STEP BATCH CONFIG

@Bean
public Job lCJob() {
    return jobs.get("lCJob")
            .listener(jobListener)
            .start(lCStep())
            .build();
}

@Bean
public Step lCStep() {
    return steps.get("lCStep")
            .<String, String>chunk(1)
            .reader(lCItemReader)
            .processor(lCProcessor)
            .writer(lCItemWriter)
            .listener(promotionListener())
            .build();
}

LISTENER

@Bean
public ExecutionContextPromotionListener promotionListener() {
    ExecutionContextPromotionListener executionContextPromotionListener = new ExecutionContextPromotionListener();
    executionContextPromotionListener.setKeys(new String[]{"npag"});
    return executionContextPromotionListener;
}

3 个答案:

答案 0 :(得分:4)

ExecutionContextPromotionListener明确指出它在步骤结束时工作,以便在编写器执行之后。因此,当您认为这种推广时,我认为您所依赖的推广不会发生。

如果我是你,我会在步骤上下文中设置它,如果你只需要一步就可以从步骤中获取它。否则我会将其设置为工作环境。

另一方面是@BeforeStep。这标志着在步骤上下文存在之前执行的方法。您在阅读器中设置nPag值的方式是在步骤开始执行之后。

答案 1 :(得分:2)

您试图在读取器中设置nPag之前读取nPag的值,最后得到一个默认值为null。您需要在直接从执行上下文中记录时读取nPag上的值。您可以保留对jobContext的引用。试试这个

@Component
@StepScope
public class LCItemWriter implements ItemWriter<String> {

private String nPag;
private ExecutionContext jobContext;

@Override
public void write(List<? extends String> continguts) throws Exception {
    try {
        this.nPag = jobContext.get("npag").toString();
        LOG.info("INSIDE WRITE, NPAG: " + nPag);
    } catch (Throwable ex) {
        LOG.error("Error: " + ex.getMessage());
    }
}

@BeforeStep
public void retrieveInterstepData(StepExecution stepExecution) {
    JobExecution jobExecution = stepExecution.getJobExecution();
    jobContext = jobExecution.getExecutionContext();

}
}

答案 2 :(得分:1)

在你的Reader and Writer中你需要实现ItemStream接口并使用ExecutionContext作为成员变量。我已经用Processor而不是Writer给出了例子,但同样适用于Writer。它对我来说工作正常,我能够从读者到处理器的价值观。

我已在阅读器的上下文中设置了值,并在处理器中获取了值。

public class EmployeeItemReader implements ItemReader<Employee>, ItemStream {

    ExecutionContext context;
    @Override
    public Employee read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        context.put("ajay", "i am going well");
        Employee emp=new Employee();
        emp.setEmpId(1);
        emp.setFirstName("ajay");
        emp.setLastName("goswami");
        return emp;
    }
    @Override
    public void close() throws ItemStreamException {
        // TODO Auto-generated method stub
    }

    @Override
    public void open(ExecutionContext arg0) throws ItemStreamException {
        context = arg0;
    }

    @Override
    public void update(ExecutionContext arg0) throws ItemStreamException {
        // TODO Auto-generated method stub
        context = arg0;
    }

}

我的处理器

public class CustomItemProcessor implements ItemProcessor<Employee,ActiveEmployee>,ItemStream{
    ExecutionContext context;
    @Override
    public ActiveEmployee process(Employee emp) throws Exception {
        //See this line
        System.out.println(context.get("ajay"));

        ActiveEmployee actEmp=new ActiveEmployee();
        actEmp.setEmpId(emp.getEmpId());
        actEmp.setFirstName(emp.getFirstName());
        actEmp.setLastName(emp.getLastName());
        actEmp.setAdditionalInfo("Employee is processed");
        return actEmp;
    }
    @Override
    public void close() throws ItemStreamException {
        // TODO Auto-generated method stub

    }
    @Override
    public void open(ExecutionContext arg0) throws ItemStreamException {
        // TODO Auto-generated method stub

    }
    @Override
    public void update(ExecutionContext arg0) throws ItemStreamException {
        context = arg0;

    }

}

希望这有帮助。