Mulesoft 简明教程

MuleSoft - Message Processor & Script Components

脚本模块让用户可以在 Mule 中使用脚本语言。简而言之,脚本模块可以交换用脚本语言编写的自定义逻辑。脚本可以用作实施或转换器。它们可用于表达式评估,即,用于控制消息路由。

The scripting modules facilitate users to use scripting language in Mule. In simple words, the scripting module can exchange custom logic written in scripting language. Scripts can be used as Implementations or transformers. They can be used for expression evaluation, i.e., for controlling message routing.

Mule 支持以下脚本语言:

Mule has the following supported scripting languages −

  1. Groovy

  2. Python

  3. JavaScript

  4. Ruby

How to Install Scripting Modules?

事实上,Anypoint Studio 随脚本模块一起提供。如果您在 Mule Palette 中找不到该模块,则可以使用 +Add Module 添加该模块。添加后,我们可以在 Mule 应用程序中使用脚本模块操作。

Actually, Anypoint Studio comes with the scripting modules. If you do not find the module in Mule Palette, then it can be added by using +Add Module. After adding, we can use the scripting module operations in our Mule application.

Implementing Example

正如所讨论的,我们需要将该模块拖放到画布中以创建工作区并在应用程序中使用它。以下是一个示例:

As discussed, we need to drag and drop the module into canvas for creating workspace and use it in our application. Following is an example of it −

install scripting modules

我们已经知道如何配置 HTTP Listener 组件;因此,我们将讨论配置脚本模块。我们需要按照以下编写步骤配置脚本模块:

We already know how to configure the HTTP Listener component; hence we are going to discuss about configuring the Scripting Modules. We need to follow the steps written below to configure scripting module −

Step 1

Step 1

从 Mule Palette 中搜索脚本模块,然后将脚本模块的 EXECUTE 操作拖动到您的流程中,如上所示。

Search for the Scripting module from Mule Palette and drag the EXECUTE operation of the scripting module into your flow as shown above.

Step 2

Step 2

现在,通过双击打开 Execute 配置选项卡。

Now, open the Execute configuration tab by double clicking on the same.

Step 3

Step 3

General 选项卡下,我们需要在 Code text window 中提供代码,如下所示: −

Under the General tab, we need to provide the code in the Code text window as shown below −

code text window

Step 4

Step 4

最后,我们需要从执行组件中选择 Engine 。以下是引擎列表: −

At last, we need to choose the Engine from the execute component. The list of engines is as below −

  1. Groovy

  2. Nashorn(javaScript)

  3. jython(Python)

  4. jRuby(Ruby)

配置 XML 编辑器中上述执行示例的 XML 如下: −

The XML of the above execution example in the Configuration XML editor is as follows −

<scripting:execute engine="jython" doc:name = "Script">
   <scripting:code>
      def factorial(n):
         if n == 0: return 1
      return n * factorial(n-1)
      result = factorial(10)
   </scripting:code>
</scripting:execute>

Message Sources

Mule 4 采用简化的 Mule 3 消息模型,使其在连接器中以一致的方式处理数据变得更加容易,而不会覆盖信息。在 Mule 4 消息模型中,每个 Mule 事件包含两部分内容: a message and variables associated with it

Mule 4 has a simplified model than Mule 3 message making it easier to work with data in a consistent way across connectors without overwriting information. In Mule 4 message model, each Mule event consists of two things: a message and variables associated with it.

Mule 消息包含有效负载及其属性,其中属性主要是元数据,例如文件大小。

A Mule message is having payload and its attributes, where attribute is mainly metadata such as file size.

变量保存任意用户信息,例如操作结果、辅助值等。

And a variable holds the arbitrary user information such as operation result, auxiliary values, etc.

Inbound

Mule 3 中的入站属性现在成为 Mule 4 中的属性。众所周知,入站属性存储通过消息源获得的有关有效负载的附加信息,但在 Mule 4 中,这是借助属性完成的。属性具有以下优点: −

The inbound properties in Mule 3 now becomes Attributes in Mule 4. As we know that inbound properties store additional information about the payload obtained through a message source, but this is now, in Mule 4, done with the help of attributes. Attributes have the following advantages −

  1. With the help of attributes, we can easily see which data is available, because attributes are strongly typed.

  2. We can easily access information contained in attributes.

以下是 Mule 4 中典型消息的示例: −

Following is the example of a typical message in Mule 4 −

inbound

Outbound

为了发送附加数据,必须由 Mule 连接器和传输中明确指定 Mule 3 中的出站属性。但在 Mule 4 中,可以使用 DataWeave 表达式为每个属性分别设置这些属性。它不会在主流程中产生任何副作用。

The outbound properties in Mule 3 must be explicitly specified by Mule connectors and transports in order to send additional data. But in Mule 4, each of those can be set separately, using a DataWeave expression for each one of them. It does not produce any side effect in the main flow.

例如,以下 DataWeave 表达式将执行 HTTP 请求并生成标头和查询参数,而无需设置消息属性。这在下面的代码中显示: −

For example, below DataWeave expression will perform a HTTP request and generates headers and query parameters without a need to set message properties. This is shown in the below code −

<http:request path = "M_issue" config-ref="http" method = "GET">
   <http:headers>#[{'path':'input/issues-list.json'}]</http:headers>
   <http:query-params>#[{'provider':'memory-provider'}]</http:query-params>
</http:request>

Message Processor

一旦 Mule 从消息源接收消息,消息处理器的任务就开始了。Mule 使用一个或多个消息处理器通过流处理消息。消息处理器的主要任务是在消息通过 Mule 流时对其进行转换、筛选、丰富和处理。

Once Mule receives a message from a message source, the work of message processor starts. The Mule uses one or more message processors to process the message through a flow. The main task of message processor is to transform, filter, enrich and process the message as it passes through the Mule flow.

Categorization of Mule Processor

以下是基于功能划分的 Mule 处理器类别: −

Following are the categories of Mule Processor, based on functions −

  1. Connectors − These message processors send and receive data. They also plug data into external data sources via standard protocols or third-party APIs.

  2. Components − These message processors are flexible in nature and perform business logic implemented in various languages like Java, JavaScript, Groovy, Python or Ruby.

  3. Filters − They filter the messages and allow only specific messages to continue to be processed in a flow, based on specific criteria.

  4. Routers − This message processor is used to control the flow of message to route, resequencing or split.

  5. Scopes − hey basically wrap snippets of code for the purpose of defining fine-grained behavior within a flow.

  6. Transformers − The role of transformers is to convert message payload type and data format to facilitate communication between systems.

  7. Business Events − They basically capture data associated with key performance indicators.

  8. Exception strategies − These message processors handle errors of any type that occur during message processing.