0

I have a stand-alone Spring-Boot Java Application with many routes. One camel route uses the sftp component which is set up to retrieve and process files.

However, I do not want to periodically poll for new files, I would like to trigger or invoke the route on demand i.e via a human most probably. Button click or run a shell script for example.

I didn't see anything in the documentation about this. Is it possible?

Thanks

user1472672
  • 313
  • 1
  • 9

1 Answers1

0

When you create a route, it already creates a consumer for your endpoint. What you need to do is, instead of defining a route, use the ConsumerTemplate to invoke the endpoint programmatically and receive the response.

public class MySFTPInvoker {

  private CamelContext camelContext;

  public String invoke() {
    ConsumerTemplate consumer = camelContext.createConsumerTemplate();
    String out = consumer.receiveBody(
                "sftp://{{ftp.server.host}}:{{ftp.server.port}}/{{ftp.root.dir}}? 
                username={{user}}&password={{password}}", 5000,
                String.class); //5000 is the receive timeout
   return out;
  }
}

If you are using Spring Boot, you could simply autowire the CamelContext in to your bean.

https://camel.apache.org/manual/consumertemplate.html

Here are some test cases from the Apache Camel source,

https://github.com/apache/camel/blob/main/components/camel-ftp/src/test/java/org/apache/camel/component/file/remote/sftp/integration/SftpConsumeTemplateIT.java

Srini
  • 420
  • 1
  • 5
  • 17