3
votes

I have a simple spring batch job - read a file line by line, do something with the input string, and write some output. Output file contains every line of input plus some processing status for that line (success/failure.) The reads a file from: <dir>/<inputFolder>/<inputFileName> and writes processed output to <dir>/<outputFolder>/<inputFileName> All these values are passed as jobParameters
File Reader is like so:

<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <property name="resource" value="file:#{jobParameters['cwd']}/#{jobParameters['inputFolder']}/#{jobParameters['inputFile']}" />

        <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="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />
            </property>
          </bean>
        </property>
    </bean>

Item writer is like so:

<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step" >
        <property name="resource" value="#{jobParameters['cwd']}/#{jobParameters['outputFolder']}/#{jobParameters['inputFile']}" />
        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
        </property>
    </bean>  

When I run this batch job, the reader reads the file properly, processor does its job but FileNotFound exception is thrown by the itemWriter

2014/06/27 18-02-31,168:OUT:ERROR[Encountered an error executing the step]
org.springframework.batch.item.ItemStreamException: Could not convert resource to file: [class path resource [S:/temp/seller-optin-batch/output/sellersToOptin_test.txt]]
    at org.springframework.batch.item.file.FlatFileItemWriter.getOutputState(FlatFileItemWriter.java:374)
    at org.springframework.batch.item.file.FlatFileItemWriter.open(FlatFileItemWriter.java:314)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
    Caused by: java.io.FileNotFoundException: class path resource [S:/temp/seller-optin-batch/output/sellersToOptin_test.txt] cannot be resolved to URL because it does not exist
        at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
        at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
        at org.springframework.batch.item.file.FlatFileItemWriter.getOutputState(FlatFileItemWriter.java:371)
        ... 58 more
    2014/06/27 18-02-31,168:ERR:ERROR[Encountered an error executing the step]

    [org.springframework.batch.item.file.FlatFileItemWriter.getOutputState threw org.springframework.batch.item.ItemStreamException: Could not convert resource to file: [class path resource [S:/temp/seller-optin-batch/output/sellersToOptin_test.txt]]]
    Batch Execution Failed!

Everytime the batch job runs, the output file does not exist yet. The itemWriter has to create it. Is it not possible using FlatFileItemWriter ?

1
Try prefix filename with file:// - Luca Basso Ricci
Well, it's definitely possible to create files with an ItemWriter; it looks to me like either the base path doesn't exist, you (or the account you're running under) don't have write permissions, or that you're using the wrong path separator for your environment. Have you tried writing to a different location? Give it a shot just using a file name and omitting the path. - Ickster
@LucaBassoRicci: that was the problem. Thanks for pointing it out. Silly mistake - chetan
@Ickster: Thanks. I did have permissions and base path exists. - chetan

1 Answers

7
votes

Adding 'file://' prefix solved my problem. Thanks @LucaBassoRicci.