You are on page 1of 4

9/1/2018 Mule Enricher Example - http://www.javarticles.com/2018/07/mule-enricher-example.

html

Java Articles Search for: Search … Search

TUTORIALS  JAVA  ANDROID DESIGN PATTERNS  SPRING  HIBERNATE CAMEL MULE WEB  DATA STRUCTURES

GUAVA HAZELCAST MONGODB ACTIVEMQ QUARTZ RXJAVA OTHERS  UNIT TESTING 

Mule Enricher Example


0

BY RAM SATISH ON JULY 13, 2018 MULE

Mule enricher as the name suggests helps use to enrich the existing message. For example, we can enrich
the payload, or add new headers, attachments to the current message or call out to another processor and
assign the result to a ow variable without overwriting the existing payload. We can de ne the part that we
are enriching in the attribute target .

Win with the leaders. Find AppExchange solutions that extend Salesforce
to every department and industry.
Salesforce AppExchange

Enriching message with an outbound property

In the below <enricher> element we initialize the outbound property header1 to payload speci ed using
attribute source . We further transform the message using <append-string-transformer> .

1 <enricher target="#[header:OUTBOUND:header1]" source="#[payload]"> ?


2 <append-string-transformer message=" and something more" />
3 </enricher>

Multiple enrich elements

We can enrich the message multiple times using child enricher elements each of which has its own target
attribute.

1 <enricher> ?
2 <append-string-transformer message=" and more " />
3 <enrich target="#[header:OUTBOUND:header1]" />
4 <enrich target="#[header:OUTBOUND:header2]" source="#[payload]" />
5 </enricher>

Enrich using ow variable

The enricher can call out to another processor or ow. In the below example, we call a di erent ow using flow-
ref , the result returned by the ow is assigned to a ow variable thus the existing payload is not overwritten.

Build Your Website with


Namecheap
Ad More Value. Better Performance.
Learn More!
Namecheap.com

Learn more

http://www.javarticles.com/2018/07/mule-enricher-example.html 1/4
9/1/2018 Mule Enricher Example - http://www.javarticles.com/2018/07/mule-enricher-example.html
1 <enricher target="#[variable:status]"> ?
2 <flow-ref name="determineStatus" />
3 </enricher>
4 <custom-processor class="com.javarticles.mule.MyProcessor"/>
5
6 <sub-flow name="determineStatus">
7 <expression-transformer expression="Processed #[payload]" evaluator="string" />
8 </sub-flow>

In the custom processor we retrieve the ow variable.

MORE POSTS

Android AlertDialog with List

Spring JdbcTemplate Example

Android DialogFragment Example

Android ListView: Adding Quick ‘Add’ action bar to


MyProcessor: the List Page

1 package com.javarticles.mule; ?
File Persistence of B-Tree Index
2
3 import org.mule.api.MuleEvent;
4 import org.mule.api.MuleException;
Guava Multiset Examples
5 import org.mule.api.processor.MessageProcessor;
6
7 public class MyProcessor implements MessageProcessor {
8 Android Nine Patch Drawable Example
9 @Override
10 public MuleEvent process(MuleEvent event) throws MuleException {
11 Object enrichedContent = event.getMessage().getInvocationProperty("status"); Mule accessing cookies example
12 System.out.println("Flow variable(status)=" + enrichedContent);
13 return event;
14 } Java Encapsulation
15
16 }
Android App Development – Skip Welcome Page

Here is the complete mule context.


Java File Compression Example

muleContext.xml: Spring Security JdbcUserDetailsManager Example

Android Drag and Drop Example

http://www.javarticles.com/2018/07/mule-enricher-example.html 2/4
9/1/2018 Mule Enricher Example - http://www.javarticles.com/2018/07/mule-enricher-example.html
Java Find All Active Threads

Spring List Examples

1 <?xml version="1.0" encoding="UTF-8"?> ? JUnit TestName Rule Example


