javaapache-camelintegration-patterns

Camel: pollEnrich and access to the Exchange


I have this route

from(URI_WEBSERVICE)
.convertBodyTo(Entrada.class)
.process(new ProcessorTratarWS()) 
.pollEnrich("ftp://10.100.8.2/entradaCamel?username=USER&password=PASSWORD&delete=true&fileName=${property.archivoRespuesta}", timeOut, new EstrategiaConfirmacion())
.to(WS_RESPONDER)

In ProcessorTratarWS() I set the value of property.archivoRespuesta and is the name of the file that the pollEnrich should donwload.

But, documentation says that "PollEnrich does not have access to the Exchange". It means the PollEnrich can't read the value of ${property.archivoRespuesta}

Are there some alternative ways to do in Camel the same thing I'm trying?

Thanks!


Solution

  • From http://camel.apache.org/content-enricher.html

    ... Instead of using enrich you can use Recipient List and have dynamic endpoints and define an AggregationStrategy on the Recipient List which then would work as a enrich would do. ...

    try something like:

    from(URI_WEBSERVICE)
    .convertBodyTo(Entrada.class)
    .process(new ProcessorTratarWS()) 
    .recipientList(simple("ftp://10.100.8.2/entradaCamel?username=USER&password=PASSWORD&delete=true&fileName=${property.archivoRespuesta}")).aggregationStrategy(new EstrategiaConfirmacion())
    .to(WS_RESPONDER)
    

    Edit:

    The above code is to save file in FTP server. If you want to poll file from the FTP server you can try

            from(URI_WEBSERVICE)
                .convertBodyTo(Entrada.class)
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        // logic of ProcessorTratarWS goes here
                        ConsumerTemplate consumer=exchange.getContext().createConsumerTemplate();
                        String filename=exchange.getProperty("archivoRespuesta",String.class);                  
                        Object file=consumer.receiveBody("ftp://10.100.8.2/entradaCamel?username=USER&password=PASSWORD&delete=true&fileName="+filename,timeOut);
                        // logic of EstrategiaConfirmacion goes here
                }
    
            })
            .to(WS_RESPONDER);  
    

    Disclaimer: I have not used polling consumer much and there could be more elegant/efficient solution