Camel Context routes are configuration items that are applied to the context. In reality, this involves defining a route in code first, then attaching it to a Camel Context.
Camel, although being a Java framework, may be customized using either Java or XML syntax. The syntax you employ is referred to as a DSL (Domain Specific Language) in Camel jargon.
• Java DSL – for defining routes, this employs Java syntax. Your routes are written in a RouteBuilder class, and you use a “fluent” builder to link many route stages together.
• XML DSL (also known as Spring DSL or Blueprint DSL) – this is an XML file where you specify your routes. A route is an XML element that includes information about a path.
Although each DSL has its own syntax, the general structure of a route is mostly the same, regardless of whether it is defined in the XML DSL or the Java DSL.
A basic Camel route
Each route begins with a from Endpoint, which is often supplied as a URI and specifies where the data should come from.
A route can have numerous phases, such as data transformation or logging. A route, on the other hand, normally concludes with a to command, which specifies where the data will be sent next.
In the Java DSL, a truly simple route may look like this:
from("file:home/customers/new")
.to("file:home/customers/old");
The File component (indicated by the file: prefix) is used in this example to move all incoming files from the home/customers/new folder to the home/customers/old folder.
The following is how the same route could be expressed in the XML DSL:
<route>
<from uri="file:home/customers/new"/>
<to uri="file:home/customers/old"/>
</route>
What if we wish to add a new step to our itinerary? When we receive a file, we want to record a message. Now, all we have to do is add a new step in between the old ones, as follows:
from("file:home/customers/new")
.log("Received a new customer!")
.to("file:home/customers/old");
A message is now moved from the new folder to the old folder in the code above. We use the log step in the middle to write a log message to the console, and it would appear something like this in the XML DSL:
<route>
<from uri="file:home/customers/new"/>
<log message="Received a new customer!"/>
<to uri="file:home/customers/old"/>
</route>
Of course, routes can be more complicated than this, but what we’ve defined here is a completely functional route. Camel can achieve things that would ordinarily require a lot more Java code in just three lines.
What happens when a route comes to an end?
Camel will do one of two things at the end of a route, depending on a message property known as the Message Exchange Pattern, or MEP.
Return the final output message back to the consumer
The Message Exchange Pattern is set to InOut if the route was invoked in a synchronous manner. When the original caller anticipates a response, this happens. In this situation, Camel will finish the route by returning the output message from the final endpoint to the caller.
Camel’s Jetty component, which configures an embedded Jetty web server, is a nice example of this. The Jetty component will listen for HTTP requests when it is used as the consumer at the start of a route. The Jetty endpoint receives an HTTP request from a client and sets the Message Exchange Pattern to InOut.
A Jetty web server listens on localhost port 8008 in this example. Even though there is no explicit code to do so when a request is received, the response Hello! is delivered to the consumer:
from("jetty:http://localhost:8008/hello")
.transform(constant("Hi!"));
Do nothing
The Exchange pattern is set to InOnly if the component at the start of the route is a consumer of a one-way message — in other words if the consumer receives a message from a caller who does not expect a response. Camel will just process the final step of the route and finish in this scenario.
The File component is an example of this. A route that starts with a normal File component will read a message from the disc and not try to respond. Because there is no one to respond to, it is classified as an InOnly pattern.
from("file:documents/invoices")
.to("file:documents/done");
Conclusion
An Apache route Camel is a set of steps that Camel does in order to consume and process a message. A Camel route begins with a consumer and continues with a series of destinations and processors. For more information visit: https://camel.apache.org/manual/routes.html