Anirban
Anirban

Reputation: 949

Transfer a file using Apache Camel file component

I am trying a demo file transfer program using Spring Boot and Apache Camel file component. I have exposed a REST Controller using Spring Boot which is calling an Apache Camel route and it is doing the file transfer. I have three files in the directory C:\CamelDemo\inputFolder namely input1.txt, input2.txt and input3.txt. I want to only transfer the file input2.txt in the output folder. My Spring Boot controller is as below:

package com.example.demo.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.ProducerTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/camel")
public class FileTransferController {

    @Autowired private ProducerTemplate producerTemplate;

    @RequestMapping(value="/file", method=RequestMethod.GET)
    public String callCamelRoute() {
        String fileName = "input2.txt";

        Map<String, Object> headerMap = new HashMap<String, Object>();
        headerMap.put("fileName", fileName);

        producerTemplate.sendBodyAndHeaders("direct:transferFile", null, headerMap);

        return "Route invoked";
    }

}

My Route is as below:

package com.example.demo.route;

import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

@Component
public class FileTransferRoute extends RouteBuilder {

    @SuppressWarnings("deprecation")
    @Override
    public void configure() {
        errorHandler(defaultErrorHandler()
            .maximumRedeliveries(3)
            .redeliverDelay(1000)
            .retryAttemptedLogLevel(LoggingLevel.WARN));

        from("direct:transferFile")
            .log("Route reached")
            .log("file:C:\\CamelDemo\\inputFolder?fileName=${in.headers.fileName}&noop=true")
            .pollEnrich("file://C:/CamelDemo/inputFolder?fileName=${in.headers.fileName}&noop=true")
            .to("file://C:/CamelDemo/outputFolder?autoCreate=false")
        .end();
    }

}

But the first time I invoke this route, the file input1.txt is getting transferred even when I have specified the fileName parameter. Please help.

Upvotes: 0

Views: 3669

Answers (1)

Screwtape
Screwtape

Reputation: 1367

I think the issue is that your file name isn't being set, because you're not telling Camel that you're using a Simple expression, rather than a fixed URI.

Looking at the manual (https://camel.apache.org/manual/latest/pollEnrich-eip.html#_using_dynamic_uris), it implies that you will need

.pollEnrich().simple("file://C:/CamelDemo/inputFolder?fileName=${in.headers.fileName}&noop=true")

to be able to use the dynamic endpoint.

Upvotes: 1

Related Questions