首页 文章

Spring批处理项处理器未执行

提问于
浏览
3

我正在使用STS 2.81附带的Spring Batch模板创建一个Spring Batch作业,并使用Manning的Spring Batch in Action中的示例 . 我能够毫无问题地执行块读取器和写入器,但我的代码正在跳过处理器 . 我甚至尝试将所有对象归零,因为它进入处理器而没有任何东西,对象仍然设法被写入,好像处理器被忽略一样 . 我尝试在处理器内调用System.out.println,但nothign在终端中打印出来 . 我最终通过注释将配置从使用XML bean更改为Component,但它也无法正常工作 . 我不确定是否有一些我错过的设置......我在Spring Batch in Action和SpringSource网站上都有示例,一切看起来都不错......帮助!

这是代码:

<batch:job id="job1">
    <batch:step id="step1"  >           
        <batch:tasklet transaction-manager="transactionManager" start-limit="100" >
            <batch:chunk reader="productFlatFileReader"
                         processor="productProcessor"
                         writer="productFlatFileWriter"
                         commit-interval="10" />
        </batch:tasklet>
    </batch:step>
</batch:job>

这是处理器bean:

<bean id="productProcessor" class="com.test.training.processors.ProductProcessor" />

这是我试图执行的Processor类,但没有用:

package com.test.training.processors;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;
import com.test.training.entities.Product;

public class ProductProcessor implements ItemProcessor<Product, Product> {

@Override
public Product process(Product product) throws Exception {
    product.setDescription("Processor is WORKING!");
    return product;
    //return this.validateProductByProductIdentifier(product) ? null : product;
}

private boolean validateProductByProductIdentifier(Product product) {
    return product.getProduct_identifier() == 5 ? true : false;
}
}

1 回答

  • 0

    您的bean配置需要具有scope =“step”,以便Spring Batch将bean识别为批处理bean .

    喜欢:

    <bean id="productProcessor" scope="step" class="com.test.training.processors.ProductProcessor" />
    

相关问题