Message Enricher With Mulesoft
Message enricher works by sending a copy of the original message into the processor and then eventually moving the enriched message forward.
Join the DZone community and get the full member experience.
Join For FreeMessage enricher is one of the important components with Mulesoft. There are various scenarios in which you can use the message enricher component.
When synchronizing data between data sources, you often check to see if a record already exists in the target resource.
If you simply add an endpoint to query the target resource first before adding it, the response will become the payload. This is not what you want!
You want the external call to act as an enrichment of the existing message with the original payload retained.
How Does Message Enricher Work?
Here's the process:
Enricher sends a copy of the original message into the processor.
The original message waits.
The copy is processed.
The copy's response is a message.
Part(s) of the response are added to part(s) of the original message.
The enriched message moves forward.
We will walk through adding only new records in target system (i.e., Salesforce).
When determining if records exist in the target system, it is common to store results in record variable.
After that determining the record exists in the target system, you can use batch step filter to process only qualified records.
Create a batch job to poll the database after a specific interval of time with a specific condition (i.e., postal code).
Add a batch step and use message enricher to check if the record exists in Salesforce (account with the same name) and store the result in record variable and retain the original payload.
Add a second batch step with a filter that allows only new records to be added in Salesforce.
Use commit scope to process record in a batch.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:batch="http://www.mulesoft.org/schema/mule/batch"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata"
xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
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.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/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/sfdc http://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd">
<batch:job name="pollDatabaseBatch">
<batch:input>
<poll doc:name="Poll">
<fixed-frequency-scheduler frequency="5" timeUnit="SECONDS"/>
<watermark variable="lastAccountID" default-expression="0" selector="MIN" selector-expression="#[payload.accountID]"/>
<db:select config-ref="MySQL_Configuration" doc:name="Database">
<db:parameterized-query>
<![CDATA[select * from accounts where postal='80305' and accountID > #[flowVars.lastAccountID]]]>
</db:parameterized-query>
</db:select>
</poll>
<logger message="My Payload : #[payload]" level="INFO" doc:name="Logger"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload>
<![CDATA[%dw 1.0
%output application/java
---
payload map ((payload01 , indexOfPayload01) -> {
Id: payload01.accountID as :string,
Name: payload01.name,
BillingStreet: payload01.street,
BillingCity: payload01.city,
BillingState: payload01.state,
BillingPostalCode: payload01.postal,
BillingCountry: payload01.country
})]]>
</dw:set-payload>
</dw:transform-message>
</batch:input>
<batch:process-records>
<batch:step name="Batch_Step3">
<enricher source="#[payload.size() > 0]" target="#[recordVars.exists]" doc:name="Message Enricher">
<sfdc:query config-ref="Salesforce__Basic_Authentication" query="dsql:SELECT Name FROM Account WHERE Name = '#[payload.Name]'" doc:name="Salesforce"/>
</enricher>
<logger level="INFO" doc:name="Logger"/>
</batch:step>
<batch:step name="Batch_Step5" accept-expression="#[!recordVars.exists]">
<sfdc:create config-ref="Salesforce__Basic_Authentication" doc:name="Salesforce" type="Account">
<sfdc:objects ref="#[payload]"/>
</sfdc:create>
<logger level="INFO" doc:name="Logger"/>
</batch:step>
</batch:process-records>
</batch:job>
</mule>
global.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<mule
xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
xmlns:db="http://www.mulesoft.org/schema/mule/db"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc"
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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.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/sfdc http://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="localhost" port="8081" doc:name="HTTP Request Configuration"/>
<context:property-placeholder location="example-${env}.properties"/>
<sfdc:config name="Salesforce__Basic_Authentication" username="${sfdc.username}" password="${sfdc.password}" securityToken="${sfdc.token}" doc:name="Salesforce: Basic Authentication"/>
<db:mysql-config name="MySQL_Configuration" host="${db.host}" port="${db.port}" user="${db.user}" password="${db.password}" database="${db.database}" doc:name="MySQL Configuration"/>
<jms:activemq-connector name="Active_MQ" specification="1.1" brokerURL="${jms.broker}" validateConnections="true" doc:name="Active MQ"/>
</mule>
Please go through my blogs how to integrate Salesforce with Mule and Anypoint batch processing and polling scope with Mulesoft.
Now, you know the importance of the message enricher component!
Opinions expressed by DZone contributors are their own.
Comments