2

Is there a way to customize the MultiResourceItemReader Resources other than getting the value from JobParameters or using the Value tag? I tried the following but it did not work.

<bean id="ItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
      <property name="resources" ref="fileResources" />
      <property name="delegate" ref="flatFileItemReader" />
</bean>

<bean id="fileResources" class="com.app.batch.fileloader.file.FileResources" />

<bean id="flatFileItemReader" class="com.app.batch.fileloader.file.MyFlatFileItemReader">
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
            <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <property name="delimiter" value="," />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean class="com.app.batch.fileloader.file.MyFileFieldSetMapper" />
            </property>
        </bean>
    </property>
</bean>

In the FileResources Java class I extended the MultiResourceItemReader like this

public class FileResources extends MultiResourceItemReader<FileDTO>{

@Override
public void setResources(Resource[] resources){
    ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
    try{
        resources = patternResolver.getResources("file:" + properties.getPath() + "/*.csv");
    }
    catch(IOException ex){
        LOG.error("The resources must not be null");
    }

    super.setResources(resources);
}

What am I doing wrong can someone please let me know. Thanks!

1 Answer 1

2

Use a FactoryBean. Create an implementation that returns a Resource [] and inject that into your MultiResourceItemReader. Spring will call the factory bean and use the output to populate the dependency.

An example would look something like this:

<bean id="ItemReader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
      <property name="resources" ref="fileResources" />
      <property name="delegate" ref="flatFileItemReader" />
</bean>

<bean id="fileResources" class="com.app.batch.fileloader.file.ResourcesFactoryBean" />  

With the above configuration, you'd use the below FactoryBean implementation:

public class ResourcesFactoryBean implements FactoryBean<Resource[]>{

    @Override
    public Resource[] getObject() {
        ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
        try{
            resources = patternResolver.getResources("file:" + properties.getPath() + "/*.csv");
        }
        catch(IOException ex){
            LOG.error("The resources must not be null");
        }

        return resources;
    }
   ...

You can read more about Spring's FactoryBean interface in the documentation here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/FactoryBean.html

And here: https://docs.spring.io/spring/docs/5.0.3.RELEASE/spring-framework-reference/core.html#beans-factory-extension-factorybean

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.