Apache Camel pollEnrich is not copying all the fil

2019-09-12 03:13发布

问题:

I've a camel route which looks like below

from("activemq:queue:upload" )
    .pollEnrich().simple("file:basePath/${header.ID}?noop=true&recursive=true")
    .aggregationStrategy(new ExampleAggregationStrategy()) 
    .timeout(2000) 
 .toD("ftp:${header.destinationURI}")

In my file system file:basePath/${header.ID} contains multiple folders. When above route is executed, only 1st file from the 1st folder will be copied to ftp server. Remaining folders(with subfolders) aren't getting copied to ftp server!

And ExampleAggregationStrategy() class's aggregate() method looks like below

@Override
        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
            String destinationURI = "populatedURI";

        oldExchange.setOut(newExchange.getIn());
        oldExchange.getOut().setHeader("ID",  oldExchange.getIn().getHeader("ID"));
        oldExchange.getOut().setHeader("destinationURI", destinationURI);
        oldExchange.setFromEndpoint(newExchange.getFromEndpoint());
        oldExchange.setFromRouteId(newExchange.getFromRouteId());

            return oldExchange;
        }

I've tried setting properties and onCompletions as well. Still no luck ! Am I missing anything in aggregationStrategy ?
How to copy all the files and folders successfully using pollEnrich?

回答1:

I know it's an old question but I had similar problem and I solve it using loopDoWhile(). Here's my route:

from("direct:start")    
    .setProperty("StartDate", simple("${date:now:yyyy-MM-dd'T'HH-mm-ss}"))
    .to("direct:download");

from("direct:download")
    .loopDoWhile(body().isNotNull())
        .pollEnrich()
        .simple("sftp://{{remote.user}}@{{remote.url}}/{{remote.directory}}?password={{remote.password}}"
                + "&move=.done/${property.StartDate}"
                + "&localWorkDirectory=work/tmp"
                + "&autoCreate=false"
                + "&consumer.bridgeErrorHandler=true"
                + "&throwExceptionOnConnectFailed=true"
                + "&recursive=true"
        )
        .timeout(0)
        .choice()
            .when(body().isNotNull())
                .to("file:work/inbox/?fileName=${file:name}")
            .otherwise()
                .end()
    .end();

I hope it will be helpful for someone.



回答2:

pollEnrich always works with single file. The intention behind this is get the content from endpoint (A) and then get content from poller enrich (B) and then we aggregate this two content and write it to other endpoint.

In your case A would be from("activemq:queue:upload" ) body and B would be ("file:basePath/${header.ID}?noop=true&recursive=true") . If it finds multiple files , it selects only one file and you can play around aggregator and write it to FTP.