pfx-sftp: no method

pfx-sftp: no method

Usage of pfx-sftp without 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

<from uri="pfx-sftp://..."/>

Consumer

Downloads / polls files from the SFTP server.

<to uri="pfx-sftp://..."/>

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)

XML
<route id="sftpDownloadNoop">
    <from uri="pfx-sftp://dummy?connection=my-sftp-conn&amp;noop=true&amp;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

XML
<route id="sftpDownloadMove">
    <from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&amp;move=.done"/>
    <to uri="direct:processFile"/>
</route>
  • After successful processing, the file is moved to the .done subdirectory on the SFTP server.

Download and delete

XML
<route id="sftpDownloadDelete">
    <from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&amp;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

XML
<route id="sftpPoll">
    <from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&amp;delay=30000&amp;move=.done"/>
    <to uri="direct:processFile"/>
</route>
  • delay=30000 -- polls every 30 seconds.

  • move=.done -- moves processed files to .done.

Poll with read lock

XML
<route id="sftpPollReadLock">
    <from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&amp;delay=60000&amp;readLock=changed&amp;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

XML
<route id="sftpPollRecursive">
    <from uri="pfx-sftp://dummy/inbound?connection=my-sftp-conn&amp;delay=60000&amp;recursive=true"/>
    <to uri="direct:processFile"/>
</route>
  • recursive=true -- processes files in all subdirectories under inbound.

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

XML
<route id="sftpUpload">
    <from uri="direct:uploadFile"/>
    <to uri="pfx-sftp://dummy/outbound?connection=my-sftp-conn&amp;tempPrefix=.uploading"/>
</route>
  • tempPrefix=.uploading -- the file is written with a .uploading prefix and renamed on completion. This prevents downstream systems from picking up a partially written file.

Upload with explicit file name

XML
<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&amp;tempPrefix=.uploading"/>
</route>
  • The CamelFileName header 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:

XML
<route id="sftpInline">
    <from uri="pfx-sftp://user@sftp.example.com:22/data/inbound?password=secret&amp;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 tempPrefix on uploads

Without a temporary prefix, downstream systems may read partially written files. Always use tempPrefix for producer endpoints.

Polling too aggressively

Very short delay values increase load on the SFTP server. Use a reasonable interval (e.g., 30--60 seconds) unless near-real-time pickup is required.

No readLock when source is still writing

If external systems write files slowly, use readLock=changed to avoid reading incomplete files.

Hardcoded credentials

Always prefer a named connection over inline credentials in the URI.

Forgetting strictHostKeyChecking in production

Default is false. Enable it in production to prevent MITM attacks.

Error Handling

  • If a named connection cannot be found, a NonRecoverableException is 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).