Ce contenu n'est pas disponible dans la langue sélectionnée.
8.4. Camel
8.4.1. Camel Services
Camel services allow you to leverage the core routing engine of Apache Camel to route between services in SwitchYard. Camel endpoints function as protocol adapters, exposing services hosted in SwitchYard to the outside world and allowing external services to be invoked from within SwitchYard. You can define the Camel routes using Java DSL or XML and deploy them within SwitchYard to handle pipeline orchestration between SwitchYard services.
8.4.2. Create a Camel Service
Prerequisites
- Name: the name of the Java class or XML file for your bean service.
- Service Name: the name of the service your bean provides.
- Interface: the contract for the service being provided. Camel supports Java and WSDL contract types.
Procedure 8.9. Create a Camel Service
- Create a new Camel Route file resource in the SwitchYard Editor JBoss Developer Studio plug-in.
- Decide whether to use DSL or XML dialect for the route. (Functionally, they are more or less equivalent, the choice is determined by how you want to express your routing logic.)
- If you want create a Java DSL route, select the "Camel (Java)" implementation type. For XML, use the "Camel (XML)" type.
- Input the values into the SwitchYard Editor's New Route File Screen.
- Click Finish.
8.4.3. Guidelines of Camel Route
These are some general guidelines to keep in mind when creating either type of route:
- There is only one route per service.
- The consumer or "from" endpoint in a route is always a "switchyard" endpoint and the endpoint name must equal the service name. This is default behavior in the tooling.
- To consume other services from within your route, only use "switchyard" consumer (in other words "to") endpoints. This keeps your routing logic independent of the binding details for consumed services.
8.4.4. Java DSL Route
You can define Camel routes using the Java Domain Specific Language (DSL). To implement the Java DSL, extend the
RouteBuilder
class. As there can be only one route per service, you can have only one RouteBuilder
class for each Camel routing service in your application. You can then add logic to your routing service in the configure()
method as shown below:
package com.example.switchyard.docs; import org.apache.camel.builder.RouteBuilder; public class CamelServiceRoute extends RouteBuilder { /** * The Camel route is configured through this method. The from: * endpoint is required to be a SwitchYard service. */ public void configure() { // TODO Auto-generated method stub from("switchyard://Example").log( "Received message for 'Example' : ${body}"); } }
8.4.5. XML Route
You can define Camel routes using XML. To define Camel routes in XML files, use the
<route>
tag with the namespace "http://camel.apache.org/schema/spring" as shown below:
<?xml version="1.0" encoding="ASCII"?> <route xmlns="http://camel.apache.org/schema/spring"> <from uri="switchyard://Example"/> <log message="Example - message received: ${body}"/> </route>
You can have only one file containing a route definition for each XML routing service in your application.
8.4.6. Consuming Services From Camel Routes
You can invoke another service from your Camel route by using the SwitchYard producer endpoint (
switchyard://
) within your route as shown below:
switchyard://[service-name]?operationName=[operation-name]
Here,
- service-name: Name of the SwitchYard service. This value should match the name of a service reference defined on the service component for the route.
- operation-name: Name of the service operation you want to invoke. This is only used on references and is optional if the target service only has a single operation.
The example below illustrates the default XML route modified to invoke a SwitchYard service:
<?xml version="1.0" encoding="ASCII"?> <route xmlns="http://camel.apache.org/schema/spring"> <from uri="switchyard://Example"/> <log message="Example - message received: ${body}"/> <!-- Invoke hasItem operation on WarehouseService --> <to uri="switchyard://WarehouseService?operationName=hasItem"/> </route>
8.4.7. Message Exchange Pattern
Exchanges are of central importance in Apache Camel, because the exchange is the standard form in which messages are propagated through routing rules. An exchange object consists of a message, augmented by metadata. Using an Exchange object makes it easy to generalize message processing to different message exchange patterns (MEP). See Apache Camel Development Guide for more information about exchange objects and message exchange patterns in Camel.
It is possible to temporarily implement an InOut (Request/Response) MEP when the default has been set as non Request/Response. For example, in the
quickstarts/switchyard/camel-jpa-binding
example from the quickstarts on EAP, you can change the storeGreeting()
method to be InOut by manipulating the call.
This is the original call. No return value is specified.
void storeGreeting(Greet event);
Specify a return value and the established MEP will be overriden and will become an InOut MEP.
int storeGreeting(Greet event);
8.4.8. Using Scripting Languages
Camel supports dynamic scripting languages inside the XML and Java DSL route logic. You can use the scripting languages in the following ways:
- You can use them to create a predicate in a message filter as shown below:
public class ScriptingBuilder extends RouteBuilder { public void configure() { from("switchyard://Inbound").filter().javaScript("request.getHeader('myHeader') != null").to("switchyard://Outbound"); } }
- You can use them to implement your service using
transform
element as shown below:public class ScriptingImplementationBuilder extends RouteBuilder { public void configure() { from("switchyard://Inbound").transform().groovy("classpath:script.groovy"); // classpath resource from("switchyard://InboundBsh").transform().language("beanshell", "file:script.bsh"); // file system resource } }
Additionally, you can generate responses by using predefined variables like request, response, or exchange inside your script.
8.4.9. Supported Scripting Languages
SwitchYard supports the following scripting languages inside the XML and Java DSL route logic:
- BeanShell
- JavaScript
- Groovy
- Ruby
- Python
8.4.10. Using CDI Beans in Camel Routes
The Camel component provides a convenient and powerful mechanism for routing between SwitchYard services using Java DSL or XML routing definitions. When creating these routes, you may need to add logic to the service pipeline that is specific to the Camel route. Instead of implementing your logic as a service, you can add a CDI bean to your application and call that as a bean from within your Camel route. SwitchYard integrates the CDI Bean Manager with the Camel Bean Registry to allow you to reference CDI Beans in your Camel routes. For example, you can use the following CDI bean inside your SwitchYard Camel Routes:
@Named("StringSupport") @ApplicationScoped public class StringUtil { public String trim(String string) { return string.trim(); } }The SwitchYard Camel Route logic implemented with this CDI bean looks like this:
public class ExampleBuilder extends RouteBuilder { public void configure() { from("switchyard://ExampleBuilder") .split(body(String.class).tokenize("\n")) .filter(body(String.class).startsWith("sally:")) .to("bean:StringSupport"); } }Any Java class annotated with
@Named
annotation in your application is available through Camel's Bean registry.
8.4.11. Injecting Implementation Properties in Camel Routes
SwitchYard integrates with the Properties Component in Camel to make system and application properties available inside your route definitions. You can inject properties into your camel route using
{{propertyName}}
expression, where propertyName
is the name of the property.
For example, the following camel route expects the
user.name
property to be injected in the last <Log>
statement:
<route xmlns="http://camel.apache.org/schema/spring" id="CamelTestRoute"> <log message="ItemId [${body}]"/> <to uri="switchyard://WarehouseService?operationName=hasItem"/> <log message="Title Name [${body}]"/> <log message="Properties [{{user.name}}]"/> </route>