Simple use example of MULE message enhancement Message Enricher

tool Version
mule-standalone 3.9.0
Anypoint-Studio 6.4.0

写在前面
This example uses Message Enricherelements to "enhance" (change) the content of the message. In the use of Enricher, what is more ambiguous about the container is what the element configuration refers to, sourceand targetthe two contents are generally set using MEL expressions. Understand that the error expression is difficult to write.
Message Enricher configuration initial interface
These are inseparable from Message Enricherthe correct understanding. The following figure explains the process of Enricher, and the source of the picture comes from the source of the picture source .

In the figure, when Basic Messagepassing through the Enricher, it interacts with the Resource, which makes it become after processing by the Enricher Enriched Message, and it is obvious that the extra orange part is the enhanced content. And this enhanced thing is generally attached to the Resource.

The most important feature of Message Enricher is that it does not affect the content of the original payload, and remember that Message Enricher is an element of type SCOPE and cannot be used alone

In the example, it is mainly divided into Main Flowsum MessageEnricherFlow, the latter is called by the former, and plays Resourcea role. A original payloadsum is set in the Main Flow, and target variableboth have initial values. The logger is used to record the content before the enhancement of the two, and then passes Message Enricherthrough by Flow Referencecalling the subflow. MessageEnricherFlow, the enhanced content is logged again using the logger, and uses set payloadthe value of the payload and target variable in the output stream on the page. The sub-flow MessageEnricherFlow mainly queries a record from the database and converts it to json format. The enhanced content is to take out the record and attach it to the target target variable.

Flow结构图

xml文档

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:json="http://www.mulesoft.org/schema/mule/json"
    xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <db:oracle-config name="MY_LOCAL_DB" host="localhost" port="1521" instance="orcl" user="hr" password="hr" doc:name="Oracle Configuration"/>
    <flow name="MainFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-payload value="#['&#25105;&#26159;&#21407;&#22987;payload&#20869;&#23481;']" doc:name="original payload"/>
        <set-variable variableName="targetVar" value="#['&#25105;&#26159;&#30446;&#26631;&#21464;&#37327;']" doc:name="target variable"/>
        <logger message="#[flowVars.targetVar]" level="INFO" doc:name="Logger"/>
        <enricher source="#[json:[0]/FIRST_NAME]" target="#[flowVars.targetVar]" doc:name="Message Enricher">
            <flow-ref name="MessageEnricherFlow" doc:name="Flow Reference"/>
        </enricher>
        <logger message="#[flowVars.targetVar]" level="INFO" doc:name="Logger"/>
        <set-payload value="#[payload + '\n' +flowVars.targetVar]" doc:name="Set Payload"/>
    </flow>
    <sub-flow name="MessageEnricherFlow">
        <db:select config-ref="MY_LOCAL_DB" doc:name="Database">
            <db:parameterized-query><![CDATA[SELECT * FROM employees WHERE employee_id=101]]></db:parameterized-query>
        </db:select>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </sub-flow>
</mule>

数据库查询的记录

运行效果http://localhost:8081/

原始payload和target variable未增强前的内容

Comparing the two, it can be seen that Message Enricherafter processing, original payloadthere is no change, but target variablea sentence becomes the FIRST_NAME value of the database record.

Message Enricher配置图

It can be seen that the expression of Source is #[json:[0]/FIRST_NAME]used to obtain the FIRST_NAME of the first record in the json data of the database, and the expression of Target is the flow variable targetVar. The key is here Source从子流MessageEnricherFlow中将json内容获取FRIST_NAME增强到流变量targetVar, the role of the sub-flow Resource(即配置中source).

Personal understanding: message enhancement is often used when you do not want to change the content of the payload, but want to change a part of the message MuleMessage. (please comment if wrong)

表达式写不对往往出现错误

Execution of the expression "{xxxx}=__object_for_enrichment" failed. 
(org.mule.api.expression.ExpressionRuntimeException).

参考资料:
Official document
The Best Answer of the foreign god anirban37 has a more detailed
official explanation of the scope type. The
example explains Enricher's article. The example of this article uses the elements of the enterprise version.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325997303&siteId=291194637