0

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.

sinclair
  • 2,812
  • 4
  • 24
  • 53

2 Answers2

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

sinclair
  • 2,812
  • 4
  • 24
  • 53
  • This is not a good solution. You're setting up a converter from a `String` to a `GenericFileFilter` that hard codes the implementation you're using. If you were to add a new `GenericFileFilter`, this wouldn't work. As @Souciance said, your problem is that the `myFilter` bean isn't registered. Creating a registry and adding it to the context may not be your preferred method, but you either need to use Spring, or work out what's broken in your Camel CDI setup. You'll notice in your above example, when you say the bean is registered, you're not using it's bean name, but it's full path. – AndyN Jun 21 '16 at 08:48
-1

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.

Souciance Eqdam Rashti
  • 3,143
  • 3
  • 15
  • 31