pfx-sftp: no method
Usage of
pfx-sftpwithout an explicit method -- consumer and producer patterns for downloading, uploading, and polling files.
Overview
The pfx-sftp component does not require an explicit "method" parameter. Its behavior is determined by whether it is used as a consumer (<from>) or a producer (<to>):
|
Position |
Role |
Behavior |
|---|---|---|
|
|
Consumer |
Downloads / polls files from the SFTP server. |
|
|
Producer |
Uploads files to the SFTP server. |
This follows the standard Apache Camel SFTP component conventions.
Consumer: Downloading Files
When pfx-sftp is used as a <from> endpoint, it connects to the SFTP server and reads files from the specified directory.
One-time download (noop)
<route id="sftpDownloadNoop">
<from uri="pfx-sftp://dummy?connection=my-sftp-conn&noop=true&fileName=data.csv"/>
<to uri="direct:processFile"/>
</route>
-
noop=true-- read the file but leave it on the server (no move, no delete). -
fileName=data.csv-- only read the specific file.
Download and move
<route id="sftpDownloadMove">
<from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&move=.done"/>
<to uri="direct:processFile"/>
</route>
-
After successful processing, the file is moved to the
.donesubdirectory on the SFTP server.
Download and delete
<route id="sftpDownloadDelete">
<from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&delete=true"/>
<to uri="direct:processFile"/>
</route>
-
delete=true-- the file is deleted from the SFTP server after successful processing.
Consumer: Polling for New Files
Polling is a consumer pattern where the route periodically checks the SFTP directory for new files.
Poll with delay and move
<route id="sftpPoll">
<from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&delay=30000&move=.done"/>
<to uri="direct:processFile"/>
</route>
-
delay=30000-- polls every 30 seconds. -
move=.done-- moves processed files to.done.
Poll with read lock
<route id="sftpPollReadLock">
<from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&delay=60000&readLock=changed&readLockCheckInterval=5000"/>
<to uri="direct:processFile"/>
</route>
-
readLock=changed-- waits until the file size stops changing before reading, preventing partial reads of files still being written. -
readLockCheckInterval=5000-- checks every 5 seconds whether the file has stopped changing.
Poll recursively
<route id="sftpPollRecursive">
<from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&delay=60000&recursive=true"/>
<to uri="direct:processFile"/>
</route>
-
recursive=true-- processes files in all subdirectories underinbound.
Producer: Uploading Files
When pfx-sftp is used as a <to> endpoint, it uploads the message body as a file to the SFTP server.
Upload with temp prefix
<route id="sftpUpload">
<from uri="direct:uploadFile"/>
<to uri="pfx-sftp://dummy/outbound?connection=my-sftp-conn&tempPrefix=.uploading"/>
</route>
-
tempPrefix=.uploading-- the file is written with a.uploadingprefix and renamed on completion. This prevents downstream systems from picking up a partially written file.
Upload with explicit file name
<route id="sftpUploadNamed">
<from uri="direct:uploadFile"/>
<setHeader name="CamelFileName">
<simple>export-${date:now:yyyyMMdd}.csv</simple>
</setHeader>
<to uri="pfx-sftp://dummy/outbound?connection=my-sftp-conn&tempPrefix=.uploading"/>
</route>
-
The
CamelFileNameheader controls the destination file name on the SFTP server.
Inline Credentials (No Connection)
If no connection parameter is provided, all host, port, and credential details must be specified directly in the URI:
<route id="sftpInline">
<from uri="pfx-sftp://user@sftp.example.com:22/data/inbound?password=secret&strictHostKeyChecking=no"/>
<to uri="direct:processFile"/>
</route>
This is functionally equivalent to the standard Camel sftp component. This approach is discouraged -- use a named connection instead to keep credentials encrypted and out of version control.
Common Pitfalls
|
Pitfall |
Recommendation |
|---|---|
|
No |
Without a temporary prefix, downstream systems may read partially written files. Always use |
|
Polling too aggressively |
Very short |
|
No |
If external systems write files slowly, use |
|
Hardcoded credentials |
Always prefer a named |
|
Forgetting |
Default is |
Error Handling
-
If a named connection cannot be found, a
NonRecoverableExceptionis thrown at route startup:Cannot find SftpConnection with name: [<name>]. -
Standard Camel SFTP error handling applies for file-level operations (read locks, permission errors, network failures).