Hello everyone, In this post, you will learn How to Call a Method using the Class Component in Apache Camel. The example has been tested and shared in the post.

Maven Dependencies

<dependency>
<groupid>org.apache.camel</groupid>
<artifactid>camel-core</artifactid>
<version>${camel-version}</version>
</dependency>

Service Class

package com.dineshkrish;
/**
* 
* @author Dinesh Krishnan
*
*/
public class MyService {
public void doSomething(Object message) {
System.out.println(message);
}
}

Example Program

package com.dineshkrish;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
/**
* 
* @author Dinesh Krishnan
*
*/
public class CallMethodCamel_1 {
public static void main(String[] args) throws Exception {
// creating a camel context
CamelContext context = new DefaultCamelContext();
// adding the routes to context
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("class:com.dineshkrish.MyService?method=doSomething");
}
});
// start the context
context.start();
// creating a producer template
ProducerTemplate template = context.createProducerTemplate();
// trigger the route
template.sendBody("direct:start", "Your message goes here....");
// stop the context
context.stop();
}
}

Output

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Your message goes here....

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *