Skip to content

Instantly share code, notes, and snippets.

@heiko-braun
Created December 4, 2023 10:45
Show Gist options
  • Save heiko-braun/d0bf76c6dcd6ab7059ed1032619ec185 to your computer and use it in GitHub Desktop.
Save heiko-braun/d0bf76c6dcd6ab7059ed1032619ec185 to your computer and use it in GitHub Desktop.

Keywords: sftp | camel | Apache Camel | file processing | concurrency | idempotent

Messages

user: This is my route from("sftp://userName:password@ip:22/?move=.done") .routeId("my-route-1") . How to avoid processing the same files incase of multiple instances?

assistant: To avoid processing the same files in case of multiple instances, you can use the "idempotent" read lock feature in Apache Camel's SFTP component. This feature uses an "idempotent repository" to keep track of the files that have already been processed and prevents them from being processed again.

Here's an example of how to configure the "idempotent" read lock using Java DSL and the maven artifact reference for the necessary Camel SFTP dependency:

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.file.GenericFile;
import org.apache.camel.component.file.GenericFileFilter;

public class MyRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("sftp://userName:password@ip:22/<my-folder>?move=.done&readLock=idempotent")
            .routeId("my-route-1")
            .<processing-logic>
            // Add the processing logic here
    }
}

Maven artifact reference:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>x.x.x</version> <!-- Replace with the appropriate version -->
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-sftp</artifactId>
    <version>x.x.x</version> <!-- Replace with the appropriate version -->
</dependency>

In the Java DSL code above, the "idempotent" read lock is set using the readLock=idempotent option in the SFTP URI. This option ensures that only files that have not been processed before are picked up for processing.

Additionally, the Maven artifact references for the Camel core and SFTP component dependencies are provided for your convenience.

Let me know if you need further assistance!

@orpiske
Copy link

orpiske commented Dec 4, 2023

I have some concerns about this answer. The question says: "How to avoid processing the same files incase of multiple instances?"

  1. I think idempotent read lock is only for the file component.
  2. Because it says "processed", I am under the impression that the option idempotent=true would be better.

Probably needs further review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment