针对apache camel dsl的语法规范,我们可以按照学习一个新语言的方式去学习。我们不妨先回顾下学习C语言的经验,针对一个语言首先要有其变量、数据类型的定义、申明以及对变量的基本操作,接着要有语法控制逻辑,可以进行逻辑处理if-eles、for循环。为了扩充高级功能需要有函数和高级的数据结构,来实现大规模的应用开发。本章我们就带着这样的思路来学习Camel的DSL语法。
变量和数据结构
数据结构
camel中支持的数据结构包括以下数据结构对象
Data Format | Level | Description |
---|---|---|
Any23 | Stable | Extract RDF data from HTML documents. |
ASN.1 File | Stable | Encode and decode data structures using Abstract Syntax Notation One (ASN.1). |
Avro | Stable | Serialize and deserialize messages using Apache Avro binary data format. |
Avro Jackson | Stable | Marshal POJOs to Avro and back using Jackson. |
Barcode | Stable | Transform strings to various 1D/2D barcode bitmap formats and back. |
Base64 | Stable | Encode and decode data using Base64. |
Bindy | Stable | Marshal and unmarshal between POJOs and key-value pair (KVP) format using Camel Bindy |
CBOR | Stable | Unmarshal a CBOR payload to POJO and back. |
Crypto | Stable | Encrypt and decrypt messages using Java Cryptography Extension (JCE). |
CSV | Stable | Handle CSV (Comma Separated Values) payloads. |
FHIR JSon | Stable | Marshall and unmarshall FHIR objects to/from JSON. |
FHIR XML | Stable | Marshall and unmarshall FHIR objects to/from XML. |
Flatpack | Stable | Marshal and unmarshal Java lists and maps to/from flat files (such as CSV, delimited, or fixed length formats) using Flatpack library. |
Grok | Stable | Unmarshal unstructured data to objects using Logstash based Grok patterns. |
GZip Deflater | Stable | Compress and decompress messages using java.util.zip.GZIPStream. |
HL7 | Stable | Marshal and unmarshal HL7 (Health Care) model objects using the HL7 MLLP codec. |
iCal | Stable | Marshal and unmarshal iCal (.ics) documents to/from model objects. |
Jackson XML | Stable | Unmarshal an XML payloads to POJOs and back using XMLMapper extension of Jackson. |
JAXB | Stable | Unmarshal XML payloads to POJOs and back using JAXB2 XML marshalling standard. |
JSON Fastjson | Stable | Marshal POJOs to JSON and back using Fastjson |
JSON Gson | Stable | Marshal POJOs to JSON and back using Gson |
JSON Jackson | Stable | Marshal POJOs to JSON and back using Jackson |
JSON Johnzon | Stable | Marshal POJOs to JSON and back using Johnzon |
JSON JSON-B | Stable | Marshal POJOs to JSON and back using JSON-B. |
JSON XStream | Stable | Marshal POJOs to JSON and back using XStream |
JSonApi | Stable | Marshal and unmarshal JSON:API resources using JSONAPI-Converter library. |
LZF Deflate Compression | Stable | Compress and decompress streams using LZF deflate algorithm. |
MIME Multipart | Stable | Marshal Camel messages with attachments into MIME-Multipart messages and back. |
PGP | Stable | Encrypt and decrypt messages using Java Cryptographic Extension (JCE) and PGP. |
Protobuf | Stable | Serialize and deserialize Java objects using Google’s Protocol buffers. |
Protobuf Jackson | Stable | Marshal POJOs to Protobuf and back using Jackson. |
RSS | Stable | Transform from ROME SyndFeed Java Objects to XML and vice-versa. |
SOAP | Stable | Marshal Java objects to SOAP messages and back. |
Syslog | Stable | Marshall SyslogMessages to RFC3164 and RFC5424 messages and back. |
Tar File | Stable | Archive files into tarballs or extract files from tarballs. |
Thrift | Stable | Serialize and deserialize messages using Apache Thrift binary data format. |
uniVocity CSV | Stable | Marshal and unmarshal Java objects from and to CSV (Comma Separated Values) using UniVocity Parsers. |
uniVocity Fixed Length | Stable | Marshal and unmarshal Java objects from and to fixed length records using UniVocity Parsers. |
uniVocity TSV | Stable | Marshal and unmarshal Java objects from and to TSV (Tab-Separated Values) records using UniVocity Parsers. |
XML Security | Stable | Encrypt and decrypt XML payloads using Apache Santuario. |
XStream | Stable | Marshal and unmarshal POJOs to/from XML using XStream library. |
YAML SnakeYAML | Stable | Marshal and unmarshal Java objects to and from YAML using SnakeYAML |
Zip Deflater | Stable | Compress and decompress streams using java.util.zip.Deflater and java.util.zip.Inflater. |
Zip File | Stable | Compression and decompress streams using java.util.zip.ZipStream. |
变量定义
Apache Camel的消息类型中可以看到exchange消息中,包括exchanhe property; exchange Messag消息中,包括header、body。在变量定义中,需要符合Apache Camel的消息模型。因此在变量定义,实际我们可以定义的对象也就是exchange property、message header、message body。
exchange property
<setProperty name="from">
<simple>${body[entry][0][messaging][0][sender][id]}</simple>
</setProperty>
body
<setBody>
<simple>{"MsgType": "text", "ChannelType": "webchat","TenantId": "default","SilentGroup": "default_silent",
"SkillGroup": "ICBC","Content": "${exchangeProperty[msg]}","BizType": "facebook","ClientLevel": 1, "FromUserName": "${exchangeProperty[recipient]}",
"UserName": "${exchangeProperty[from]}", "CreateTime": "${exchangeProperty[time]}", "ChatUrl": "https://devcc.myegoo.com.cn/fidp/api/fl/callback"}
</simple>
</setBody>
header
<removeHeaders customId="true" id="removeHeaders32" pattern="*"/>
<setHeader name="CamelHttpMethod">
<constant>POST</constant>
</setHeader>
<setHeader name="Content-Type">
<constant>application/json;charset=UTF-8</constant>
</setHeader>
环境变量使用
{{}}读取环境变量
表达式
Expression Langauge
要声明一个变量或者对变量做基本的运算,就需要有表达式。比如java语言里的 String name="XXX", 或者 int a = b +c等,丢失表达式。常见的表达式包括逻辑运算、数据运算、变量声明等。在Camel DSl中一样,Camel支持一下多种表达式语言
Language | Description |
---|---|
Bean Method | Calls a Java bean method. |
Constant | A fixed value set only once during the route startup. |
CSimple | Evaluate a compiled simple expression. |
DataSonnet | To use DataSonnet scripts for message transformations. |
ExchangeProperty | Gets a property from the Exchange. |
File | File related capabilities for the Simple language |
Groovy | Evaluates a Groovy script. |
Header | Gets a header from the Exchange. |
HL7 Terser | Get the value of a HL7 message field specified by terse location specification syntax. |
jOOR | Evaluates a jOOR (Java compiled once at runtime) expression. |
JQ | Evaluates a JQ expression against a JSON message body. |
JSONPath | Evaluates a JSONPath expression against a JSON message body. |
MVEL | Evaluates a MVEL template. |
OGNL | Evaluates an OGNL expression (Apache Commons OGNL). |
Ref | Uses an existing expression from the registry. |
Simple | Evaluates a Camel simple expression. |
SpEL | Evaluates a Spring expression (SpEL). |
Tokenize | Tokenize text payloads using delimiter patterns. |
XML Tokenize | Tokenize XML payloads. |
XPath | Evaluates an XPath expression against an XML payload. |
XQuery | Evaluates an XQuery expressions against an XML payload. |
If you are outside the DSL and want to create your own expressions you can either implement the Expression interface, reuse one of the other builders or try the ExpressionBuilder class.
public interface Expression {
/**
* Returns the value of the expression on the given exchange
*
* @param exchange the message exchange on which to evaluate the expression
* @param type the expected type of the evaluation result
* @return the value of the expression
*/
<T> T evaluate(Exchange exchange, Class<T> type);
}
常见表达式介绍
- 常量表达式 <constant> </constant>
流程控制
逻辑处理
- processor
- components
代码模块
基础模块direct
routes组织和管理
context scan