简介-Apache Camel DSL语法规范

针对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

参考资料

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,372评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,368评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,415评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,157评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,171评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,125评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,028评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,887评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,310评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,533评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,690评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,411评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,004评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,812评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,693评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,577评论 2 353

推荐阅读更多精彩内容