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

自定义AlwaysDeleteProcessStrategy在Camel字符串端点失效求助

问题:自定义GenericFileProcessStrategy在字符串端点配置中失效

我创建了一个继承自GenericFileDeleteProcessStrategy的自定义处理策略类AlwaysDeleteProcessStrategy:

@Component
public class AlwaysDeleteProcessStrategy<T> extends GenericFileDeleteProcessStrategy<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(AlwaysDeleteProcessStrategy.class);

    @Override
    public void rollback(GenericFileOperations<T> operations, GenericFileEndpoint<T> endpoint, Exchange exchange, GenericFile<T> file) throws Exception {
        LOGGER.info("Deleting file despite exception");
        super.commit(operations, endpoint, exchange, file);
    }
}

使用Endpoint DSL方式配置端点时该类可正常工作,但通过字符串格式构建端点时失效:

// 正常工作
from(file("src/test/resources/input").advanced().processStrategy(new AlwaysDeleteProcessStrategy()))  

// 失效
from("file://src/test/resources/input?processStrategy=#alwaysDeleteProcessStrategy")

此时抛出如下异常:

Caused by: org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: alwaysDeleteProcessStrategy of type: org.apache.camel.component.file.GenericFileProcessStrategy
    at org.apache.camel.support.CamelContextHelper.mandatoryLookupAndConvert(CamelContextHelper.java:253)
    at org.apache.camel.support.EndpointHelper.resolveReferenceParameter(EndpointHelper.java:376)
    at org.apache.camel.support.EndpointHelper.resolveReferenceParameter(EndpointHelper.java:336)
    at org.apache.camel.support.component.PropertyConfigurerSupport.property(PropertyConfigurerSupport.java:55)
    at org.apache.camel.component.file.FileEndpointConfigurer.configure(FileEndpointConfigurer.java:131)
    at org.apache.camel.support.PropertyBindingSupport.setSimplePropertyViaConfigurer(PropertyBindingSupport.java:733)
    ... 43 common frames omitted

我已尝试手动创建Bean(即使@Component注解已存在),但问题仍未解决。如何让两种配置方式都正常工作?


解决方案

问题根源

Camel在解析字符串端点的processStrategy参数时,会严格按照GenericFileProcessStrategy类型查找Bean,但你的泛型类AlwaysDeleteProcessStrategy<T>在Spring容器中注册的是带泛型参数的类型,导致Camel的类型匹配和转换逻辑失败。

方案1:移除泛型参数,使用具体类型

修改自定义策略类,将泛型替换为具体的Object类型,这样Spring注册的Bean会被识别为兼容GenericFileProcessStrategy的类型:

@Component
public class AlwaysDeleteProcessStrategy extends GenericFileDeleteProcessStrategy<Object> {

    private static final Logger LOGGER = LoggerFactory.getLogger(AlwaysDeleteProcessStrategy.class);

    @Override
    public void rollback(GenericFileOperations<Object> operations, GenericFileEndpoint<Object> endpoint, Exchange exchange, GenericFile<Object> file) throws Exception {
        LOGGER.info("Deleting file despite exception");
        super.commit(operations, endpoint, exchange, file);
    }
}

方案2:显式注册Bean并指定目标类型

保留泛型类,但通过@Bean注解显式注册Bean,指定返回类型为GenericFileProcessStrategy<?>,确保Camel能识别该Bean:

@Configuration
public class CamelConfig {

    @Bean("alwaysDeleteProcessStrategy")
    public GenericFileProcessStrategy<?> alwaysDeleteProcessStrategy() {
        return new AlwaysDeleteProcessStrategy<>();
    }
}

额外检查点

  • 确认Spring组件扫描范围包含该策略类所在的包
  • 可以通过@Component("alwaysDeleteProcessStrategy")显式指定Bean名称,避免Spring自动生成的名称出现偏差

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 22:10:44