StaxEventItemWriter - File not writable issue

孤街醉人 提交于 2020-06-28 07:34:07

问题


I have an item writer configured as below which generates an xml:

<beans:bean id="delegateItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter" scope="step">
    <beans:property name="resource" value="file:#{jobParameters['OutputDirPath']}${myFileName}" /> 
    <beans:property name="overwriteOutput" value="true"/>
    <beans:property name="rootTagName" value="disclosure-feed" />
    <beans:property name="rootElementAttributes" >
        <beans:map>
            <beans:entry key="xmlns:xsi" value="http://www.w3.org/2001/XMLSchema-instance" />               
            <beans:entry key="xsi:noNamespaceSchemaLocation" value="XYZ.xsd"/>
        </beans:map>
    </beans:property>
    <beans:property name="marshaller" ref="xmlMarshaller" />  
</beans:bean>

Even though every thing seems correct, sometimes on restarting a job after fixing a failure of previous run, I get below error:

2013-07-19 02:14:34,921 [main] ERROR org.springframework.batch.core.step.AbstractStep  - Encountered an error executing the step
org.springframework.batch.item.ItemStreamException: File is not writable: [/myOutputDir/myOutput.xml]

When I manually remove the job entries from batch_ tables so that the job starts afresh from the start instead of from where it failed during last run, the file gets generated as expected.

What is the reason for this issue? How to resolve it ? Is there some configuration stuff I am missing ?

Thanks for reading!


回答1:


Looks like a bug in org.springframework.batch.item.utilFileUtils.java, Spring batch is expecting the file already created in the first run.

   if (!restarted) {
                if (file.exists()) {
                   ...
                }
                if (file.getParent() != null) {
                    new File(file.getParent()).mkdirs();
                }
                file.createNewFile();
   }

   if (!file.canWrite()) {
            throw new ItemStreamException("File is not writable...");
   }

If it is a restart, no file is created, hence you are getting the exception.




回答2:


I came across same problem. I created a new file in Java Config class to resolve the issue. Now whenever Job is executed, while initializing config a new file is created. Thus on restart also the file is already created.

FlatFileItemWriter<List<String>>  flatFileWriter= new FlatFileItemWriter<>();
DelimitedLineAggregator<List<String>> delimittedLineAggregator = new DelimitedLineAggregator<>();
delimittedLineAggregator.setDelimiter(System.lineSeparator());
Resource res = new FileSystemResource(file);
res.getFile().createNewFile();
flatFileWriter.setResource(res);

I don't see any issues in creating new file for my use case.




回答3:


pls call preventRestart() method on jobBuilderFactory while defining job. As every time job runs, restart property is true. Hence it is expecting the file to be present.



来源:https://stackoverflow.com/questions/17741511/staxeventitemwriter-file-not-writable-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!