问题
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