Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current resource name using MultiResourceItemReader Spring batch

I am using MultiResourceItemReader in Spring Batch for reading multiple XML files and I want to get current resource.Here is my configuration:

public class MultiFileResourcePartitioner extends MultiResourceItemReader<MyObject> {
    @Override
    public void update(final ExecutionContext pExecutionContext) throws ItemStreamException {
        super.update(pExecutionContext);
        if (getCurrentResource() != null && getCurrentResource().getFilename() != null) {
            System.out.println("update:" + getCurrentResource().getFilename());
        }
    }
}

And my reader:

<bean id="myMultiSourceReader"
   class="mypackage.MultiFileResourcePartitioner">
   <property name="resources" value="file:${input.directory}/*.xml" />
        
</bean>

The code above read XML files correctly but the method getCurrentResources() return null. By debugging, the batch enter to update method

Please help!

UPDATE: My code bellow is correct. I have just clean my project and now I can get the current resource.

like image 653
Walid Ammou Avatar asked Apr 16 '26 10:04

Walid Ammou


1 Answers

There is a specific interface for this problem called ResourceAware: it's purpouse is to inject current resource into objects read from a MultiResourceItemReader.
Check this thread for further information.

like image 136
Luca Basso Ricci Avatar answered Apr 19 '26 01:04

Luca Basso Ricci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!