Milioli Luca
Milioli Luca

Reputation: 309

How to create a camel route that references a specific Java method?

I want to write a Camel route that reads all xml files in a specific directory, then calls a process Java method of a class that implements Processor to do something and print the result to screen.

For example the Java class is named ScriptProcessor, and it has a process method:

public class ScriptProcessor implements Processor{

    final Script script ;


    public ScriptProcessor(Script script){
        this.script = script;
    }


    @Override
    public void process(Exchange exchange) throws Exception {
       //do something ...

    }

}

So, currently I have a camel context with a route like this:

   <camelContext xmlns="http://camel.apache.org/schema/spring">
      <route>
        <from uri="file:?noop=true"/>
        <to uri="mock:result"/>
      </route>
    </camelContext>

I suppose that all xml files are in the same directory of the file with the Camel context definition ("from" tag), and I use mock element to specify the destination of route. I don't know how call the process method of the ScriptProcessor class inwards that Camel route. It's necessary a "process" tag or something similar? Can someone help me?

Upvotes: 2

Views: 10698

Answers (2)

Milioli Luca
Milioli Luca

Reputation: 309

I solved in this way. I create a xml file with a route definition:

<route id="scriptProcessor">  
    <from uri="file:/C:/Users/milioli/Documents/NetBeansProjects/camel-rule-engines-processor/src/main/resources/samples/?noop=true"/>
    <bean beanType="com.mycompany.processor.ScriptProcessor" method="process"/> 
    <to uri="mock:result"/>  
    <onCompletion>
        <bean beanType="com.mycompany.processor.context.handler.ShutdownContextProcessor" method="process"/>
    </onCompletion>
</route>  

Then in the process method I do operations that I need. The definition of the route in this way avoid me to load the definition using the method described here.

Upvotes: 0

cslysy
cslysy

Reputation: 830

You can use processor that way:

<bean id="scriptProcessor" class="com.my.app.ScriptProcessor"/>

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="file:?noop=true"/>
    <process ref="scriptProcessor" />
    <to uri="mock:result"/>
  </route>
</camelContext>

Or use camel bean integration:

public class SomeBean {

    public void someMethod(Exchange exchange) throws Exception {
        //do something
    }
}

camel context:

<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="file:?noop=true"/>
    <bean ref="someBean" method="someMethod"/>
    <to uri="mock:result"/>
  </route>
</camelContext>

For more details please see http://camel.apache.org/bean-language.html

Upvotes: 1

Related Questions