Reputation: 1498
I am configuring a route in Apache Camel that a single from() but multiple to() within it. My task is to create a file (works already) and either place it in some folder (works) or send it over TCP.
How to define a route that does create the file and then either save it to disk or send it via TCP?
I did try with a processor that creates the file, but i want to create the file only once, and then use camel to store or send. I hope somebody understands my words. Currently I am checking config properties and get into trouble when both endpoints (file and tcp) are enabled, i get a camel error.
My goal is to use the camel architecture.
@Component
public class HL7DirectImportRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
String gdtexportFolder = propertyService.getExportFolderGDT();
// inspect gdt file and create a hl7 message and send it
if (propertyService.isExportMessageHl7Enabled()) {
log.info("export hl7 message (mllp) is enabled....");
throw new L7Exception("not yet implemented");
} else {
log.info("export hl7 message (mllp) is disabled");
}
// the customer wants to have the hl7 messages as file to be exchanged via a
// shared network folder
if (propertyService.isExportMessageFileEnabled()) {
log.info("export hl7 message file is enabled....");
from("file:" + padsyGdtExportFolder).log(
"File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelFileLastModified}")
.process(new GDTFileProcessor());
} else {
log.info("export hl7 message file is disabled");
}
if (propertyService.isExportPdfFileEnabled()) {
log.info("export pdf file is enabled....");
from("file:" + padsyGdtExportFolder).log(
"File event: ${header.CamelFileEventType} occurred on file ${header.CamelFileName} at ${header.CamelFileLastModified}")
.process(new CopyPDFFileProcessor(propertyService));
} else {
log.info("export pdf file is disabled");
}
}
Edit: Added coded and remove confidential parts.
I don't know how to chanage the route dynamically, according to the configuration of the application
What i want to have in pseudocode: from(file:input).createMessage() // either place the file on disk or send it // or just copy the file
Upvotes: 0
Views: 829
Reputation: 2187
You can create a file using camels file producer endpoint eg. to(file:path?filenName=example.txt)
. The file will have message body as its content. When you create the file can controlled with the consumer endpoint which could be a lot of things like timer, schedule, message queue event etc.
Example using timer:
from("timer:createFileTimer?repeatCount=1")
.routeId("createFileTimer")
.setBody(constant("Hello world"))
.to("file:{{input.path}}?fileName=Hello.txt")
;
This creates a file and saves it to the disk using path defined in input.path property of application.properties file used by spring boot.
To move a file from one location to another you can simply use:
from("file:{{input.path}}?delete=true")
.routeId("moveFileToPath")
.to("file:{{output.path}}")
;
Note that technically this will read the file from input.path and write it to output.path. With delete=true
this will delete the original file from input.path once done.
To send file using TCP you can use FTP-component preferably with SFTP using strong RSA private key to authenticate.
from("file:{{input.path}}?delete=true")
.routeId("moveFileToPath")
.to("sftp:{{sftp.host}}:{{sftp.port}}"
+ "?username={{sftp.username}}&privateKeyFile={{sftp.keyfile.path}}")
;
Camel-ftp maven dependency for Spring boot:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-ftp-starter</artifactId>
<version>x.x.x</version>
</dependency>
or just copy the file
If you're using file consumer endpoint (from("file:path")
one key thing to understand is that it will consume the file after route has finished processing the file. This means that the file will by default be moved to hidden .camel
folder in the input.path directory to prevent it from getting consumed again. This can lead to unexpected behavior if your goal is to copy a file.
Using noop=true
setting will prevent camel from moving or deleting the original file and use in-memory IdempotentRepository to keep track of consumed files to prevent them from being consumed again.
To move processed files to custom location you can use move=/some/path/for/processed/files/${file:name}
setting with the file consumer endpoint.
Upvotes: 2