0
votes

I need some help with deleting the file in Mule application. Here is my use case:

  1. I get a file using File Inbound Endpoint, get the data and upload the data to the database after doing some validations on the data.

  2. I would like to delete the file after uploading to database is successful. I tried using java.io.File.delete(as suggested in the this stackoverflow post) but it's not working.

Mule - Delete files with Files Connector

I am using Mule 3.3.2 and running the server on my local machine. (I am able to delete the file when I delete using a simple Java program.)

Could you please help?

----UPDATE---- This is what I have in my OnCall() method of the Java component I am using to delete the file. isDeleted returns false.

@Override
public Object onCall(MuleEventContext eventContext) throws Exception {

  String fileName = (String) eventContext.getMessage().getProperty(
            "originalFilename");

    filePath = "C:\\REP-UNZIP\\";
    File file = new File(filePath + fileName);

    boolean isDeleted = false;
    isDeleted = java.nio.file.Files.deleteIfExists(file.toPath());
    if (file.isFile()) {
        isDeleted = file.delete();
    }

// isDeleted = FileUtils.deleteQuietly(file); LOG.info("isDeleted = " + isDeleted);

    return Boolean.valueOf(isDeleted);
}
1
How about you read file, move it to backup directory, if file insert in db unsuccessful, move the original file to working directory. - Charu Khurana
Thanks Charu! Let me try this. If you have tried this use case, would you mind sharing the code. - user6042886

1 Answers

0
votes

Accessing a file with file processor in the middle of a flow is only possible using custom components. For your use case you could use the requester module.

This will allow you to call a file inbound component in the middle of the flow, and you can have the file processor configured to delete the file after read. The overhead is that the file will be put in memory again and after deleted it.

An alternative is just to create a simple java class that does the delete for you, it will be just few lines and you can use it everywhere in your flows without degradation of performances.