簡體   English   中英

以編程方式調用 Apache Camel Route

[英]Invoking an Apache Camel Route programatically

我有一個獨立的 Spring-Boot Java 應用程序有很多路線。 一種駱駝路線使用設置為檢索和處理文件的 sftp 組件。

但是,我不想定期輪詢新文件,我想按需觸發或調用路由,即最有可能通過人。 例如,單擊按鈕或運行 shell 腳本。

我在文檔中沒有看到任何關於此的內容。 可能嗎?

謝謝

當您創建路由時,它已經為您的端點創建了一個使用者。 您需要做的是,而不是定義路由,而是使用 ConsumerTemplate 以編程方式調用端點並接收響應。

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;
  }
}

如果您使用 Spring 啟動,您可以簡單地將 CamelContext 自動連接到您的 bean。

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

以下是來自 Apache Camel 源的一些測試用例,

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM