此内容没有您所选择的语言版本。

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

  1. Create a new Camel Route file resource in the SwitchYard Editor JBoss Developer Studio plug-in.
  2. 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.)
  3. If you want create a Java DSL route, select the "Camel (Java)" implementation type. For XML, use the "Camel (XML)" type.
  4. Input the values into the SwitchYard Editor's New Route File Screen.
  5. 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. 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:
  1. 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");
    }
    }
  2. 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.8. Supported Scripting Languages

SwitchYard supports the following scripting languages inside the XML and Java DSL route logic:
  • BeanShell
  • JavaScript
  • Groovy
  • Ruby
  • Python

8.4.9. 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.10. 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>
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.