You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Camel SFTP Consumer按修改时间预排序的技术咨询

Apache Camel SFTP Consumer 优先消费最早文件的问题与解决方案

我们的需求是让Apache Camel SFTP Consumer优先消费最早的稳定文件,当前使用Camel 4.14.5与Spring Boot 3.5.14。

现有配置及问题

配置参数

  • maxMessagesPerPoll=10
  • sortBy=file:modified
  • eagerMaxMessagesPerPoll=false

缺陷分析

场景A(显式配置eagerMaxMessagesPerPoll=false):
服务器所有文件→获取全部SftpRemoteFile条目→为每个文件创建Exchange/GenericFileMessage对象→排序→返回前10个,内存占用随文件总数递增。

场景B(默认eagerMaxMessagesPerPoll=true):
Camel在排序前仅获取10个随机文件→仅对该10个文件排序,无法保证优先消费最早文件。

临时解决方案

Camel提供preSortComparator可在Exchange创建前于GenericFile层面排序,但无法通过URI配置且无修改时间内置选项,因此通过以下代码扩展实现预排序:

CustomSortedSftpComponent:

@Component("sftp")
public class CustomSortedSftpComponent extends SftpComponent {
  @Override
  protected GenericFileEndpoint<SftpRemoteFile> buildFileEndpoint(
      String uri, String remaining, Map<String, Object> parameters) throws Exception {
    // 委托给原生SftpComponent解析URI并创建配置
    GenericFileEndpoint<SftpRemoteFile> stockEndpoint =
        super.buildFileEndpoint(uri, remaining, parameters);
    SftpEndpoint sftp = (SftpEndpoint) stockEndpoint;

    // 返回使用相同解析配置的自定义端点
    return new CustomSortedSftpEndpoint(
        uri, this, (SftpConfiguration) sftp.getConfiguration());
  }
}

CustomSortedSftpEndpoint:

public class CustomSortedSftpEndpoint extends SftpEndpoint {
  public CustomSortedSftpEndpoint(String uri, SftpComponent component,
      SftpConfiguration configuration) {
    super(uri, component, configuration);
  }

  @Override
  public RemoteFileOperations<SftpRemoteFile> createRemoteFileOperations() {
    CustomSortedSftpOperations operations = new CustomSortedSftpOperations(getProxy());
    operations.setEndpoint(this);
    return operations;
  }
}

CustomSortedSftpOperations:

public class CustomSortedSftpOperations extends SftpOperations {

  private static final Logger LOG = LoggerFactory.getLogger(CustomSortedSftpOperations.class);

  public CustomSortedSftpOperations(Proxy proxy) {
    super(proxy);
  }

  @Override
  public SftpRemoteFile[] listFiles(String path) throws GenericFileOperationFailedException {
    SftpRemoteFile[] files = super.listFiles(path);

    if (files != null && files.length > 1) {
      LOG.debug("按最后修改时间(最早优先)排序 {} 个SFTP条目", files.length);
      Arrays.sort(files, Comparator.comparingLong(SftpRemoteFile::getLastModified));
    }

    return files;
  }
}

该方案仅对SftpOperations返回的RemoteFile数组执行自定义排序,其余Camel默认行为保持不变。需注意:SftpOperations为内部实现类,未来Camel版本重构可能导致该方案失效。

技术问询

  1. 是否计划推出基于file:modified的内置预排序选项,以规避内存占用过高问题?
  2. 上述临时方案是否会影响Camel默认行为?未来升级Camel版本时是否存在兼容性风险?

内容的提问来源于stack exchange,提问作者Manikanta Bandaru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 10:40:37