2 <mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XML
3 xmlns:spring="http://www.springframework.org/schema/beans" xmlns:vm="http://www.mulesoft
4 xsi:schemaLocation=" RxJava Observable from Iterable Example
5 http://www.springframework.org/schema/beans http://www.springframework.org/sc
6 http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/
7 http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm Java non-daemon thread alive
8
9
10 <flow name="enrichWithAttributes"> Android Make Phone Call Example
11 <vm:inbound-endpoint path="enrich1-in"
12 exchange-pattern="request-response" />
13 <enricher target="#[header:OUTBOUND:header1]" source="#[payload]">
HashMap
14 <append-string-transformer message=" and something more" />
15 </enricher>
16 </flow>
17 Guava ClassToInstanceMap Example
18 <flow name="enrichWithElements">
19 <vm:inbound-endpoint path="enrich2-in"
20 exchange-pattern="request-response" /> Spring @TestPropertySource Annotation Example
21 <enricher>
22 <append-string-transformer message=" and more " />
23 <enrich target="#[header:OUTBOUND:header1]" /> Mule jms Endpoint Example
24 <enrich target="#[header:OUTBOUND:header2]" source="#[payload]" />
25 </enricher>
26 </flow> Spring lazy-init Examples
27
28 <flow name="enrichWithFlowRef">
29 <vm:inbound-endpoint path="enrich3-in" Android Set Theme Dynamically
30 exchange-pattern="request-response" />
31 <enricher target="#[variable:status]">
32 <flow-ref name="determineStatus" />
33 </enricher>
34 <custom-processor class="com.javarticles.mule.MyProcessor"/>
35 </flow>
36
37 <sub-flow name="determineStatus">
38 <expression-transformer expression="Processed #[payload]"
39 evaluator="string" />
40 </sub-flow>
41 </mule>

We send messages to each of the ows and verify the message properties.

MuleEnricherExample:

Oxford Blockchain Strat.


Explore the effect of blockchain on your OPEN
business strategy, online, with Oxford Saïd

1 package com.javarticles.mule; ?
2
3 import org.mule.api.MuleContext;
4 import org.mule.api.MuleMessage;
5 import org.mule.api.client.MuleClient;
6 import org.mule.api.context.MuleContextBuilder;
7 import org.mule.api.context.MuleContextFactory;
8 import org.mule.config.DefaultMuleConfiguration;
9 import org.mule.config.spring.SpringXmlConfigurationBuilder;
10 import org.mule.context.DefaultMuleContextBuilder;
11 import org.mule.context.DefaultMuleContextFactory;
12
13 public class MuleEnricherExample {
14 public static void main(String[] args) throws Exception {
15 DefaultMuleConfiguration dmc = new DefaultMuleConfiguration();
16 dmc.setId("muleexample");
17 dmc.setWorkingDirectory("/esb/mule");
18 SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
19 "muleContext.xml");
20 MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
21 contextBuilder.setMuleConfiguration(dmc);
22 MuleContextFactory contextFactory = new DefaultMuleContextFactory();
23 MuleContext ctx = contextFactory.createMuleContext(configBuilder,
24 contextBuilder);
25 ctx.start();
26 try {
27 System.out.println("Test enrich1-in flow");
28 MuleClient muleClient = ctx.getClient();
29 MuleMessage response = muleClient.send("vm://enrich1-in", "XYZ", null);
30 System.out.println("Header1: " + response.getInboundProperty("header1"));
31
32 System.out.println("Test enrich2-in flow");
33 response = muleClient.send("vm://enrich2-in", "XYZ", null);
34 System.out.println("Header1: " + response.getInboundProperty("header1"));

http://www.javarticles.com/2018/07/mule-enricher-example.html 3/4
9/1/2018 Mule Enricher Example - http://www.javarticles.com/2018/07/mule-enricher-example.html
35 System.out.println("Header2: " + response.getInboundProperty("header2"));
36
37 System.out.println("Test enrich3-in flow");
38 response = muleClient.send("vm://enrich3-in", "XYZ", null);
39 } finally {
40 ctx.dispose();
41 }
42 }
43 }

Output:

1 Test enrich1-in flow ?


2 [INFO ] 2018-07-12 18:06:52.701 [main]AbstractLifecycleManager - Initialising: 'connector.VM
3 [INFO ] 2018-07-12 18:06:52.701 [main]AbstractLifecycleManager - Starting: 'connector.VM.mul
4 Header1: XYZ and something more
5 Test enrich2-in flow
6 [INFO ] 2018-07-12 18:06:52.797 [main]AbstractLifecycleManager - Initialising: 'connector.VM
7 [INFO ] 2018-07-12 18:06:52.797 [main]AbstractLifecycleManager - Starting: 'connector.VM.mul
8 Header1: XYZ and more
9 Header2: XYZ and more
10 Test enrich3-in flow
11 [INFO ] 2018-07-12 18:06:52.813 [main]AbstractLifecycleManager - Initialising: 'connector.VM
12 [INFO ] 2018-07-12 18:06:52.813 [main]AbstractLifecycleManager - Starting: 'connector.VM.mul
13 Flow variable(status)=Processed XYZ

Download the source code

This was an example about Mule Enricher.

You can download the source code here: muleEnricherExample.zip

SHARE.       

Comments are closed.

http://www.javarticles.com/2018/07/mule-enricher-example.html 4/4

You might also like