使用cdi的camel-file组件过滤器

时间:2016-06-17 13:23:50

标签: apache-camel apache-camel-cdi

我使用没有Spring框架的camel(使用CDI代替)。 如何为camel-file组件设置过滤器?

我的过滤器类看起来像这样:

@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出现在日志中。

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

2 个答案:

答案 0 :(得分:1)

<强>更新 由于Camel-cdi使用JNDI-registry,因此过滤器的配置如下:

filter=#java:global/Abc/MyFilter

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

@Converter
public class MyGenericFileFilterConverter implements TypeConverters {

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

答案 1 :(得分:-1)

您是否已将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方法中。