Search code examples
apache-camelspring-batchspring-camel

Calling a camel route using Producer Template


My use case is based on the rest controller input I need to fetch or move files from different source system to destination system.

Route :-

@Component
public class MoveFile extends RouteBuilder {
@override
public void configure() throws Exception {

from("file:tmp/${header.inPath}")
    .to("file:/tmp${header.outPath}?fileName=${header.fileName}")
    .setBody().constant("File - ${header.inPath}/${header.fileName} Moved Succesfully")

}
}

My rest controller will pass the jobName along the getMapping to invoke this specific route inPath , outPath and File Names

@Resource(name=RouteProperties)
private Prosperties props;

@GetMapping("/runJob/{jobToInvoke}")
public String runJob (@PathVariable final String jobToInvoke){
String inPath=props.getProperty("inPath"+jobToInvoke)
String outPath=props.getProperty("outPath"+jobToInvoke)
String fileName=props.getProperty("fileName"+jobToInvoke)

String jobStatus = ProducerTemplate.withHeader("inPath",inPath)
                   .   
                   .
                   .to(??)
                   .request(String.class)
}

I need help to use Producer Template to pass the properties using to ? I tried some search on the google, but there is an example available in youtube (link) , But in that Video it is calling uri , (Direct:sendMessage) and from in the route also has that. How to handle in this scenario ? Thanks in Advance


Solution

  • A route beginning with a direct: endpoint can be invoked programmatically from Java code. In the route, the pollEnrich component invokes a consumer endpoint to read a file and replace the exchange message body with the file contents.

    from("direct:start")
        .pollEnrich().simple("file:/tmp?fileName=${header.inPath}")
        .toD("file:/tmp?fileName=${header.outPath}")
        .setBody().simple("File - ${header.inPath} Moved Successfully");
    

    To invoke the route from Java code:

    String jobStatus = producerTemplate.withHeader("inPath", inPath)
        .withHeader("outPath", outPath)
        .to("direct:start")
        .request(String.class);