Lukas Karmanovas
Lukas Karmanovas

Reputation: 409

How to move file to error directory if file name patter does not match with Camel

I want to move files to error directory if file name patter does not match provided regex. I have a RegexFileFilter<T> class which implements GenericFileFilter<T>. This is the accept method.

    public boolean accept(GenericFile<T> file) {
        Matcher matcher = getFileNameRegexp().matcher(file.getFileName());
        boolean matches = matcher.matches();
        if (!matches) {
            log.error("File named [{}] is not imported because it doesn't matches the expected pattern: [{}].", file.getFileName(), getFileNameRegexp().pattern());
        }
        return matches;
    }

Problem is that if matches = false the file will be taken once again when scheduler restarts it. I have tried to throw an exception after logging error

throw new PatternSyntaxException("File name doesn't match expected pattern ", getFileNameRegexp().toString(), -1);

And created configuration which should move file to errorLocation if exception PatternSyntaxException is thrown

onException(PatternSyntaxException.class)
                    .useOriginalMessage()
                    .handled(true)
                    .to(getFTPErrorLocation());

The problem is that when I throw exception Camel lib will eat that exception and exclude file which threw that exception. Is there a way to bypass it or maybe other way of using an exclude to move file?

I have also tried adding it URI format &scheduler=spring&scheduler.cron=0+0/1+*+*+*+?&readLock=rename&move=processed&disconnect=true&filter=#fileFilter&exclude=.*(\.GL1025\.).*&move=error But it did not help. Any suggestions?

Edit

Here is my route part.

    public void configure() {
        addFileAlreadyProcessedExceptionHandler();
        addEmptyFileExceptionHandler();
        addFailedFileTransformationExceptionHandler();
        addFailedFileNamePatternExceptionHandler();
        addTransactionProcessExceptionHandler();
        addFileDownloadRoute();
        from(getLocalFileSystemDownloadLocation())
                .onCompletion()
                .bean(CreditCardImportLogServiceBean.class)
                .process(fileImportStatisticProcessor)
                .end()
                .transacted()
                .routeId(getRouteId())
                .bean(CreditCardStatusServiceBean.class)
                .to(xsltProcess)
                .process(transactionCounterProcessor)
                .split(xPathBuilder)
                .convertBodyTo(String.class, "ISO-8859-1")
                .unmarshal(jaxb)
                .bean(ImportLogProcessor.class)
                .bean(CurrencyCodeServiceBean.class)
                .bean(CCTransactionConverterProcessor.class)
                .bean(CreditCardTypeMapperServiceBean.class)
                .bean(CreditCardImporterServiceBean.class);
    }

    protected void addFileDownloadRoute() {
        from(getFtpConfiguration())
                .routeId(getDownloadRouteId())
                .transacted()
                .to(getLocalFileSystemDownloadLocation());
    }

    protected void addFailedFileNamePatternExceptionHandler() {
        onException(PatternSyntaxException.class)
                .useOriginalMessage()
                .handled(true)
                .to(getFTPErrorLocation());
    }

Every other exception works correctly, the only part that does not work is addFailedFileNamePatternExceptionHandler or at least does not work as I intend it to work.

Upvotes: 1

Views: 211

Answers (1)

Claus Ibsen
Claus Ibsen

Reputation: 55525

You cannot do this, as the file is not accepted, and therefore not really processed by Camel, unless you turn on bridge error handler on the consumer.

So instead you may want to use the filter EIP pattern afterwards where you check the file name, and then throw an exception if it fails etc.

Upvotes: 1

Related Questions