Sam Berchmans
Sam Berchmans

Reputation: 127

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

Upvotes: 0

Views: 2378

Answers (2)

Chin Huang
Chin Huang

Reputation: 13860

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);

Upvotes: 2

burki
burki

Reputation: 7025

I don't know if these dynamic file URIs in from work, but at least the Camel File documentation states

Also, the starting directory must not contain dynamic expressions with ${ } placeholders. Again use the fileName option to specify the dynamic part of the filename.

So the docs are suggesting to change

from("file:tmp/${header.inPath}")

into

from("file:tmp?fileName=${header.inPath}")

The fileName option can be a relative path (not just a filename).

If that change works for you, your question becomes obsolete because the route URI is no more dynamic.

.withHeader("inPath",inPath)
.to("file:tmp")

Upvotes: 0

Related Questions