Creating Routes with RouteBuilder
Let’s dive into the practical aspects of using the RouteBuilder class to define routes in Camel. In every example main class will be the same
Java
import org.apache.camel.CamelContext; import org.apache.camel.impl.DefaultCamelContext; public class MyAppication { public static void main(String[] args) throws Exception { CamelContext context = new DefaultCamelContext(); context.addRoutes( new MyCamelRoute()); context.start(); Thread.sleep( 7000 ); context.stop(); } } |
Example 1: Basic Route
In this example, we create a simple Camel route that listens to a file endpoint, processes the files, and moves them to another directory.
Java
import org.apache.camel.builder.RouteBuilder; public class MyCamelRoute extends RouteBuilder { @Override public void configure() throws Exception { from( "file:C:/Users/Nandini Gujral/Desktop/Start?noop=true" ) .to( "file:C:/Users/Nandini Gujral/Desktop/End" ); } } |
In this route, we use the `from` method to listen to the `file:input` directory. The `noop=true` option ensures that files are not deleted after processing. Files are then routed to the `file:output` directory.
Output:
Example 2: Content-Based Routing
This example demonstrates content-based routing, where messages are routed based on their content.
Java
import org.apache.camel.builder.RouteBuilder; public class ContentBasedRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { from( "direct:input" ) .choice() .when(body().contains( "important" )) .to( "direct:importantQueue" ) .when(body().contains( "low-priority" )) .to( "direct:lowPriorityQueue" ) .otherwise() .to( "direct:defaultQueue" ); } } |
In this route, messages received from the “direct:input” endpoint are routed based on their content. Messages containing “important” are sent to `direct:importantQueue`, those containing “low-priority” go to “direct:lowPriorityQueue“, and all other messages are directed to `direct:defaultQueue`.
Example 3: Error Handling
You can define error handling strategies for your routes using the “errorHandler” method.
Java
import org.apache.camel.builder.RouteBuilder; public class ErrorHandlingRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { errorHandler(deadLetterChannel( "file:error" )); from( "direct:input" ) .process( new MyProcessor()) .to( "direct:output" ); } } |
In this route, we specify a dead-letter channel using “deadLetterChannel(“file:error”)“. Messages that encounter errors during processing are routed to the “file:error” directory, ensuring that they are not lost and can be reviewed later.
Example 4: REST Endpoint
Camel makes it easy to create RESTful endpoints using the `rest` DSL.
Java
import org.apache.camel.builder.RouteBuilder; public class RestRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { restConfiguration().component( "servlet" ).port( 8080 ); rest( "/api" ).get( "/hello" ).to( "direct:hello" ); from( "direct:hello" ) .transform() .constant( "Hello, Camel!" ); } } |
In this route, we configure a REST endpoint to listen on port 8080 and define a `/api/hello` endpoint that responds with “Hello, Camel!” when accessed.
Example 5: Timer Trigger
Camel provides a timer component to trigger routes at specific intervals.
Java
import org.apache.camel.builder.RouteBuilder; public class TimerRouteBuilder extends RouteBuilder { @Override public void configure() throws Exception { from( "timer://myTimer?period=5000" ) .to( "log:TimerRoute?level=INFO&showAll=true" ); } } |
In this route, the “from” method with the “timer://myTimer?period=5000” endpoint triggers the route every 5 seconds. It logs a message to the console using the “log” endpoint.
Apache Camel – Routing with RouteBuilder
In the realm of integration and message routing, the Apache Camel framework is a shining star, known for its flexibility and robustness. Central to Apache Camel’s routing capabilities is the RouteBuilder class, a crucial tool that empowers developers to define complex routing rules with ease. In this article, we will explore the RouteBuilder class in Java, its significance in Apache Camel, and provide practical examples to illustrate how it can be employed to create powerful and flexible message routing configurations.