camel-file component filter with cdi

本秂侑毒 提交于 2019-12-12 03:46:34

问题


I'm using camel without the Spring framework (using CDI instead). How can I set a filter for the camel-file component?

My filter class looks like this:

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

So I tried this:

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

But I'm getting:

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

What am I missing?

Update:

Please note that the bean is registered. If I use:

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

then MyFilter was triggered is showing up in the log.

So the problem is about configuring the file component filter.


回答1:


Update: Since Camel-cdi uses JNDI-registry, the filter is configured like this:

filter=#java:global/Abc/MyFilter

Since I do not use Spring and the filter parameter is awaiting an instance and not only a classname, a TypeConverter is necessary

@Converter
public class MyGenericFileFilterConverter implements TypeConverters {

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




回答2:


Did you add myFilter to your registry?

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()); 

That part should be in the configure method of your routeBuilder.



来源:https://stackoverflow.com/questions/37882752/camel-file-component-filter-with-cdi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!