1.2. Basic Java DSL Syntax
来源:互联网 发布:学java跟不上 编辑:程序博客网 时间:2024/05/20 06:38
What is a DSL?
A Domain Specific Language (DSL) is a mini-language designed for a special purpose. A DSL does not have to be logically complete but needs enough expressive power to describe problems adequately in the chosen domain. Typically, a DSL does not require a dedicated parser, interpreter, or compiler. A DSL can piggyback on top of an existing object-oriented host language, provided DSL constructs map cleanly to constructs in the host language API.
Consider the following sequence of commands in a hypothetical DSL:
command01;command02;command03;
You can map these commands to Java method invocations, as follows:
command01().command02().command03()
You can even map blocks to Java method invocations. For example:
command01().startBlock().command02().command03().endBlock()
The DSL syntax is implicitly defined by the data types of the host language API. For example, the return type of a Java method determines which methods you can legally invoke next (equivalent to the next command in the DSL).
Router rule syntax
Apache Camel defines a router DSL for defining routing rules. You can use this DSL to define rules in the body of a RouteBuilder.configure() implementation. Figure 1.1, “Local Routing Rules” shows an overview of the basic syntax for defining local routing rules.
Figure 1.1. Local Routing Rules
A local rule always starts with a from(“EndpointURL”) method, which specifies the source of messages (consumer endpoint) for the routing rule. You can then add an arbitrarily long chain of processors to the rule (for example, filter()). You typically finish off the rule with a to(“EndpointURL”) method, which specifies the target (producer endpoint) for the messages that pass through the rule. However, it is not always necessary to end a rule with to(). There are alternative ways of specifying the message target in a rule.
Note You can also define a global routing rule, by starting the rule
with a special processor type (such as intercept(), exception(), or
errorHandler()). Global rules are outside the scope of this guide.
Consumers and producers
A local rule always starts by defining a consumer endpoint, using from(“EndpointURL”), and typically (but not always) ends by defining a producer endpoint, using to(“EndpointURL”). The endpoint URLs, EndpointURL, can use any of the components configured at deploy time. For example, you could use a file endpoint, file:MyMessageDirectory, an Apache CXF endpoint, cxf:MyServiceName, or an Apache ActiveMQ endpoint, activemq:queue:MyQName. For a complete list of component types, see .
Exchanges
An exchange object consists of a message, augmented by metadata. Exchanges are of central importance in Apache Camel, because the exchange is the standard form in which messages are propagated through routing rules. The main constituents of an exchange are, as follows:
In message—is the current message encapsulated by the exchange. As the exchange progresses through a route, this message may be modified. So the In message at the start of a route is typically not the same as the In message at the end of the route. The org.apache.camel.Message type provides a generic model of a message, with the following parts: Body. Headers. Attachments. Out message—is a temporary holding area for a reply message or for a transformed message. Certain processing nodes (in particular, the to() command) can modify the current message by treating the In message as a request, sending it to a producer endpoint, and then receiving a reply from that endpoint. The reply message is then inserted into the Out message slot in the exchange.Message exchange pattern (MEP)—affects the interaction between the exchange and endpoints in the route, as follows: Producer endpoints—the MEP affects the producer endpoints that the exchange encounters along the route (for example, when an exchange passes through a to() node). For example, if the current MEP is InOnly, a to() node would not expect to receive a reply from the endpoint. Sometimes you need to change the current MEP in order to customize the exchange's interaction with a producer endpoint. For more details, see Section 4, “Endpoints”. Exchange properties—a list of named properties containing metadata for the current message.
Message exchange patterns
Using an Exchange object makes it easy to generalize message processing to different message exchange patterns. For example, an asynchronous protocol might define an MEP that consists of a single message that flows from the consumer endpoint to the producer endpoint (an InOnly MEP). An RPC protocol, on the other hand, might define an MEP that consists of a request message and a reply message (an InOut MEP). Currently, Apache Camel supports the following MEPs:
InOnlyRobustInOnlyInOutInOptionalOutOutOnlyRobustOutOnlyOutInOutOptionalIn
Where these message exchange patterns are represented by constants in the enumeration type, org.apache.camel.ExchangePattern.
Grouped exchanges
Sometimes it is useful to have a single exchange that encapsulates multiple exchange instances. For this purpose, you can use a grouped exchange. A grouped exchange is essentially an exchange instance that contains a java.util.List of Exchange objects stored in the Exchange.GROUPED_EXCHANGE exchange property. For an example of how to use grouped exchanges, see Section 5, “Aggregator”.
Processors
A processor is a node in a route that can access and modify the stream of exchanges passing through the route. Processors can take expression or predicate arguments, that modify their behavior. For example, the rule shown in Figure 1.1, “Local Routing Rules” includes a filter() processor that takes an xpath() predicate as its argument.
Expressions and predicates
Expressions (evaluating to strings or other data types) and predicates (evaluating to true or false) occur frequently as arguments to the built-in processor types. For example, the following filter rule propagates In messages, only if the foo header is equal to the value bar:
from("seda:a").filter(header("foo").isEqualTo("bar")).to("seda:b");
Where the filter is qualified by the predicate, header(“foo”).isEqualTo(“bar”). To construct more sophisticated predicates and expressions, based on the message content, you can use one of the expression and predicate languages (see Expression and Predicate Languages).
- 1.2. Basic Java DSL Syntax
- Java Basics Part 3/20 - Basic Syntax
- Java基本句法(basic syntax)
- Basic Syntax基本语法
- SQL Basic Syntax Principles
- Basic Swift Syntax
- Regular Expression Basic Syntax Reference
- Kotlin Reference (一) Basic Syntax
- Kotlin Reference (一) Basic Syntax
- java syntax
- 把Java看作DSL
- php study note 2 --- basic syntax
- Regex - Regular Expression Basic Syntax Reference
- PowerShell Step By Step (1): Basic Syntax
- CSS Fundamentals/Basic CSS Structure & Syntax
- basic Python 1(syntax&strings&dastetime)
- DSL
- DSL,
- Python实现简单网络爬虫--转自极客头条
- Mac下安装Memcache
- Python如何搜索模块
- Mvp模式在Android开发中的应用
- PhpStorm上解决move_uploaded_file函数open失败的问题 图文教程
- 1.2. Basic Java DSL Syntax
- 每天一个 Linux 命令(15):tail 命令
- Qt引用新类,出现LNK2019: 无法解析的外部符号
- 【Unity&C#】如何对一组Bool型变量进行判断
- 一般与特殊结构关系 整体与部分结构关系
- dom02批量禁用文本框和批量拼接字符串
- 网络层协议(4):动态选路
- 1.3. Router Schema in a Spring XML File
- AdaBoost算法理解基于机器学习实战