Saturday, 22 March 2025

Enrich clone parameter

What Does the clone parameter in the Enrich mediator mean?

In the context of the WSO2 Micro Integrator (MI) <enrich> mediator:

<enrich>
    <source clone="false" type="body"/>
    <target property="ORIGINAL-PAYLOAD" type="property"/>
</enrich>

clone="false" → Ensures the payload is referenced directly instead of creating a deep copy (duplication) of the message.

Why Does This Matter?

In WSO2 MI, the message context (including the payload) is stored in memory as an OM (Object Model) structure, which is part of the AXIOM (AXis Object Model) framework.

  • clone="false" → References the existing payload without copying data in memory.
  • clone="true" → Creates a new copy of the payload (uses additional memory and CPU cycles).

Example to Illustrate the Difference

Payload Before Enrich Mediator

Incoming XML payload:
<order>
    <id>12345</id>
    <amount>100</amount>
</order>

Enrich Mediator with clone="false" (Recommended for Performance)

<enrich>
    <source clone="false" type="body"/>
    <target property="ORIGINAL-PAYLOAD" type="property"/>
</enrich>

Result:
  • No new copy is created.
  • The ORIGINAL-PAYLOAD property points to the same memory reference as the original payload.
  • If the payload is modified later, the ORIGINAL-PAYLOAD will reflect those changes.
Fastest method when you only need to reference the payload.

Enrich Mediator with clone="true" (Data Duplication)

<enrich>
    <source clone="true" type="body"/>
    <target property="ORIGINAL-PAYLOAD" type="property"/>
</enrich>

Result:
  • A new copy of the payload is created in memory.
  • Changes made to the original payload later will not affect the copy stored in ORIGINAL-PAYLOAD.
Safer if you need to preserve the original state of the payload before modifying it.
Slower because it consumes more memory and CPU.

When to Use Each Option?

Use Case Recommended Option
Log the payload without modifying it clone="false"
Backup the payload before modifying it clone="true"
Capture large payloads in performance-critical flows clone="false"
Ensure payload integrity for error handling clone="true"

Example Flow Using Both Options

Scenario: Capture the original payload for logging and modify the message later.

<sequence xmlns="http://ws.apache.org/ns/synapse" name="ExampleSequence">
    <!-- Capture Original Payload for Logging (Best Performance) -->
    <enrich>
        <source clone="false" type="body"/>
        <target property="ORIGINAL-PAYLOAD" type="property"/>
    </enrich>

    <!-- Log Original Payload -->
    <log level="custom">
        <property name="Original Payload" expression="get-property('ORIGINAL-PAYLOAD')"/>
    </log>

    <!-- Modify the Payload -->
    <payloadFactory media-type="xml">
        <format>
            <response>
                <status>Success</status>
                <originalAmount>$1</originalAmount>
            </response>
        </format>
        <args>
            <arg evaluator="xml" expression="//order/amount"/>
        </args>
    </payloadFactory>

    <!-- Log Modified Payload -->
    <log level="custom">
        <property name="Modified Payload" expression="get-property('body')"/>
    </log>
</sequence>

Expected Logs:

[Original Payload] 
<order>
    <id>12345</id>
    <amount>100</amount>
</order>

[Modified Payload] 
<response>
    <status>Success</status>
    <originalAmount>100</originalAmount>
</response>

After <payloadFactory> Modification:
Since clone="false" was used earlier, ORIGINAL-PAYLOAD is updated as well.
Value of ORIGINAL-PAYLOAD (and the modified payload):
<response>
    <status>Success</status>
    <originalAmount>100</originalAmount>
</response>

Key Takeaways

  • clone="false" — Best for performance; avoids unnecessary memory duplication but reflects changes made to the original payload.
  • clone="true" — Creates a deep copy of the payload, ideal for protecting the original data from modifications.

For performance-critical scenarios or large payloads, clone="false" is generally preferred.

No comments:

Post a Comment