Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to clean a folder in afterStep in Spring Batch

I have a job where I process and copy all files from a dir to another dir.After the step I want to delete the first directory but I get an error while trying to delete the last file processed(copied).The result is a folder with only one file that can't be deleted and this file happens to be the last processed
My StepExecutionListener looks like this:

public class CleanUpListener implements StepExecutionListener {

private static final Logger logger = LoggerFactory.getLogger(CleanUpListener.class);

private String folderToBeDeleted;

@Override
public ExitStatus afterStep(StepExecution arg0) {
    try {
        logger.info("Deleting folder: " + folderToBeDeleted + " ...");
        File file = new File(folderToBeDeleted);
        logger.info("folder contains: " + file.listFiles().length);
        FileUtils.deleteDirectory(file);
        logger.info("Deleted folder: " + folderToBeDeleted);
    } catch (IOException e) {
        logger.error("Failed to delete folder: " + folderToBeDeleted + " with exception: " + e.toString());
    }
    return null;
}

@Override
public void beforeStep(StepExecution arg0) {
    // TODO Auto-generated method stub

}

public String getFolderToBeDeleted() {
    return folderToBeDeleted;
}

public void setFolderToBeDeleted(String folderToBeDeleted) {
    this.folderToBeDeleted = folderToBeDeleted;
}

}

produces a result:

11/03/2014 13:53:56.804 [http-bio-8080-exec-2] [] INFO                 CleanUpListener - Line (29) Deleting folder: C:/Myfolder ...
11/03/2014 13:53:56.806 [http-bio-8080-exec-2] [] INFO                 CleanUpListener - Line (31) folder contains: 14
11/03/2014 13:53:56.826 [http-bio-8080-exec-2] [] ERROR                CleanUpListener - Line (35) Failed to delete folder: C:/Myfolder with exception: java.io.IOException: Unable to delete file: C:/Myfolder/14.xml

All files are deleted except the last one processed(14.xml)

UPDATE: Reader of the step:

<bean id="modelReader"
        class="org.springframework.batch.item.file.MultiResourceItemReader"
        scope="step">
        <property name="resources" value="file:${step3.reader.resource}/*.xml"></property>
        <property name="delegate" ref="staxPatentReader"></property>
        <property name="strict" value="true"></property>
    </bean>

    <bean id="staxPatentReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
        <property name="fragmentRootElementName" value="Root" />
        <property name="unmarshaller" ref="modelMarshaller" />
    </bean>

    <bean id="modelMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.sample.MyModel</value>
            </list>
        </property>
    </bean>
like image 844
dimzak Avatar asked Mar 11 '14 12:03

dimzak


1 Answers

To this kind of step use a custom Tasklet implementation.

class DeleteFilesTasklet implements Tasklet {
  execute(StepContribution, ChunkContext) throws Exception {
    // do deletion and return the right value
  }
}

and in your job.xml

<job>// previous steps
  <step id="deleteFiles">
    <tasklet ref="deleteFiles" />
  </step>
  <bean class="DeleteFilesTasklet" id="deleteFiles" />
</job>

In this way you are sure all files (and other resources) has been closed in the previous step.

like image 199
Luca Basso Ricci Avatar answered Oct 14 '22 03:10

Luca Basso Ricci