简体   繁体   中英

Spring Batch Item Processor not Executing

I am creating a Spring Batch job using the Spring Batch template included with STS 2.81 and using examples from Spring Batch in Action by Manning. I am able to execute the chunk reader and writer without problems, but my code is skipping the processor. I have even tried nulling out all objects as it gets in the processor and nothing, the objects still manage to get written as if the processor is ignored. I tried calling the System.out.println within the Processor but nothign gets printed out in the terminal. I finally changed the configuration from using an XML bean to a Component via annotation and it did not work either. I am not sure if there is some setting that I am missing out...I followed examples in both Spring Batch in Action and from the SpringSource website and everything look ok...help!

Here's the code:

<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>

Here's the processor bean:

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

This is the Processor class that I am trying to execute with no avail:

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

Your bean configuration needs to have scope="step" for Spring Batch to recognize bean as a batch bean.

Like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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