2

The following is my Spring Batch processing config file, i am reading multiple files (xml, csv etc), the files generate dynamically with time stamp as suffix i can read file's data and process,
now the Question is, i would like know the file name. How to get file name when job is processing.

<import resource="../config/context.xml" />
    <bean id="domain" class="com.di.pos.Domain" />
    <job id="readMultiFileJob" xmlns="http://www.springframework.org/schema/batch">
        <step id="step1">
            <tasklet>
                <chunk reader="multiResourceReader" writer="flatFileItemWriter"
                    commit-interval="1" />
            </tasklet>
        </step>
    </job>
    <bean id="multiResourceReader"
        class=" org.springframework.batch.item.file.MultiResourceItemReader">
        <property name="resources" value="file:csv/inputs/dipos-*.csv" />
        <property name="delegate" ref="flatFileItemReader" />
    </bean>
    <bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <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="names" value="id, name" />
                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean
                        class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="domain" />
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Ravichandra
  • 1,260
  • 7
  • 23
  • 37
  • 1
    Possible duplicate of http://stackoverflow.com/questions/22993633/get-current-resource-name-using-multiresourceitemreader-spting-batch – Luca Basso Ricci Feb 15 '16 at 11:11

1 Answers1

0
  1. Create a custom Mapper which extends LineMapper.
  2. Override mapLine() method

public FileData mapLine(String line, int lineNumber) throws Exception { FileData fileData = new FileData();

        Resource currentResource = delegator.getCurrentResource();
        String[] fileName = currentResource.getFilename().split("/");

        //Use this to access file path
        URI fileUri = currentResource.getURI();
        return fileData;
    }
user1270392
  • 2,981
  • 4
  • 21
  • 25