Wire Tap in Mule: copying the order to audit without slowing down or failing the flow
- 9 min read
On this page
Code
Every incoming order has to be copied to an audit system. When that call sits in the middle of the flow, a slow audit system makes the order slow, and an audit outage gets the order refused. The Wire Tap sends a copy of the message to a second destination and lets the original go on. In Mule 4, the example builds this with an Async scope and its own Try, at the price of a copy that can be lost.
The problem
Order ORD-7001 arrives over HTTP. The first version (POST /eip/wire-tap/inline/orders) writes the audit copy with an HTTP Request and accepts the order only after the response:
<try doc:name="Try Write Audit Copy Inline">
<http:request doc:name="POST Audit Copy Inline" config-ref="eip-self-http-request-config" method="POST" path="/eip/wire-tap/audit/copies" responseTimeout="${eip.wireTap.auditTimeoutMillis}" target="vAuditReply">
<http:body><![CDATA[#[payload]]]></http:body>
</http:request>
<error-handler>
<on-error-continue doc:name="On Error Continue (audit system timed out)" type="HTTP:TIMEOUT, HTTP:GATEWAY_TIMEOUT" logException="false">
<set-variable doc:name="SET vAuditFailure" variableName="vAuditFailure" value="TIMEOUT" />
</on-error-continue>
<on-error-continue doc:name="On Error Continue (audit system unavailable)" type="HTTP:INTERNAL_SERVER_ERROR, HTTP:BAD_GATEWAY, HTTP:SERVICE_UNAVAILABLE, HTTP:CONNECTIVITY" logException="false">
<set-variable doc:name="SET vAuditFailure" variableName="vAuditFailure" value="UNAVAILABLE" />
</on-error-continue>
</error-handler>
</try>
<choice doc:name="Check Audit Outcome">
<when expression="#[vars.vAuditFailure == 'TIMEOUT']">
<raise-error doc:name="Raise GATEWAY_TIMEOUT Error - Audit timed out" type="APP:GATEWAY_TIMEOUT" description="Audit system timed out, order not accepted" />
</when>
<when expression="#[vars.vAuditFailure == 'UNAVAILABLE']">
<raise-error doc:name="Raise SERVICE_UNAVAILABLE Error - Audit unavailable" type="APP:SERVICE_UNAVAILABLE" description="Audit system unavailable, order not accepted" />
</when>
</choice>
<flow-ref doc:name="Refer to sf-accept-tapped-order" name="sf-accept-tapped-order" />
In the repository: src/main/mule/eip/wire-tap/wire-tap.xml
With audit answering 500, 502 or 503, or with no connection, the order gets 503. On a timeout (eip.wireTap.auditTimeoutMillis, 5000 in app.yaml) or a 504 from audit, it gets 504. With audit merely slow, the response takes just as long.
The timeout has one more effect: the caller does not know what happened on the other side. In the example, the simulated audit system finishes the work later and records the copy of an order that was not accepted (wt-inline-timed-out-audit-still-records-the-copy-of-an-order-not-accepted).
The pattern in the book
The book starts from the need to test, monitor or troubleshoot a channel and asks how to inspect the messages that travel on a point-to-point channel. The solution is to insert a simple Recipient List into the channel, which publishes each incoming message to the main channel and to a secondary channel (Wire Tap).
What sets it apart from an ordinary Recipient List is that the two outputs are fixed and the message does not change. The cost of installing it is a new channel between the tap and the destination; the analysis logic lives in another component, so the original channel keeps behaving as before.
The diagram answers where each message goes. It redraws the book’s sketch (CC BY 4.0) in the order scenario: the orders API is the source, payment is the destination, and audit reads the secondary channel.
In Mule
The example uses the Async scope. The reference says the main flow continues while the scope is processed, the result of what runs inside it is not available to the main flow, and the scope does not inherit the flow’s error handling: to handle errors in there, you use a Try (Async Scope).
The “copy” has a limit. Async receives a copy of the Mule message, but the payload objects are not copied: both messages reference the same ones. If the payload is a mutable object, such as a Java bean, and a processor inside Async changes a field, the main flow sees the changed value (Async Scope). Replacing the payload in the branch is a different matter: in the test wt-tap-branch-does-not-change-the-main-payload-or-variables, the mocked call swaps the branch’s payload and variables, and the main flow’s response keeps the original order and its own variables.
The diagram answers what the client waits for and what stays outside that wait in the example’s flow:
In the MuleSoft documentation
The page Enterprise Integration Patterns Using Mule has no row for the Wire Tap. Async with a Try is this page’s own assembly, based on the scope reference. The same Async makes the copy that a Message Store needs.
The example
The second version (POST /eip/wire-tap/orders) takes the call out of the order’s path:
<async doc:name="Async Wire Tap">
<try doc:name="Try Send Audit Copy">
<http:request doc:name="POST Audit Copy From Tap" config-ref="eip-self-http-request-config" method="POST" path="/eip/wire-tap/audit/copies" responseTimeout="${eip.wireTap.auditTimeoutMillis}">
<http:body><![CDATA[#[payload]]]></http:body>
</http:request>
<logger doc:name="INFO: Log audit copy sent" level="INFO" message="#['Audit copy of orderId=' ++ vars.vOrderId ++ ' sent, correlationId=' ++ correlationId]" />
<error-handler>
<on-error-continue doc:name="On Error Continue (audit copy not confirmed)" type="ANY" logException="false">
<logger doc:name="WARN: Log audit copy not confirmed" level="WARN" message="#['Audit copy of orderId=' ++ vars.vOrderId ++ ' not confirmed: ' ++ error.errorType.namespace ++ ':' ++ error.errorType.identifier ++ ', correlationId=' ++ correlationId]" />
</on-error-continue>
</error-handler>
</try>
</async>
<flow-ref doc:name="Refer to sf-accept-tapped-order" name="sf-accept-tapped-order" />
In the repository: src/main/mule/eip/wire-tap/wire-tap.xml
Three decisions make the tap:
- Async comes before the acceptance. The branch receives the payload as the order arrived, including fields the flow does not use (
wt-tap-copy-arrives-unmodified-when-audit-is-up). - The Try goes inside Async, with
on-error-continuefor any error. It logs a WARN with theorderIdand the error type. There is no retry. - The HTTP Request has a
responseTimeout. It limits how long a copy can stay hanging.
Caveats
- The tap loses copies. The copy lives in memory while the branch runs: if audit refuses it, or if the application stops before the branch finishes, there is nowhere to resend it from. The log says “not confirmed”, not “lost”, because on a timeout the result is unknown; a copy refused with a 503, on the other hand, never arrives. In the example, audit records the timed-out copy later (
wt-tap-timed-out-copy-is-not-confirmed-and-still-arrives), so repeating the call could record it twice. maxConcurrencyis not set in the example. The reference says that, at the maximum number of concurrent messages, the scope cannot receive new requests, and points to back-pressure management (Async Scope), described as actions that depend on each flow’s source (Execution Engine). Before setting a limit, test its effect with audit slow (this page’s recommendation).- The same order can show up more than once. The simulated audit system records every copy it receives, and the example does not deduplicate: a client that resends the order creates another copy.
- The copy carries the whole order. If it holds data audit should not see, a Content Filter inside the branch builds another payload without changing the shared object (this page’s recommendation).
When not to use it
Use the Wire Tap for disposable copies: monitoring, sampling, debugging. If audit is a condition for accepting the order, because of a business or legal rule, keep the call before the acceptance or use Guaranteed Delivery with an Idempotent Receiver. If the caller needs the result of the second call, do not use Async. For several destinations chosen by content, use a Recipient List; for all the subscribers of a broker, Publish-Subscribe.
Running the example and the tests
git clone https://github.com/brunosouzas/mulesoft-patterns-examples.git
cd mulesoft-patterns-examples
mvn clean test
MUnit runs on the Enterprise runtime, so Maven needs access to the MuleSoft Enterprise repository (credentials in ~/.m2/settings.xml). The wire-tap-test-suite.xml suite records the behavior observed on Mule 4.9.17 with MUnit 3.7.1:
| Test | What it proves |
|---|---|
wt-inline-order-fails-when-audit-is-down |
Audit down: the in-flow version answers 503 and does not accept the order |
wt-inline-order-waits-for-a-slow-audit |
With a 1500 ms delay, the response takes at least that long |
wt-inline-audit-timeout-is-504 |
A timeout (HTTP:TIMEOUT simulated with a mock) gives 504, with no acceptance |
wt-inline-audit-connection-failure-is-503 |
A connection failure (HTTP:CONNECTIVITY simulated with a mock) gives 503 |
wt-inline-audit-500-is-503 |
A 500 error simulated with a mock becomes 503 |
wt-inline-audit-502-is-503 |
A 502 error simulated with a mock becomes 503 |
wt-inline-audit-504-is-504 |
A 504 error simulated with a mock stays 504 |
wt-inline-timed-out-audit-still-records-the-copy-of-an-order-not-accepted |
The copy of an order refused on timeout is recorded later |
wt-tap-accepts-the-order-when-audit-is-down-and-logs-the-unconfirmed-copy |
With the tap, 201 and a WARN; the refused copy does not come back |
wt-tap-copy-arrives-unmodified-when-audit-is-up |
Audit receives the order as sent, with the request’s correlationId |
wt-tap-answer-does-not-wait-for-a-slow-audit |
With a 3000 ms delay, the response goes out before the copy arrives |
wt-tap-branch-does-not-change-the-main-payload-or-variables |
What the branch swaps in the payload and the variables, with the call mocked, does not reach the main flow |
wt-tap-timed-out-copy-is-not-confirmed-and-still-arrives |
HTTP:TIMEOUT in the WARN, and the copy recorded later |
wt-order-without-id-is-refused-and-not-copied |
Without orderId, 400 in both versions and no copy |
wt-simulated-audit-system-answers-its-settings-and-receipts |
The simulated audit system returns its settings, answers 201 with an auditId and lists the copy |
wt-invalid-audit-settings-are-refused |
The simulated audit system refuses invalid settings |
The slow-audit test checks that the response came first:
<munit:validation>
<munit-tools:assert-that doc:name="201" expression="#[vars.vPosted.status]" is="#[MunitTools::equalTo(201)]" />
<munit-tools:assert-that doc:name="Answered before the copy" expression="#[sizeOf(vars.vAtOnce)]" is="#[MunitTools::equalTo(0)]" />
<munit-tools:assert-that doc:name="Copy arrived later" expression="#[vars.vCopies[0].order.orderId]" is="#[MunitTools::equalTo('ORD-WT-7')]" />
</munit:validation>
In the repository: src/test/munit/eip/wire-tap-test-suite.xml
To call the endpoints, run the application in Anypoint Studio (port 8081) with the Wire Tap entries in requests.http. The first ones switch off the simulated audit system and compare the two versions:
### EIP wire tap — switch the simulated audit system off, then compare the inline write (503) with the tap (201)
PUT {{base}}/eip/wire-tap/audit/availability
Content-Type: application/json
{ "available": false }
###
POST {{base}}/eip/wire-tap/inline/orders
Content-Type: application/json
{ "orderId": "ORD-7001", "amount": 120 }
###
POST {{base}}/eip/wire-tap/orders
Content-Type: application/json
{ "orderId": "ORD-7002", "amount": 120 }
In the repository: requests.http
The in-flow version answers 503, and the tap version, 201. The next entries repeat the comparison with a 3 s delay and list the copies received. The other patterns are in the catalog of integration patterns in Mule and, by component name, in the index by component.