1

How to move a File in a FTP route to a different directory based on the error?

from("sftp://XXX@safsdf.de/dir/?delay=2s&move=done&moveFailed=failImport")
        .split()
        .body()
        .process(e -> {
            String fileName = (String) e.getIn().getHeader(Exchange.FILE_NAME);
             // do some magic which could throw a exception
        })
        .log("Imported file ${file:name} completely.");
Zarathustra
  • 2,853
  • 4
  • 33
  • 62

2 Answers2

0

Check out the onException handler in Camel. It allows you to catch specific exceptions and then route accordingly. There is also try-catch-like syntax if you want the error handling to be more in-lined with your Camel route.

onException(BadThingHappenedException.class).to(file:///errorFolder)

Documentation:

onException

try-catch-finally

Custom file processing strategy

Matt Pavlovich
  • 4,087
  • 1
  • 9
  • 17
  • do I have to use the whole sftp configuration in `to` again? – Zarathustra Jun 19 '17 at 16:11
  • seems like the original file does not get deleted/moved as well. – Zarathustra Jun 19 '17 at 16:53
  • 1. Yes, if you want to move the file to another folder on the remote system, you would specify another sftp:// in the "to" – Matt Pavlovich Jun 19 '17 at 16:59
  • 2. Check out the delete=true flag in the Camel FTP2 documentation: http://camel.apache.org/ftp2.html – Matt Pavlovich Jun 19 '17 at 17:00
  • but using this combination would result in the file stored in `/errorFolder` and in /done. I do not want to store the file in `failImport` neither `done` when such an error occurs – Zarathustra Jun 19 '17 at 17:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147101/discussion-between-zarathustra-and-matt-pavlovich). – Zarathustra Jun 19 '17 at 17:10
  • You'll have to pick on handler or the other. Since you want to have custom error handling and folder movement, you'll need to handle all scenarios-- including success on your own in the route, or implement a custom GenericFileProcessStrategy and reference that in the uri instead of 'move' and 'moveFailed'. – Matt Pavlovich Jun 19 '17 at 17:15
0

It my be archived with a onException, try-catch-finally or Custom file processing strategy but none of it is really handy.

I came up with this simple code:

@Override
public void configure() throws Exception {
    errorHandler(loggingErrorHandler(logger));
    from("sftp://XXX@safsdf.de/dir/?delay=2s&move=${in.header.outDirectory}&moveFailed=failImport")
            .process(exchange -> exchange.setProperty(originalMessage, exchange.getIn()))
            .split()
            .body()
            .process(e -> {
                String fileName = (String) e.getIn().getHeader(Exchange.FILE_NAME);
                Message origMsg = (Message) e.getProperty(originalMessage);
                try {
                    // do what ever
                    origMsg.setHeader(outDirectory, "done/" + fileName);
                } catch (KaboomException ex) {
                    origMsg.setHeader(outDirectory, "retry/" + fileName);
                }
            })
            .log("Imported file ${file:name} completely.");

}

Its important to set the header on the original message.

Zarathustra
  • 2,853
  • 4
  • 33
  • 62