【问题标题】:camel-file component filter with cdi带有 cdi 的骆驼文件组件过滤器
【发布时间】:2016-06-17 13:23:50
【问题描述】:

我正在使用没有 Spring 框架的骆驼(使用 CDI 代替)。 如何为骆驼文件组件设置过滤器?

我的过滤器类如下所示:

@Named
@Stateless
public class MyFilter<T> implements GenericFileFilter<T> {
   System.out.println("MyFilter was triggered");
  .......

所以我尝试了这个:

<route>
   <from uri="file://somewhere?filter=#myFilter"/>
   <to uri="...."/>
 </route>

但我得到了:

java.lang.IllegalArgumentException: Could not find a suitable setter for
property: filter as there isn't a setter method with same type: 
java.lang.String nor type conversion possible: No type converter 
available to convert from type: java.lang.String to the required type:
org.apache.camel.component.file.GenericFileFilter with value #myFilter

我错过了什么?

更新:

请注意,bean 已注册。如果我使用:

<to uri="ejb:java:global/Abc/MyFilter?method=accept"/>

然后MyFilter was triggered 出现在日志中。

所以问题在于配置文件组件过滤器。

【问题讨论】:

    标签: apache-camel apache-camel-cdi


    【解决方案1】:

    更新: 由于 Camel-cdi 使用 JNDI-registry,过滤器配置如下:

    filter=#java:global/Abc/MyFilter
    

    由于我不使用 Spring 并且过滤器参数正在等待一个实例,而不仅仅是一个类名,因此需要一个 TypeConverter

    @Converter
    public class MyGenericFileFilterConverter implements TypeConverters {
    
       @Converter
       public static GenericFileFilter toMYFilter(String filter){
          return new MyFilter();
       }
    }
    

    【讨论】:

    • 这不是一个好的解决方案。您正在设置一个从 StringGenericFileFilter 的转换器,该转换器对您正在使用的实现进行硬编码。如果您要添加一个新的GenericFileFilter,这将不起作用。正如@Souciance 所说,您的问题是myFilter bean 未注册。创建注册表并将其添加到上下文中可能不是您的首选方法,但您需要使用 Spring,或者找出 Camel CDI 设置中的问题。您会注意到在上面的示例中,当您说 bean 已注册时,您使用的不是它的 bean 名称,而是完整路径。
    【解决方案2】:

    您是否将 myFilter 添加到您的注册表中?

    final CamelContext camelContext = getContext();
    final org.apache.camel.impl.SimpleRegistry registry = new org.apache.camel.impl.SimpleRegistry();
    final org.apache.camel.impl.CompositeRegistry compositeRegistry = new org.apache.camel.impl.CompositeRegistry();
    compositeRegistry.addRegistry(camelContext.getRegistry());
    compositeRegistry.addRegistry(registry);
    ((org.apache.camel.impl.DefaultCamelContext) camelContext).setRegistry(compositeRegistry);
    registry.put("myFilter", new MyFilter()); 
    

    那部分应该在你的 routeBuilder 的 configure 方法中。

    【讨论】:

    • 问题是关于camel-cdi,所以你的回答有点离题。
    • 是的,但是您的错误表明注册表中不存在对 myFilter 的引用,以便 Camel 找到它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2014-11-03
    相关资源
    最近更新 更多