Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple...

162
1 1 Contents Contents CONTENTS................................................................................................................................................ 1 HTTP ............................................................................................................................................................ 5 HTTP Listener ........................................................................................................................................................5 HTTP Request ........................................................................................................................................................9 HTTPS ........................................................................................................................................................ 11 Consuming Secured Service ................................................................................................................................. 14 Using HTTP Request Connector with SSL enabled ..................................................................................................14 HTTP Request Connector Configuration to access SSL enabled service ..................................................................14 Using Web Service Consumer ..................................................................................................................................16 ROUTING................................................................................................................................................. 18 Splitters ............................................................................................................................................................... 18 Collection splitter and Collection Aggregator ....................................................................................................... 19 Message chunk splitter and Message chunk Aggregator ...................................................................................... 19 Scatter Gather ..................................................................................................................................................... 20 For Each ............................................................................................................................................................... 21 Main Flow, Sub Flow and Flow reference. ........................................................................................................... 22 FILTERS:.................................................................................................................................................. 23 DATA MAPPER ...................................................................................................................................... 25 DataMapper Concepts: ........................................................................................................................................ 26 Basic Usage with Example: ......................................................................................................................................26 1. Viewing sample mapping values: ........................................................................................................................28 2. Input and Output Metadata: ...............................................................................................................................29 3. Propagating DataSense data: ..............................................................................................................................31 4. Mapping only even numbered values: ................................................................................................................33 5. Streaming large files through DataMapper: ........................................................................................................35 6. Using MEL to invoke Java functions: ...................................................................................................................37 7. Using Flows as Lookup Tables: ............................................................................................................................40

Transcript of Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple...

Page 1: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

1

1 Contents

Contents

CONTENTS ................................................................................................................................................ 1

HTTP ............................................................................................................................................................ 5

HTTP Listener ........................................................................................................................................................ 5

HTTP Request ........................................................................................................................................................ 9

HTTPS ........................................................................................................................................................ 11

Consuming Secured Service ................................................................................................................................. 14

Using HTTP Request Connector with SSL enabled .................................................................................................. 14

HTTP Request Connector Configuration to access SSL enabled service .................................................................. 14

Using Web Service Consumer .................................................................................................................................. 16

ROUTING................................................................................................................................................. 18

Splitters ............................................................................................................................................................... 18

Collection splitter and Collection Aggregator ....................................................................................................... 19

Message chunk splitter and Message chunk Aggregator ...................................................................................... 19

Scatter Gather ..................................................................................................................................................... 20

For Each ............................................................................................................................................................... 21

Main Flow, Sub Flow and Flow reference. ........................................................................................................... 22

FILTERS:.................................................................................................................................................. 23

DATA MAPPER ...................................................................................................................................... 25

DataMapper Concepts: ........................................................................................................................................ 26

Basic Usage with Example: ...................................................................................................................................... 26

1. Viewing sample mapping values: ........................................................................................................................ 28

2. Input and Output Metadata: ............................................................................................................................... 29

3. Propagating DataSense data: .............................................................................................................................. 31

4. Mapping only even numbered values: ................................................................................................................ 33

5. Streaming large files through DataMapper: ........................................................................................................ 35

6. Using MEL to invoke Java functions: ................................................................................................................... 37

7. Using Flows as Lookup Tables: ............................................................................................................................ 40

Page 2: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

2

2 Contents

JMS ............................................................................................................................................................ 43

Queues: ............................................................................................................................................................... 43

Configuration with Example: ................................................................................................................................... 43

Topics: ................................................................................................................................................................. 49

Example: .................................................................................................................................................................. 50

Example to understand how JMS uses serializing, and de-serializing objects: ...................................................... 52

DATABASE ................................................................................................................................................ 57

Database URL ...................................................................................................................................................... 58

INSERT using “Template Query” ......................................................................................................................... 60

INSERT using “Parameterized Query” ................................................................................................................. 64

INSERT using “Dynamic Query” .......................................................................................................................... 66

UPDATE using “Parameterized Query” ............................................................................................................... 69

UPDATE using “Bulk Mode” ................................................................................................................................ 71

Execute DDL ........................................................................................................................................................ 74

Bulk Execute ........................................................................................................................................................ 75

Stored Procedure ................................................................................................................................................ 77

DELETE ................................................................................................................................................................ 80

SELECT ................................................................................................................................................................. 82

Building SOAP web services in Mule .................................................................................................................... 84

Simple class as a web service ............................................................................................................................... 86

Consuming using Simple Client ............................................................................................................................ 87

Creating a service using JAX-WS service ............................................................................................................... 88

Creating Client using jaxws client ......................................................................................................................... 90

Securing Web services ......................................................................................................................................... 96

Consuming using Web service consumer ............................................................................................................. 98

JAVA CUSTOM COMPONENTS ....................................................................................................... 105

Java Component: ............................................................................................................................................... 105

Java Component with Singleton: ....................................................................................................................... 107

Invoke component: ............................................................................................................................................ 111

Reflection Entry Point Resolver: ........................................................................................................................ 114

Page 3: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

3

3 Contents

Multiple Arguments method: ............................................................................................................................ 115

Single Argument method: .................................................................................................................................. 117

No Arguments method: ..................................................................................................................................... 117

Annotated Entry Point Resolver: ........................................................................................................................ 118

Invoking a service using java component: .......................................................................................................... 119

MULE MESSAGE ENRICHER ........................................................................................................... 122

Enricher: ............................................................................................................................................................ 122

Example: ............................................................................................................................................................ 122

Description: ....................................................................................................................................................... 124

Output: .............................................................................................................................................................. 124

More Complex Enrichment: ............................................................................................................................... 125

EXPRESSIONS ..................................................................................................................................... 126

Mule Expression Component: ............................................................................................................................ 126

Mule Expression Transformer: ........................................................................................................................... 127

Example Flow: ....................................................................................................................................................... 129

Description: ........................................................................................................................................................... 129

PROPERTIES ....................................................................................................................................... 131

Property Placeholders: ...................................................................................................................................... 131

Global Properties: .............................................................................................................................................. 131

Properties Files: ................................................................................................................................................. 132

System Properties: ............................................................................................................................................. 133

Environment Variables: ..................................................................................................................................... 135

Mule-app.properties: ......................................................................................................................................... 135

Example: ............................................................................................................................................................ 136

Page 4: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

4

4

REST ........................................................................................................................................................ 137

Creating a REST Service using REST Component ................................................................................................. 137

REST Service Consumer ...................................................................................................................................... 140

TRANSACTIONS .................................................................................................................................... 145

DATA SOURCE CONFIGURATION VIA SPRING BEAN ...................................................................... 153

CACHE SCOPE ........................................................................................................................................ 154

Using Default Cache .......................................................................................................................................... 155

Using custom caching ....................................................................................................................................... 158

INSTRUCTIONS TO SET UP PROJECTS ....................................................................................... 159

BATCH PROCESSING ........................................................................................................................ 160

Input .................................................................................................................................................................. 160

Process Records ................................................................................................................................................. 160

On Complete ...................................................................................................................................................... 160

Example ................................................................................................................................................................. 160

Page 5: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

5

5 HTTP

HTTP

HTTP Listener

HTTP Listener connector provides a way to listen HTTP requests. Below figure shows the HTTP listener.

Figure-1: Http listener/Inbound Endpoint

Figure-2 shows the listener configuration, required fields are Connector configuration and Path.

Click “+” highlighted in red to create a global connector for HTTP Listener. This connector will be available for all HTTP Listeners within the application.

Figure-2: Configuration for HTTP Listener

Page 6: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

6

6 HTTP

Figure-3 shows the HTTP Listener configuration. Protocol, Host and Port are required fields. If we do not supply any of these values, defaults will be set. Below figure shows the defaults.

Figure-3: HTTP Listener configuration

Page 7: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

7

7 HTTP

Figure-4 shows the configuration for the Path element in the HTTP Listener (Figure-2) highlighted in green. All flows which are configured to use same HTTP Listener connector (Figure-3) will have the same URL. This path will be appended at the end of the URL and helps in accessing a specific application/flow.

Figure-4: Basic Settings in HTTP Listener connector.

Figure-5 shows the configuration XML for the flow created.

Figure-5: Configuration XML.

Code highlighted in red shows the response builder for the service.

statusCode- is the HTTP status code

reasonPhrase- is the text if given, appears along with statusCode when the response gets generated.

Page 8: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

8

8 HTTP

Figure-6: HTTP Listener properties

Figure-6 shows the HTTP Listener properties. This allows us to specify custom message for HTTP Response and Error Response based on the status code. The same reason will be shown in the HTTP Header.

Page 9: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

9

9 HTTP

HTTP Request

The HTTP Request Connector provides the most practical way to consume an external HTTP service. When sending HTTP requests, you can choose what method to use (GET, POST, etc) and may include a body, headers, attachments, query parameters, form parameters and URI parameters. The response is then received by the connector and is passed on to the next element in the flow.

Figure-6 shows the HTTP Request Configuration. Like HTTP Listener, HTTP Request can also have a global connector defined. This global connector is similar to the HTTP Listener connector created.

Figure-7: HTTP Request Transport

Connector Configuration and Path are similar to the HTTP Listener. Connector Configuration will have the details of the HTTP Service we want to invoke. Path is same as mentioned for that application.

Method lets us specify the HTTP method that the service accepts. This attribute can take dynamic values also.

Page 10: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

10

10 HTTP

Parameters let us supply the parameters that the service we are invoking expects. These parameter can be header, query-params etc.. we can choose from the list of options provided. We need to provide a Name and a Value for each parameter we create. These Name and Value fields also accept dynamic values.

Figure-8 shows the configuration XML for the Greeter Service.

Figure-8 : Configuration XML

Figure-9 shows the Response of the service created.

Figure-9: Response

Text highlighted in red shows the custom message that we have set as shown in Figure-5.

Text highlighted in blue shows the response generated from our service invocation.

Page 11: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

11

11 HTTPS

HTTPS

HTTPS connector is similar to the HTTP connectors shown above. The only difference is that HTTPS is SSL enabled and uses https as protocol instead of http. Configuration is similar to the HTTP connector.

Figure-10: HTTPS Listener Configuration

Figure-11 shows the TLS/SSL tab in Connector Configuration popup for HTTPS.

There are 2 ways we can provide the required certificate and keystore file to enable accessing application using HTTPS.

1. Use TLS Config: This option creates TLS configuration for the specified listener. It is not accessible outside the HTTP Listener in which it was created. Trust Store Configuration and Key Store Configuration details need to be provided. Trust store accepts “.cer” file path and password for that certificate. KeyStore accepts “.jks” file path, the password and the keystore password those were used while generating keystore.

2. Use TLS Global Config: This option creates a global TLS configuration and can be used by any HTTP connector to enable HTTPS. This also requires KeyStore, Trust Store files and passwords for those files.

Page 12: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

12

12 HTTPS

We can either create Certificate and Keystore or we can get the Certificate from the HTTPs service we are invoking.

Figure-11: TLS/SSL Configuration for HTTPS Listener Connector

Figure-12 shows the TLS configuration.

Figure-12: Configuration XML

Page 13: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

13

13 HTTPS

Figure-13: Accessing application from browser

Figure-13 shows the error that occurs when we try to access service from browser. We have two options; we can choose any of them. If we chose “close this webpage” option, the page gets closed. Other option is to continue to the web site if we chose this option we’ll be navigated to next page.

Figure-14: WSDL

Figure-14 shows the WSDL rendered after choosing to “continue to this website” option as shown in Figure-13.

Page 14: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

14

14 HTTPS

Consuming Secured Service

Using HTTP Request Connector with SSL enabled

Figure-15 shows the consumer for Secured service shown in Figure-10. This service invokes getUser operation on the secured service. We can use SOAPAction to identify an operation on the service. We need to specify a userId for which we want to retrieve the information in the request body. The request sent will be transformed to XML which will be sent to service as request body.

Figure-15: Consumer for secured SOAP Service

Figuer-16 shows the Configuration XML for the secured service consumer shown in above figure.

Figure-16: Configuration XML

HTTP Request Connector Configuration to access SSL enabled service

Figure-17 shows the HTTP request connector configuration to access a HTTPS service. This configuration is similar to the normal HTTP request configuration. We need to provide TLS configuration details i.e. KeyStore Path and Password in addition to the normal HTTP request configuration. This Keystore is same as the one used in service to enable SSL.

Page 15: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

15

15 HTTPS

Figure-17: HTTP request connector configuration for SSL

Figure-18 shows sample request and response from SOAPUI for the secured service consumer.

Figure-18: SOAP UI request and response

Page 16: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

16

16 HTTPS

Using Web Service Consumer

Figure-19 shows how to consume a secured SOAP web service using Web service consumer. This is similar to the one shown in Figure-15, but this uses Web service consumer instead of HTTP request.

The datamapper on the left side of web service consumer is generated using DataSense, this datamapper is different from the one shown in Figure-15

Figure-19: Web service consumer to consumer secured service

Page 17: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

17

17

Figure-20 shows the configuration XML for the above flow.

Figure-20: Configuration XML

Figure-21 shows the Request and Response for the Secured service consumer using the SOAPUI.

Figure-21: Request and response

Page 18: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

18

18 Routing

Routing The Routing module reviews the different types of Routers and how Routers are used to control

how messages are sent and received by components. The message can be route in different ways. Below

are explained in this example.

• Splitters and Aggregators

Collection splitter and Collection Aggregator

Message chunk splitter and Message chunk aggregator

• Scatter gather

• For each

• Mule flows, Sub flows and flow reference

• Filters

Splitters

Splitters are used to split the message and process split messages in parallel. After processing

completed, those messages get aggregate by aggregator components. Below is the splitters main flow

diagram.

Above flow exposes a HTTP service to implement collection splitter and message chunk splitter.

This flow expects a query parameter ‘splitter’. If ‘splitter’ parameter value is ‘collection’ then choice

Page 19: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

19

19 Routing

router routes the flow to collection splitter or if the value is ‘chunk’ then it routes to message chunk

splitter implementation.

Collection splitter and Collection Aggregator

Collection splitter is used to split collection object and process split messages individually.

Collection splitter expects a collection object as a payload. A java component has been used to create a

List object. Each object of List is routed to another flow via VM queue in one-way mode.

In the above flow after logger component (which logs payload) we have two important message processors. Resequencer and Collection Aggregator. While elements of List are processing invidually, the elements may get change their order. Resequencer is used to reorder the elements of List object. Collection Aggregator is used to aggregate the processed invidual message payload.

Message chunk splitter and Message chunk Aggregator

We need to provide some message payload to split the message.

Message chunk splitter is used to chunk a message into a number of fixed-length messages based on the Message Size you configure in the Message Chunk Spliter.

Page 20: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

20

20 Routing

This splitter first convert the message into byte array then split this array into chunks. Each chunked message is routed to another flow via VM queue in one-way mode.

Message Chunk Aggregator is used to aggregate the chunked messages. Byte Array to String component needs to co

Scatter Gather

Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the response of all the routes and aggregate into a single message.

Page 21: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

21

21 Routing

For Each

The Foreach scope splits a collection into elements and processes them iteratively through the processors embedded in the scope, then returns the original message to the flow.

As For Each expects a collection object is expected a java component is used to generate a List object.

Page 22: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

22

22 Routing

Above properties are available in for each scope. Collection field which accepts MEL to provide a collection object to the for each component for iteration. Counter Variable Name is a variable which stores the count of iterations. Batch Size is partition the Collection into sub collections of the specified size. Root Message Variable Name holds the message before being split.

Main Flow, Sub Flow and Flow reference.

A mule flow (main flow) which accepts message source and message processor where sub flow accepts only message processor. Sub flow can be called via flow reference because it doesn't accept messag source.

Sub flow runs completely in the same context of the flow that calls it, inheriting transcation context, exception handler, all variables and headers, etc.

A Main flow has its own context, transcation context, exception handler, all variables and headers etc.

Page 23: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

23

23 Filters:

Filters: Filters are used to filter the message using mule expressions.

Above flow accepts a HTTP request and filters the message using Expression filter and also throws an exception if Expression filter is not satisfied using Message Filter.

Page 24: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

24

24 Filters:

Expression Filter allows you to right a Mule Expression. if the expression returns true then the process continuous to next message processor. or else the flow get discarded without throwing any exception. Here the condition is checking for payload instance is java.util.List or not.

If we need to throw an exception when Expression Filter returns false, then Expression filter needs wrapped up the Message Filter and throwOnUnaccecpted attribute should be true as shown in below snippet.

<message-filter throwOnUnaccepted="true" doc:name="Message-filter-thow-exception">

<expression-filter expression="#[payload instanceof com.techm.splitters.SplitterCollections]"/>

</message-filter>

Page 25: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

25

25 Data Mapper

Data Mapper

DataMapper is a Mule transformer that delivers simple, yet powerful, visual design of

complex data transformations for use in Mule flows, including:

• Extraction and loading of flat and structured data formats

• Filtering, extraction and transformation of input data using Xpath and powerful scripting

• Augmenting data with input parameters and lookups from other data sources

• Live design-time previews of transformation results

• High-performance, scalable data mapping operations

Inputs and outputs can be “flat” (that is, row-structured) data like CSV files or Excel spreadsheet

data, or structured data in the formats supported throughout Mule: XML, JSON, key/value

Maps and trees of Plain Old Java Objects (POJOs).

Page 26: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

26

26 Data Mapper

A DataMapper-based implementation is faster to implement and easier to maintain than the

alternatives. Graphical design of the complex transformations supported by DataMapper makes

it simple to apply all of its capabilities with little coding beyond basic expressions and function

calls to transform data.

DataMapper Concepts:

Anypoint™ DataMapper takes data in a specific format and outputs the same data in the format

of your choice. For example, you can take data stored as XML and output the same data in JSON

format. Both the input and the output can be in any of the formats supported by Mule:

“Flat,” row=oriented formats:

CSV

Fixed-width

MS Excel sheets

“Structured formats:

XML

JSON

POJO object trees

Key-value Maps

You configure DataMapper using its GUI, called the graphical mapping editor. This editor has

two panes: an Input pane and an Output pane, where you define your input metadata (format,

names of fields, etc.) and your output metadata respectively.

Basic Usage with Example:

Configuring an instance of DataMapper in your flow involves several high-level steps:

1. Insert a DataMapper transformer at the desired location in your application flow.

2. Tell DataMapper what comes in and what comes out (notice the Input and Output panes in

the image below).

Page 27: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

27

27 Data Mapper

In the image above, you select XML from the Type drop-down menu in the Input pane and

provide an .xsd file to generate the structure, and JSON in the Output pane.

3. Click Create mapping (see image above) to create an initial data mapping. DataMapper will

automatically map corresponding fields between the input and output data and will leave any

other fields unmapped.

4. If necessary, graphically modify the mapping, defining input elements and attributes to

output elements and attributes:

Page 28: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

28

28 Data Mapper

Note: unlike most components in Anypoint Studio, the DataMapper doesn't offer a way of

being configured via XML code. Mappings must always be done via the GUI, they are then

stored as .grf files in the /mappings folder. All you can do via your XML code is to reference one

of these existing mapping .grf files.

1. Viewing sample mapping values:

DataMapper uses the Input file example to define input fields; it automatically detects the

information in the file and displays them as sample values for each field.

For example, the mapping input was a CSV file which contained the following information:

company_name, company_address, company_city, company_city, company_state, company_zip

Universal Exports, 55 Main Street, Miami, fl, 33126

Add a DataMapper to my flow and used the example CSV file to define the input fields. Because

the example CSV contains values for each field, DataMapper displays sample values for each

field to make mapping more intuitive.

Page 29: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

29

29 Data Mapper

2. Input and Output Metadata:

Metadata describes the data formats of the input and output. For "flat" data, this is a list of

column names, data types and possibly sizes. For structured data, the metadata describes a

tree-like hierarchy of elements and attributes, with element and attribute names, data types,

and sizes and so on.

DataMapper is intelligently predictive, it will automatically populate fields when it can guess

what you're likely to want to map. If Anypoint Studio can use Data Sense to access metadata

about the elements that fit in the flow before and after your DataMapper instance, then the

structures of input and output data will be autocompleted as soon as you place the

DataMapper instance in the flow. If these predictions don't match what you need, you're free

to edit these values at will.

When Studio has access to the Meta-Data at design time, you can preview what this known

metadata looks like before adding a DataMapper to your flow. The Metadata explorer displays

the data structure of both the input and output of any component in the flow. By looking at two

adjacent components, you can tell if they can truly communicate with each other effectively or

if some conversion is necessary in between, the DataMapper is often the ideal tool to make this

conversion.

At times, you may need change some fields and re-create the mapping accordingly.

DataMapper has a “magic” tool to make this happen.

Click the “magic wand” icon in the upper left-hand corner of the Input panel to display the

Metadata Handling tools.

Reload Metadata:

Page 30: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

30

30 Data Mapper

Step 1: Right-click your main input mapping item (in the example above, “companies2”), and

select Add field. Enter a name for your new field, use the drop-down to define the type, then

click OK to save.

Step 2: Click the magic wand, then select Reload Metadata.

Step 3: Watch as DataMapper magically uploads a sample value for your new field. In such a

case, the value is “null”. My example below has a new field for

“has_given_contact_permission”.

Recreate Metadata:

Step 1: Add an input field to your CSV.

Step 2: In your Input panel, click Re-Create Metadata. Browse to select your newly modified

CSV example file, and then click OK. The new field appears in the Input panel.

Recreate Metadata from Input:

If you want to include the new field in the output, click the “magic wand” icon in the Output

panel, then select Re-Create Metadata From Output to transfer all input fields – including any

new ones – to the output panel.

Page 31: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

31

31 Data Mapper

3. Propagating DataSense data:

Automatically import a Anypoint Connector’s data structure to DataMapper? Consider it

done! Using DataSense, you can easily map data between connectors without the hassle of

manually researching and defining the fields. Each connector sucks in the data structure from

its respective SaaS, and a DataMapper dropped between them pulls in the metadata so you can

configure input and output with a few clicks.

To demonstrate, I’ll map a Salesforce connector’s inout to another Salesforce connector’s

output.

Step 1: Build a flow with two Salesforce Anypoint Connectors.

Step 2: Configure each Salesforce connector, testing the connectivity of each. See Testing

Connections for details.

Step 3: Drop a DataMapper between the Salesforce connectors.

Page 32: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

32

32 Data Mapper

Step 4: Double-click to open the DataMapper. DataSense has already populated the input and

output configurations, pulled automatically from each connector.

Step 5: Click Finish and witness all necessary input and output fields appear, ready for drag-

and-drop mapping.

Page 33: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

33

33 Data Mapper

Example:

4. Mapping only even numbered values: Here is a sample example to illustrate this activity, consider the following XML as input and the

expected output should be the xml with only even ids:

To achieve this goal, I created an XML-to-XML mapping, transforming them to XSD.

DataMapper produced the following:

Page 34: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

34

34 Data Mapper

Then create a Rule to check if the id is even or not.

Page 35: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

35

35 Data Mapper

Then use the Rule to map the output values with input if the id is even.

The output we obtained is:

5. Streaming large files through DataMapper:

Streaming extra-large files through DataMapper without consuming tons of memory is

possible. Let me illustrate with an example.

Page 36: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

36

36 Data Mapper

The HTTP endpoint accepts a message – a large file – which it passes into a DataMapper.

Passing through a Logger, the message then reaches a Foreach which wraps a Database

endpoint. DataMapper must create “iteratable” objects from the file and so that the Foreach

can process the items iteratively and push them into the database. In order to manage the

processing of this large file, you can enable streaming on DataMapper.

Step 1: To enable streaming, click to open the DataMapper Properties (upper right hand corner

of the DataMapper console).

Step 2: Check the box to enable streaming.

Page 37: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

37

37 Data Mapper

Step 3: Save and start streaming!

6. Using MEL to invoke Java functions:

We can use one of two expression languages in DataMapper: Mule Expression Language (MEL)

or Clover Transformation Language (CTL) and we can invoke Java functions using MEL.

Step 1: When you create a new mapping, DataMapper utilizes MEL by default. If you have

previously changed your Default Script Type to CTL, you can change it back to MEL in the Mule

Studio Preferences (Mule Studio > Preferences).

Page 38: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

38

38 Data Mapper

Step 2: Create any mapping you want, then click “Script” (upper right corner of the DataMapper

console) to view the script of the mapping which looks something like this: “output.name =

input.name”.

Step 3: Click to set your cursor just after “input.name” then add “.toLowerCase()” . This

modification invokes a Java function to change the input name to lowercase. See example

below.

Step 4: We can also call a java class in the script tag and check the example below:

Page 39: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

39

39 Data Mapper

TIP! We can also use auto-complete to invoke a Java function? Set your cursor at the end of

“input.name” then hit “Ctrl + Space Bar” to display a list of auto-complete options.

Page 40: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

40

40 Data Mapper

7. Using Flows as Lookup Tables:

Let’s say you need to map and transform a message payload from one structure and format to

another. Of course, DataMapper is the perfect tool for that job. However, what if you also

need to add to the payload as it is transformed and mapped? You could use DataMapper and a

lookup table (CSV, DB or User Defined) to find and add data to a message.

Use Case: A company needs to upload contacts in a XML file to other source. The gender of the

employees is to be matched to the Male or Female fields based on the Salutation.

To meet these objectives, we’re going to use a DataMapper and a FlowRef Lookup table to

access another flow with a Groovy script which uses the value of “State” to determine

“Region”.

Step 1: After creating the mapping create a CSV Lookup table.

Page 41: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

41

41 Data Mapper

Step 2: Create the mapping in CSV Lookup table as below.

Page 42: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

42

42 Data Mapper

Step 3: The mapping in DataMapper should be as below:

Step 4: Edit the mapping in Script as below:

Step 5: The Final output will be ass transformed below:

Page 43: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

43

43

JMS

JMS (Java Message Service) is a widely-used API for Message Oriented Middleware. It allows

communication between different components of a distributed application to be loosely

coupled, reliable, and asynchronous.

JMS supports two models for messaging:

Queues - Point-to-point

Topics - Publish and subscribe

Mule's JMS transport lets you easily send and receive messages to queues and topics for any

message service which implements the JMS specification.

Queues:

In the point-to-point or queuing model, a sender posts messages to a particular queue and a

receiver reads messages from the queue. Here, the sender knows the destination of the

message and posts the message directly to the receiver's queue. It is characterized by the

following:

Only one consumer gets the message

The producer does not have to be running at the time the consumer consumes the

message, nor does the consumer need to be running at the time the message is sent

Every message successfully processed is acknowledged by the consumer

Configuration with Example:

Anypoint Studio offers easy-to-use components to connect to JMS Queues and Topics. In the

below example, we will use ActiveMQ, a leading open source JMS implementation from Apache

that supports JMS.

The outline of the simple steps required to implement this example is as follows:

Import ActiveMQ libraries

Page 44: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

44

44 JMS

Create an ActiveMQ connector

Enqueue to JMS Queue

Run the sample

1. Import ActiveMQ Library:

Download ActiveMQ binaries from http://activemq.apache.org/download.html (ActiveMQ

5.11). Unzip the compressed file to any folder on your file system. Let’s call it

<ACTIVEMQ_HOME>.

Create a new flow in Mule Studio and name it “jms”. To configure the project to use ActiveMQ

libraries, right-click on the project -> Properties -> Java Build Path -> Add External JARs. Select

“activemq-all-5.11-SNAPSHOT.jar” from <ACTIVEMQ_HOME>.

Page 45: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

45

45 JMS

2. Create an ActiveMQ Connector

Double-click on “jms” flow to bring up the message flow. Click on the “Global Elements” tab.

Click on “Create” and locate ActiveMQ under “Connectors -> JMS”. Leave the default values for

the Active_MQ connector and click OK.

Mule will initialize the ActiveMQ connector with a default instance of the ActiveMQ connection

factory and establish a TCP connection to the remote standalone broker running on a local host

and listening on port 61616.

3. Enqueue to JMS Queue

We will use the request payload received from an HTTP inbound endpoint to seed the

ActiveMQ Queue. Open the “jms” message flow and drag and drop an HTTP endpoint on to the

flow.

Page 46: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

46

46 JMS

Double-click on the HTTP endpoint to bring up the properties dialog. Specify “jms_queue” for

Path. This will make the HTTP endpoint accessible using URL http://localhost:7777/jms_queue.

Set a payload that you want to add to the queue.

Drag and drop a JMS endpoint next to the HTTP inbound endpoint.

Double-click the JMS endpoint to bring up the properties dialog. Specify “queue” for Queue

name.

Select “Active_MQ” for Connection Reference in the Connector Configuration that we created

in Step 2.

The final JMS Message Flow is as below:

Page 47: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

47

47 JMS

4. Create a Jms_receiver

Use a Jms endpoint to receive the messages in the queue. Its configuration is as follows:

Use a Logger to log the Received Message.

Use Set-payload to send a response back to the Jms Service.

The final JMS Receiver Message Flow is as below:

Page 48: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

48

48 JMS

5. Run the sample

First, run ActiveMQ using executable “activemq-admin” from <ACTIVEMQ_HOME>/bin folder.

Make sure you can access the ActiveMQ admin page using http://localhost:8161/.

Run the JMS flow.

Open http://localhost:7777/jms_queue.This will enqueue the request to the ActiveMQ JMS

Queue “queue”.

Verify this by examining the ActiveMQ administration page at

http://localhost:8161/admin/queues.jsp. We can see the messages under the “Messages

Enqueued” column.

Page 49: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

49

49 JMS

The Output you receive after the execution is the Payload Set by the JMS-Client.

Note: Only one client can read from a queue at one time and the messages read from the queue are

removed from the queue. If you want to perform any transactions on top of JMS then the Transaction

settings come in handy.

Topics:

The publish/subscribe model supports publishing messages to a particular message topic.

Subscribers may register interest in receiving messages on a particular message topic. In this

model, neither the publisher nor the subscriber knows about each other. A good analogy for

this is an anonymous bulletin board. The following are characteristics of this model:

Multiple consumers (or none) receive the message

There is a timing dependency between publishers and subscribers. The publisher has to

create a message topic for clients to subscribe.

The subscriber has to remain continuously active to receive messages, unless it has

established a durable subscription. In that case, messages published while the

subscriber is not connected redistribute when it reconnects.

Page 50: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

50

50 JMS

Note: The configuration is same as the ‘queue’ but we use topics in the JMS Connector

Configuration.

Example:

JMS Publisher Flow Configuration:

Open the “jms” message flow and drag and drop an HTTP endpoint on to the flow. Double-click

on the HTTP endpoint to bring up the properties dialog. Specify “jms_topic” for Path. This will

make the HTTP endpoint accessible using URL http://localhost:7777/jms_topic.

Set a payload that you want to add to Publish.

Drag and drop a JMS endpoint next to the HTTP inbound endpoint.

Double-click the JMS endpoint to bring up the properties dialog. Specify “topic” for Topic name.

Select “Active_MQ” for Connection Reference in the Connector Configuration that we created

earlier.

Page 51: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

51

51 JMS

The JMS Publisher Message Flow is as below:

JMS Subscriber Flow Configuration:

Use a Jms endpoint to subscribe the Published messages. Its configuration is as follows:

Use a Logger to log the Received Message.

Use Set-payload to send a response back to the Jms Service.

Create another Subscriber to receive the same Published messages.

The final JMS Subscriber Message Flow is as below:

Page 52: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

52

52 JMS

Run the JMS flow. Open http://localhost:7777/jms_topic.

This will publish the request to the ActiveMQ JMS Topic “topic”. Verify this by examining the

ActiveMQ administration page at http://localhost:8161/admin/topics.jsp. We can see the

messages enqueued, dequeued and the number of consumers.

The Output you receive after the execution is the Payload Set by any of the two subscribers JMS-

Topic 1 or JMS-Topic2.

Example to understand how JMS uses serializing, and de-serializing

objects:

Serialization is a process of converting an object into a sequence of bytes which can be

persisted to a disk or database or can be sent through streams. The reverse process of creating

object from sequence of bytes is called deserialization.

Steps to share Objects via JMS are:

1. Open the “jms” message flow and drag and drop an HTTP endpoint on to the flow. Double-

click on the HTTP endpoint to bring up the properties dialog. Specify “/jms_serializable_queue”

for Path. This will make the HTTP endpoint accessible using URL

http://localhost:7777//jms_serializable_queue.

2. Create a Java Class that implements the Serializable interface as below:

Page 53: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

53

53 JMS

3. Instantiate the class using Set-payload.

4. Set the values to the object using Expression.

Page 54: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

54

54 JMS

4. Drag and drop a JMS endpoint next to the HTTP inbound endpoint.

Double-click the JMS endpoint to bring up the properties dialog.

Specify “serial_queue” for queue name. Select “Active_MQ” for Connection Reference in the

Connector Configuration that we created earlier.

Page 55: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

55

55 JMS

5. The JMS Client Message Flow is as below:

6. Use a Jms endpoint to receive the messages on the Destination with the below configuration

and Active_MQ is configured before.

7. Use an expression to print the contents of the received payload.

Page 56: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

56

56 JMS

8. Use a Logger to log the Received Message.

9. The final JMS Subscriber Message Flow is as below:

Run the JMS flow. Open http://localhost:7777/jms_serialiable_queue.

This will publish the request to the ActiveMQ JMS Topic “topic”. Verify this by examining the

ActiveMQ administration page at http://localhost:8161/admin/queues.jsp. We can see the

messages enqueued, dequeued and the number of consumers.

The Output you receive after the execution is the name that is sent from the Client.

The Serializable implementation is done in a similar way using Topic but with many publishers

and subscribers.

Page 57: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

57

57

Database

The Database connector replaces the JDBC connector. The Database connector allows us to connect

with database; it allows us to run different SQL operations on the database we have connected to. These

operations include SELECT, INSERT, UPDATE, DELETE, Stored procedures and DDL. The Database

connector lets us perform predefined queries as well as queries that take the connector's input to

specify variable parameters or even to construct sections of the query dynamically. All the examples

shown in this document are executed using the PostgreSQL database.

Figure-36: Database connector in pallette

Figure-37 shows Database connector configuration.

Page 58: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

58

58 Database

Figure-37: Database connector configuration

Figure-38 shows the Database configuration which gets opened when we click on “+” symbol highlighted

in red as shown in Figure-37. We have 2 ways in which we can configure database for accessing using

Database Connector.

1. Database URL

2. Configure via spring-bean

Database URL

Below screenshot shows the configuration using Database URL. It requires values for 2 attributes URL

and Driver Class Name.

URL- is the connection string. We can provide the user name and password if required, to access the

database. This is similar to obtaining connection in Java using JDBC.

Page 59: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

59

59 Database

Figure-38: Database configuration

Driver Class Name- is the class name which implements java.sql.Driver. This class can be found in the

database specific jar included in the classpath. In the example shown, we are connecting to postgresql

database.

Enable DataSense – this option enabled DataSense, i.e. when a datamapper is placed on to the left or to

the right of the Database connector, the corresponding request (if placed on to left) or the response (if

placed on to right) will be populated automatically.

Once this values are supplied, we can click on “Test Connection” (this is optional) button to test the

connection to the database with the given values. Test will be successful, when connector is able to

connect to the database with the given values.

Figure-39 shows the list of operations available in Database connector. These operations are the SQLs

DDL and DML statements. We can select any of those operations shown.

Figure-39: List of operations available in Database connector

Figure-40 shows the type of statements available for the selected operation.

Figure-40: Type of SQL

Figure-41 shows the “Advanced” tab and the options available in it. In this tab we can provide the Auto

generated Keys so that we need not include them in INSERT, UPDATE statements. These columns will

have auto generated value or a default value. In the example shown, “id” is the Auto generated/Auto

Incremented column hence we need not supply a value while INSERTing a row. “created” column is

TIMESTAMP and the default value given for this is CURRENT_TIMESTAMP. So whenever a row is created

Page 60: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

60

60 Database

or modified current TIMESTAMP will be saved into this column against the row that is created or

modified.

Transactional Action-is optional, it has a list of actions from which we can select one. Default is

JOIN_IF_POSSIBLE; other options are ALWAYS_JOIN and NOT_SUPPORTED.

Figure-41: Auto generated Keys in Database connector

INSERT using “Template Query”

Figure-42: Using Template Query to Insert a record

Figure-42 shows how to insert a record in database table using “Template Query” (shown in Figure-40).

The Database configuration is same as shown in Figure-38.

Figure-43 shows the Database connector configuration for INSERT using Template Query

Page 61: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

61

61 Database

Figure-43: Connector Configuration

Figure-44 shows the Template Query global configuration this window gets opened when we click on “+”

symbol highlighted (Figure-43) in red.

Query Type – is the type of the query we want to execute, we have 2 options here Parameterized Query

and Dynamic Query.

Parameterized Query with named parameters – is the SQL statement we want to run. We can wither

provide values directly or using named parameters. In this case, it accepts named parameters. Input

parameters are given in Input parameters section as shown in Figure -44. Input Parameters section has

4 parameters (firstname, lastname, email, phone) defined with the values assigned from flow variables

the same parameters are used in the parameterized query.

Dynamic Query – this can accept a query prepared outside the connector. We do not have any Input

parameters for this option since we can prepare a query outside the connector.

Page 62: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

62

62 Database

Figure-44:Template Query Configuration

Page 63: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

63

63 Database

Figure-45 shows the Expression component used to parse payload and assign the values to flow

variables required to insert a record in a database table.

Figure-45: Expression component to process payload

Figure-46 shows the Configuration XML for the INSERT using Template Query

Figure-46: Configuraton XML

Page 64: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

64

64 Database

Figure -47 shows the request and response for Insert using Template Query

Figure-47 : Request and Response

INSERT using “Parameterized Query”

Figure-48: Using Parameterized Query to Insert a record

Figure-48 shows the flow configuration for inserting a record using Parameterized Query. Flow

configuration is similar to the one shown in “INSERT using “Template Query” ”. Only change is the

Database connector.

Page 65: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

65

65 Database

Figure-49 shows the Database connector configuration to use parameterized query to insert a record in

database table. Values for the flow variables are set in the expression component used in the flow. This

is same as the one used for INSERT using Template Query.

Type – Parameterized

Operation - Insert

Figure-49: INSERT using Parameterized Query

Figure-50 shows the Configuration XML for INSERT using Parameterized Query.

Figure-50: Configuration XML for Parameterized Query

Page 66: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

66

66 Database

Figure-51: Request and Reponse

Figure-51 shows the request and response to insert a record using Parameterized query.

INSERT using “Dynamic Query”

Figure-52: Flow configuration to insert a record using Dynamic Query

Figure-52 shows the flow configuration for inserting a record using Dynamic Query. Flow configuration is

similar to the one shown in “INSERT using “Template Query” ”. Only change is the Database connector.

Page 67: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

67

67 Database

Figure-53: Expression component to parse input payload

Figure-53 shows Expression component used to parse input payload and prepare a query with the

values set. The query created is given as input to the Dynamic query. Other way is to prepare the query

in the Dynamic query itself instead of preparing it outside.

Figure-54: Dynamic query

Figure-54 shows using a dynamic query to insert a record in database table. In this example, query is

prepared in the Expression component and set in flow variable. The same flow variable

dynamicInsertStmt is given as input to the Dynamic Query.

Page 68: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

68

68 Database

Figure-55 shows the Configuration XML to insert a record using Dynamic Query.

Figure-55: Configuration XML

Figure-56 shows the request and response to insert a record using Dynamic Query.

Figure-56: Request and Response

Page 69: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

69

69 Database

UPDATE using “Parameterized Query”

Figure-57 shows the flow configuration for updating a record using Parameterized Query. Flow

configuration is similar to the one shown in “INSERT using “Template Query” ”. Only change is the

Database connector.

Figure-57: Flow configuration to update a record using Parameterized query.

Figure-58 shows the Database connector configuration to update a record using Parameterized query

Type-Parameterized

Operation-Update

Figure-58: Database Connector

In this example, we are going to update email id alone for the given employee id. The same is given in

the Parameterized query and the values for email and employee id are read from the payload and set to

flow variables in the expression component used in this flow.

Page 70: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

70

70 Database

Figure-59 shows the configuration XML for updating a records data using Parameterized query.

Figure-59: Configuration XML

Figure-60 shows the request and response for to update a record using Parameterized query. Response

for this operation is the number of rows updated. In this example, response is “1”.

Figure-60: Request and Response

Page 71: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

71

71 Database

UPDATE using “Bulk Mode”

Figure-61 shows how to update a set of records using Bulk Mode option available in Database

connector. This Bulk Mode accepts a collection and iterates over each collection item and updates in the

database.

Figure-61: Flow to update a record using Bulk Mode

Connector configuration is similar to the one shown in “INSERT using “Template Query” ”. Only change

is the Database connector.

Figure-62: Connector configuration for Bulk Mode udpate

Figure-62 shows the connector configuration to update multiple records using Dynamic Query and Bulk

Mode (highlighted in red). Values for the email and id columns are supplied using a collection as

payload.

Page 72: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

72

72 Database

Figure-63 shows the Expression component to fetch data from payload. In the code shown below, a map

is prepared using the employee data retrieved from the input payload and the same map is set as

payload which will be used by Database connector to update the data in a database table.

Figure-63: Expression component to prepare payload

Page 73: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

73

73 Database

Figure-64 shows the configuration XML for Database update using Bulk Mode.

Figure-64: Configuration XML

Figure-65 shows sample request and response to update multiple records using Bulk Mode. Response

shows whether a record is updated or not. 1 indicated update successful, 0 indicates failure.

Figure-65: Request and Response

Page 74: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

74

74 Database

Execute DDL

Using this option we can perform a DDL operation. Connector configuration is similar to the one shown

in “INSERT using “Template Query” ”. Only change is the Database connector.

Figure-66: Flow to execute DDL

Figure-67 shows DDL. The ALTER statement shown adds a new column “lastModified” to the employee

table.

Figure-67: Execute DDL

Figure-68 shows the configuration XML for the Execute DDL operation.

Figure-68: Configuration XML

Page 75: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

75

75 Database

Figure-69 shows the Request and Response for the Execute DDL flow. Response 0 indicates the

operation is successful.

Figure-69: Request and Response

Bulk Execute

The operation “Bulk Execute” available in Database connector lets us execute multiple SQL statements

in single connector. This is different from the “Bulk Mode” we have seen in UPDATE using “Bulk Mode”.

Bulk Mode executes same statement with different set of data which is provided as a collection. Bulk

Execute lets us specify multiple SQL statements in the same query text and executes them.

Figure-70: Flow configuration for Bulk Execute

Page 76: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

76

76 Database

Figure-71 shows the Database connector for Bulk Execute operation. In the query text field, we have

provided 3 SQLs each terminated with a semicolon (;). In this example, we are executing an INSERT,

UPDATE and DELETE statements. Values for the insert statement are set using an Expression

component. Input payload gets parsed in expression component and the required values for the INSERT

statement are set in flow variables written in the Query text.

Figure-71: Database connector for Bulk execute

Figure-72 shows the Configuration XML for the Bulk Execute operation.

Figure-72: Configuration XML

Page 77: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

77

77 Database

Figure-73 shows sample request and response for Bulk Execute operation. Response indicates the

number of rows created, deleted and updated by executing the 3 statements.

Figure-73: Request and Response

Stored Procedure

Database connector provides an option to execute stored procedures which are stored on Database

server. This is similar to calling a stored procedure using CallableStatment in Java. Database connector

configuration is similar to the one shown in “INSERT using “Template Query” ”. Only change is in

operation.

Figure-74 shows the Flow configuration to call a stored procedure using Database connector.

Figure-74: flow configuration to call a stored procedure

Page 78: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

78

78 Database

Figure-75 shows the Database connector configuration to execute a stored procedure. We can choose

any of the Query Type from the drop-down. In this example, we have chosen Dynamic; other options

are Parameterized Query and Template Query. The configuration for these query types is same as

shown in INSERT using “Template Query”, INSERT using “Parameterized Query”, INSERT using “Dynamic

Query”.

Figure-75: Database connector for Stored Procedure

Figure-76 shows the SQL for the stored procedure get_emp_details. This store procedure takes

employee id as IN param and returns employee information as OUT param.

Figure-76: Stored procedure get_emp_details

Page 79: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

79

79 Database

Figure-77 shows the configuration XML for Stored procedure operation using Database connector.

Figure-77: Configuration XML

Figure-78 shows sample request and response for the stored procedure flow.

Figure-78: Request and Response

Page 80: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

80

80 Database

DELETE

Database connector provides an option to delete record(s) from a database table using DELETE

operation. Database configuration is similar the ones shown in above. The change comes in the

Database operation. Figure-79 shows the Flow configuration for DELETE operation.

Figure-79: DELETE operation

Figure-80 shows Database connector configuration to perform DELETE operation. Bulk Mode, Query

Type (Dynamic, Parameterized, Template Query) shown in previous sections applies to this as well.

Configuration remains same for all these.

Figure-80: Database connector for DELETE operation.

Page 81: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

81

81 Database

Figure-81 shows the configuration XML for the DELETE operation using Database connector.

Figure-81: Configuration XML

Figure-82 shows sample request and response for the DELETE operation using Database connector.

Response shows the number of rows deleted.

Figure-82: request and response

Page 82: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

82

82 Database

SELECT

Database connector provides an option to fetch record(s) from a database table using SELECT operation.

Database configuration is similar the ones shown in above. The change comes in the Database

operation. Figure-83 shows the Flow configuration for SELECT operation.

Figure-83: Flow configuration for SELECT operation

Figure-84 shows Database connector configuration to perform SELECT operation. Bulk Mode, Query

Type (Dynamic, Parameterized, Template Query) shown in previous sections applies to this as well.

Configuration remains same for all these.

Figure-84: Database connector configuration for SELECT operation

Page 83: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

83

83 Database

Fogure-85 shows configuration XML for SELECT operation in Database connector.

Figure-85: Configuration XML

Figure-86 shows sample request and response for SELECT operation in Database connector.

Figure-86: Request and Response

Page 84: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

84

84 Database

Building SOAP web services in Mule

Mule provides “cxf” connector to build and consume SOAP based web services. We can expose a

JAX-WS service using CXF connector.

Figure-1: Service Flow.

Above figure shows the flow configuration to build a SOAP web service using CXF connector provided by

mule.

Figure-2:Select the “Generate from WSDL”

Page 85: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

85

85 Database

As shown in Figure-2, click “Generate from WSDL” button if you are building a WSDL first service.

Give the details of WSDL location and package name (to generate source files) in the popup; CXF will

generate the source files in the specified package.

Figure-3:CXF connector Configuration

Above figure shows the CXF configuration elements. Specify the details Port, Namespace, Service as

mentioned in WSDL. Service Class is optional; we can mention the interface created for our service.

Next, provide the implementation of Interface generated using WSDL and add it to the flow using java

component as shown in Figure-1.

Once you invoke service using the configuration mentioned in <http:listener> like:

http://{host}:{port}/{path}?wsdl

You will see the WSDL loaded.

Page 86: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

86

86 Database

Simple class as a web service

A simple service allows us to create web services which don't require annotation. To do this, we need to

write an interface and supply it as the “serviceClass” to the CXF component, and the implementation

class needs to be supplied as value to the “class” attribute of “component”.

Figure-4: Simple Service configuration

In the code shown in figure-4, value supplied to serviceClass is an interface, and the value supplied to

class (UserInfoImpl) attribute is the implementation of the interface (UserInfo).

Figure-5: SimpleService flow configuration

Figure-6: Sample Request-Response for Simple Service configuration.

Page 87: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

87

87 Database

Consuming using Simple Client

We can build a client for your simple frontend based services without the need to generate a client from WSDL. To

do this, we need a copy of the service interface and all the data objects locally to use. This can simplify consuming

web services if we already have access to the code used to build the service.

Figure-7: shows the XML configuration

Above figure shows the XML configuration to consume a service using simple-client. Here, we need to

have all the java classes copied to client application which are used to create service. Similar to the

service creation, we need to provide the interface as the value for the serviceClass attribute in

<cxf:simple-client>, no implementation class is required. After configuring simple-client, we need to

invoke the service using the outbound endpoint.

Figure-8: Flow Configuration

Above figure shows the Flow configuration for the simple-client service.

Page 88: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

88

88 Database

Figure-9: Sample request-response

Creating a service using JAX-WS service

The JAX-WS specification defines a series of APIs and annotations that help us build web services. To

create a web service using JAX-WS, we need to have an interface annotated with @WebService, and the

implementation class of that interface. Interface needs to be supplied as value to the serviceClass

attribute in <cxf:jaxws-service>, and the implementation class needs to be supplied as value to the class

attribute in <component>.

Figure-10: XML configuration for JAXWS service

Page 89: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

89

89 Database

Figure-11: Flow Configuration for JAX-ws service

Above figure shows the flow configuration for the XML shown in figure-10.

Figure-12: Sample request-response

Above figure shows the request and response for the JAXWS service.

Page 90: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

90

90 Database

Creating Client using jaxws client

We can also build a client for JAX-WS services without the need to generate a client from WSDL. To do

this, we need a copy of your service interface and all data objects locally to use. This can simplify

consuming web services if we already have access to the code used to build the service.

Figure-13: XML configuration for jaxws-client

Figure-14: Flow Configuration for Jax-ws client

Page 91: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

91

91 Database

Figure-15: Sample request-response

Page 92: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

92

92 Database

Consuming SOAP web services in Mule using Web Service consumer

We can consume a SOAP web service using Web Service Consumer.

Figure-16: Consuming a SOAP web service

Configuration shown in the above figure exposes a WSDL(generated by Service) to work as proxy. In the

above figure, SOAP component is configured as “Proxy Service”.

Figure-17: CXF proxy service configuration

Page 93: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

93

93 Database

Above figure shows the details configured in CXF. Values for Port, Namespace, and Service are same as

mentioned in WSDL.

Figure-18: Advanced tab in CXF

In the “Advanced” tab provide the WSDL location, this can be a server URL or location of WSDL placed in

our application folders.

Figure-19: SOAPAction variable

We’ll identify the operation to invoke based on the “SOAPAction” mentioned in the WSDL supplied. The

same SOAPAction is used in “choice” block to route.

Figure-20: Web service consumer properties

Page 94: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

94

94 Database

Above figure shows the properties available for Web service consumer. Connector configuration is

shown in Figure-9 Web Service Consumer properties. Opertaion gets populated after the connector is

configured. If there are more than 1 opertaions are available, the drop-down provided will let us choose

the operation we are interested in. Otherwise, if there is only one operation available on the service we

want to invoke, the same will be selected by default.

Figure-21: Web Service Consumer properties

Above figure shows the configuration details of Web service Consumer. Click “+” (highlighted in red).

WSDL location can be a service URL as shown in the above figure, or a WSDL placed in the application.

The details Service, Port, Address will be auto populated soon after the WSDL location is specified.

Enable DataSense, is optional. If we choose this option, Mule provides the request structure and

response structure when we use DataMapper along with the Web service consumer.

Page 95: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

95

95 Database

Figure-22: Web service consumer with Datamappers

Datamapper to the left of the Web service consumer will have the input structure accepted by the web

service.

Figure-23: Datamapper at the request end

XML structure on the left side is the payload that is passed from our service. XML structure on the right

side (highlighted in RED) is the input structure accepted by web service consumer. If the DataSense

option is enabled, structure accepted by web service will be automatically generated.

Figure-24: Datamapper at response end

Above figure shows the datamapper configuration at the response end (i.e. at the right side of the web

consumer in Figure-10). XML structure on the left is generated when we enable the data Sense option in

Web service consumer. XML structure on right side is the structure we want to display.

Page 96: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

96

96 Database

Securing Web services

Mule provides few security implementations to secure services. In this document, we’ll see how to work

with Basic Authentication and UserNameToken authentication.

Basic Authentication:

Figure-25: Basic Authentication

Figure-26: Code for basic Authentication

Above configuration uses spring security to provide basic authentication. Basic Security Filter added at

after the http:listener to enable basic authentication.

Page 97: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

97

97 Database

Accessing Service enabled with Basic authentication using SOAPUI

Figure-27:SOAPUI project to access basic authentication enabled service

When we invoke the service after configuring basic authentication, SOAPUI prompts for credentials.

Give the credentials as mentioned in Figure-14. When invoking a specific operation as shown in the

above figure, we need to supply the same credentials as shown (highlighted in RED ).

Page 98: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

98

98 Database

Consuming using Web service consumer

We can consume a service with basic authentication using web service consumer just like a normal

service. One additional configuration required is <http:request> connector with basic authentication

enabled. <http:request> connector needs to be supplied as connector to web service consumer.

Figure-28:HTTP requester configuration for web service consumer

Above figure shows the “References” tab in web service consumer. ”General” tab is shown in Figure-9

Page 99: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

99

99 Database

Figure-29:HTTP request configuration with basic authentication

As shown in Figure-16, once a HTTP request connector is created, provide the authentication details in

selecting the “Authentication” tab. In this case, Basic authentication is selected as this needs to access

the service with basic authentication.

Page 100: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

100

100 Database

Figure-30: Accessing service with basic authentication service using a consumer

Response shown in the above figure is similar to the one we received for service as shown in Figure-15.

UsernameToken authentication:

Figure-31: UsernameToken authentication configuration

Snippet highlighted in RED adds UsernameToken to the CXF service.

Figure-32: Authentication manager configuration

Above code is similar to the one shown in Figure-14. Authentication manager is not required as we are

using Custom Token validator to validate password.

Page 101: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

101

101 Database

Accessing UsernameToken service using SOAPUI

Figure-33: SOAPUI project to consume service with UsernameToken

Double-click on the SOAPUI project(UsernameTokenExample), a window highlighted in RED will get

opened. Click on the “WS-Security configurations” tab to configure security required to access a service.

Figure-34: configuring security

Click “+” to configure security. Give a name in the pop came up.

Page 102: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

102

102 Database

Figure-35: Add the authentication required

Click the “+” (highlighted in RED) to add the mode of authentication required. Select the authentication

(Username) from the drop-down.

Figure-36: Adding the required details

Select the “username” created in Figure-23, and give the details Username, password as configured in

the service.

Page 103: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

103

103 Database

Figure-37: Applying the security mode

Right click on the request and select the Apply ”UsernameToken” option to apply the UsernameToken

authentication we have created in Figure-24.

Figure-38: SOAP request with usernameToken authentication

On applying the “UsernameToken” authentication, the request would look similar to the one shown in

the above figure.

Page 104: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

104

104

Figure-39: Response

Once you invoke the service, the response would look similar to the one shown in figure.

Consuming service enabled with usernametoken using webserviceconsumer:

Figure-40: Web service consumer configuration

Above figure shows the change required to access a web service enabled with usernametoken using

web service consumer.

Page 105: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

105

105 Java Custom Components

Java Custom Components Java components are used write our custom code and custom transformation. java transformers

will be used when we cannot transform a message using available Mule transformers. In this example,

following components are explained.

• Java Component

• Java component with singleton

• Invoke Component

• Java Transformer

• Reflection Entry Point Resolver

• Annotated Entry Point Resolver

• Invoking a service using java component

Below is the main flow which exposes a HTTP service and refers to multiple sub flows one after

another to cover all above concepts.

Java Component:

Java component is used to refer a class which has complex code.

Example:

Below is sub flow named 'simple-java-component' which has Set Payload and java component.

'Set Payload' component sets payload as "Custom Payload".

Page 106: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

106

106 Java Custom Components

Java component is reffered to custom made class (UsingCallable) which implements Callable interface. This class is used to print current payload, size of inbound properties and size of invocation properties.

public class UsingCallable implements Callable {

@Override

public Object onCall(MuleEventContext eventContext) throws Exception {

MuleMessage message = eventContext.getMessage();

System.out.println("Payload: "+message.getPayloadAsString());

System.out.println("No of Inbound Properties : "+message.getInboundPropertyNames().size());

System.out.println("No of Variables : "+ message.getInvocationPropertyNames().size());

return null;

}

}

Above class implements Callable interface. it has onCall(MuleEventContext eventContext) overriden method. This method provides MuleEventContext . We can get all the data using MuleContext instace as shown in above code snippet.

Page 107: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

107

107 Java Custom Components

Java Component with Singleton:

The singleton object factory does not create a new instance for each request; instead, the

instance is shared.

Example:

Drag java component and double click on the java component to bring up properties.

Click "+" symbol next to the 'Object' field

Select “core:singleton-object” and click "next".

Click on "..." button and select class "com.techm.javacustomcomponents.UsingSingletonObject"

Page 108: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

108

108 Java Custom Components

Click on "Advanced" tab and create following three properties using "+" as shown below and click on Finish.

• name

• dept

• location

Page 109: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

109

109 Java Custom Components

Same properties with same names need to be created along with setters and getters in "UsingSingletonObject" class. So that, specified properties values in java component are assigned into java class properties. Below is the code to create map object with these three properties.

import java.util.HashMap;

import java.util.Map;

import org.mule.api.MuleEventContext;

import org.mule.api.lifecycle.Callable;

public class UsingSingletonObject implements Callable {

private String name;

private String dept;

private String location;

Page 110: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

110

110 Java Custom Components

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getDept() {

return dept;

}

public void setDept(String dept) {

this.dept = dept;

}

public String getLocation() {

return location;

}

public void setLocation(String location) {

this.location = location;

}

@Override

public Object onCall(MuleEventContext eventContext) throws Exception {

Map<String, Object> employee = new HashMap<String, Object>();

Page 111: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

111

111 Java Custom Components

employee.put("name", getName());

employee.put("department", getDept());

employee.put("location", getLocation());

return employee;

}

}

Invoke component:

Invoke component is used to invoke the method of a given object (bean). Below flow has 3 invoke components which refers to 3 different methods of a bean.

Code of Java class:

public class ForInvokeComponent {

public int addition(int a, int b) {

System.out.print("Addition: ");

System.out.println(a + b);

return a + b;

}

public int substract(int a, int b) {

System.out.print("Substraction: ");

System.out.println(a - b);

Page 112: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

112

112 Java Custom Components

return a - b;

}

public int multiply(int a, int b) {

System.out.print("Multiply: ");

System.out.println(a * b);

return a * b;

}

}

A bean needs to be created in global elements to use Invoke component. Create a bean which refers to a custom made java class in global elements. In "Global Elements" tab click on "Create" button.

Click on "..." symbol next to "Class" field to select a java custom made class. Provide some meaningful names in "ID" and "Name" fields. Click on OK button.

Page 113: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

113

113 Java Custom Components

Drag a invoke component and double click on the component to bring up the properties. Fill the required fields as shown below.

Page 114: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

114

114 Java Custom Components

Name Any meaningful name

Object Ref "ID" value of bean which is created earlier

Method Method name which needs to be invoked

Method Arguments Values of method arguments

In the same way two more invoke components are created for two methods (substract and multiply)

Reflection Entry Point Resolver:

Reflection Entry Point Resolver is used to determine the entry point of the component. Class method invocation happens based on the payload type as the argument using reflection. For multiple parameters, the payload should be an Array of objects. If the payload is other than Array type then the respective method which has same type of argument will be invoked. If the message payload is of type NullPayload the resolver will look for a no-argument method.

Example:

Sub flow:

Page 115: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

115

115 Java Custom Components

Below sub flow uses java components to implement Reflection Entry Point Resolver

Java class:

Below java class "EntryPointResolver" has three methods with different argument types.

public class EntryPointResolver {

public String multipleArgumentsMethod(String name, String dept){

return "::In multipleArgumentsMethod:: " + name + " - "+dept;

}

public String singleArgumentMethod(String name){

return "::In singleArgumentMethod:: "+name;

}

public String noArgumentsMethod(){

return "::In noArgumentsMethod::";

}

}

Multiple Arguments method:

Drag an expression component and write code to create Array (strArray) of two String objects. Store that Array object into payload.

Page 116: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

116

116 Java Custom Components

Drag a java component and refer to a class "EntryPointResolver".

Page 117: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

117

117 Java Custom Components

As the payload is Array of two objects after 'Expression' component, "multipleArgumentsMethod(String name, String dept)" method of "EntryPointResolver" Class gets invoked.

Single Argument method:

Drag a 'Set Payload' component and set value a string value as "RAM". Drag a java component and refer to a class "EntryPointResolver" as shown earlier.

As payload is String type after 'Set payload' component, "singleArgumentMethod(String name)" method of "EntryPointResolver" Class gets invoked.

No Arguments method:

Drag a 'Set Payload' component and set value a string value as "#[null]". So that payload becomes null. Drag a java component and refer to a class "EntryPointResolver" as shown earlier.

Page 118: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

118

118 Java Custom Components

As payload is null after 'Set payload' component, "noArgumentsMethod()" method of "EntryPointResolver" Class gets invoked.

Annotated Entry Point Resolver:

Annotated Entry Point Resolver is used to determine the entry point of the component using annotations.

Example:

In below flow 'Set Payload' component has been used to set a String as "RAM" and 'Property' component has been to create outbound property (dept = IT). A Java component is used to refer a java class "AnnotatedEntryPointerResolver".

Java class :

import java.util.Map;

Page 119: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

119

119 Java Custom Components

import org.mule.api.annotations.param.OutboundHeaders;

import org.mule.api.annotations.param.Payload;

public class AnnotatedEntryPointerResolver {

public String multipleArgumentsMethod(@Payload String name, @OutboundHeaders Map<String, Object> dept){

return "::In multipleParametersMethod:: " + name + " - " + dept.get("dept");

}

}

Above java class has a method multipleArgumentsMethod(@Payload String name, @OutboundHeaders Map<String, Object> dept) which has two arguments referred by annotations. Based on the annotation the respective value gets match to the argument.

For example, payload value matches to the argument 'name'.

@Payload String name

In the same way, all outbound properties match to the argument 'dept' which is type java.util.Map.

@OutboundHeaders Map<String, Object> dept

Invoking a service using java component:

In this example a java component is reffered to a class "HTTPInvocation" which invokes another HTTP service and returns the HTTP resoponse. Logger component is used to log the response.

Java class:

Page 120: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

120

120 Java Custom Components

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

public class HTTPInvocation {

public String invokeRestService(Object payload) {

String output = "";

try {

URL url = new URL(

"http://localhost:8081/javaCustomComponent/annotatedEntryPointResolver");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("GET");

conn.setRequestProperty("Accept", "application/json");

if (conn.getResponseCode() != 200) {

throw new RuntimeException("Failed : HTTP error code : "

+ conn.getResponseCode());

}

BufferedReader br = new BufferedReader(new InputStreamReader(

(conn.getInputStream())));

String line;

while ((line = br.readLine()) != null) {

Page 121: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

121

121 Java Custom Components

output += line;

}

conn.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return output;

}

}

Page 122: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

122

122 Mule Message Enricher

Mule Message Enricher

Enricher:

Enricher is used if the target system needs more information than the source system can

provide. It enriches the mule message by calling external system or do some transformation to

existing payload and save it into some scope of variable like session or outbound or invocation

and the transformation happened in en-richer scope doesn't affect the actual payload.

Set-property: Save some information extracted from payload or original payload to some

invocation or flow scope variable.

NOTE: Mule currently supports enrichment of flow variables and message headers only.

Example:

Consider a message from a source system contains a zip code but the target system needs the

two letter state. A message enricher can be used to lookup the state using the zip (postal code)

from an enrichment resource. The enricher calls out to the enrichment resource with the

current message (containing the zip code) then enriches the current message with the result.

Page 123: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

123

123 Mule Message Enricher

This is a very simple flow with one-way inbound and outbound endpoints, and which acts as

part of an order processing pipeline. This flow uses an enricher to add a state flow variable to

the current message with the state that the flow ref returns. The ‘target’ attribute defines how

Page 124: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

124

124 Mule Message Enricher

the current message is enriched using a MessageEnricher which uses the same syntax as

expression evaluators.

Description:

1. The http endpoint receives an xml input as a payload with H-No, street, city and zip elements.

2. In message enricher we modified the payload as zip and forwarded the same to sub flow to

retrieve the state for that particular zip.

3. The flow reference in the processor chain of the enricher receives the state as a payload

which enricher assigns to a new target flow variable named state.

4. The payload sent from the enricher is the same as the input payload and the new state

variable is added to the xml using Data-Mapper.

Output:

Page 125: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

125

125 Mule Message Enricher

More Complex Enrichment:

The enricher element also supports more advanced use cases where the message returned by

the enrichment resource isn’t just a simple string which is exactly what we need to enrich the

current message with; often you may want to enrich your message with just part of the

information from the result of the invocation of an external service.

Page 126: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

126

126

In this particular example the ‘Get State’ endpoint receives the full message, and we are

supposed to use a part of that payload. Here we mention the part of the payload in the Source

section of the Message Enricher and that is saved in the target section.

The “enrichment resource” can be any message processor, outbound connector, processor-

chain or flow-ref. If using an outbound-connector then of course it should have a request-

response exchange pattern.

Expressions

Mule Expression Component:

This component evaluates an expression.

Page 127: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

127

127 Expressions

It accepts either a user-defined expression or a reference to a file.

Minimum requirement: The expression or file to evaluate.

Mule Expression Filter: Evaluates a range of expressions.

Minimum Configuration: The Evaluator and Expression properties are required.

Mule Expression Transformer:

The Expression Transformer executes one or more expressions on the current message.

The result of these expressions becomes the payload of the current message.

Page 128: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

128

128 Expressions

Minimum Configuration: An expression evaluator and the expression itself.

In the below figure, Check the Return source if Null box if you want the message payload source to be

returned without modification when all expressions evaluate to null.

Use the Advanced tab to optionally configure the return arguments.

Page 129: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

129

129 Expressions

For each return argument, you enter or select from the pull-down list its expression evaluator. Then

enter the expression to use. If you set Evaluator to custom, you also need to specify the custom

evaluator. If you are using a custom expression evaluator, you must first have registered the custom

evaluator with the Expression Evaluator Manager. Expression syntax varies depending on the evaluator.

When you have multiple expressions for return arguments, by default expression evaluation returns an

error and stops when an expression evaluates to null. Check the Optional box if you want expression

evaluation to continue to the next expression when an expression evaluates to null.

The evaluators should be of this enumeration '[attachment, attachments, attachments-list, bean,

endpoint, exception-type, function, groovy, header, headers, headers-list, json, json-node, jxpath, map-

payload, message, mule, ognl, payload, process, regex, string, variable, xpath, xpath2, xpath-node,

custom]'.

Example Flow:

Description:

1. Use http connector to trigger the flow.

2. Expression component to set the Employee Details as outbound properties.

3. Message Properties component to set a Session Variable.

4. Pass all the properties to another flow using a http outbound end point and add session properties to

the header as session expires after every flow.

Page 130: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

130

130

5. The data received will be of Byte Array Stream so use an Object to String Transformer.

6. Check the attached Session variable using the "#[message]" MEL in Logger component.

7. Get all the details from the inbound properties and use a map object to set-payload. In a similar

fashion List can also be used.

8. Transform the payload type from Object to String.

9. Evaluate if the payload type is of String or not using Expression Filter. If the payload is of type String

the flow execution forwards.

10. Use choice router to check for a specific text in the payload and print his Server IP using Mule

Expression Transformer.

11. Refer the ExpressionExample.zip for the example flow and SOAP UI test xml.

Page 131: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

131

131 Properties

Properties

A properties file is a simple collection of key-value pairs that can be parsed by

the java.util.Properties class. They are often used to store configuration or localization data. In

mule properties file can be configured using property placeholders and system properties.

Property Placeholders:

Property placeholders allow you to upload the parameters from a properties file. This enables

you, for example, to have different property files for different environments (Dev, QA, and

Prod) or allows you to reuse the same value in different parts of your configuration.

A very simple example shows how to use the property placeholders.

The values for these placeholders can be made available in a variety of ways, as described in the

sections below.

Global Properties:

You can use the <global-property> element to set a placeholder value from within your Mule

configuration, such as from within another Mule configuration file:

They can be configured as below:

Page 132: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

132

132 Properties

Properties Files:

To load the properties from a file, you can use the standard spring element

<context: property-placeholder>.

Page 133: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

133

133 Properties

Where the contents of the test.properties is:

To load multiple properties files, separate them with commas:

System Properties:

The placeholder value can come from a JDK system property. If you start Mule from the

command line, you would specify the properties as follows:

Page 134: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

134

134 Properties

Page 135: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

135

135 Properties

Environment Variables:

There is no standard way in Java to access environment variables. But the setting of environment

variables can be done in the run configurations window…choose Environment tab.

Mule-app.properties:

The property can be configured in mule-project.xml as below:

Add an environment variable by pressing the button:

Page 136: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

136

136 Properties

Example:

The example above tries to display the property name which is a common property from

various sources the observation is as below:

Observation:

The property in the mule-app.properties is prioritized the most, Global variables is prioritized

the next most and next is the run time arguments followed by Environment Variables and then

follows the property files in alphabetical order.

Page 137: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

137

137 REST

REST

Creating a REST Service using REST Component

Use this component to publish a RESTful Web Service. A REST component publishes a RESTful web

service via JAX-RS annotations and using Jersey. Mule hosts RESTful web services using Jersey, which is a

JAX-RS implementation. JAX-RS is a specification that provides a series of annotations and classes that

make it possible to build RESTful services.

Figure-22: REST component available in pallette

Figure-23 shows the REST Service flow creating using REST component.

Figure-23: REST service Flow

Page 138: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

138

138 REST

Figure-24 shows the REST component configuration. Component is the required element, which is java

class with JAX-RS annotations.

Figure-24: REST component configuration

Figure-25 shows the java class annotated with JAX-RS annotations @Path, @GET,@Produces,

@Consumes..

Figure-25: Java class with JAX-RS annotations

@Path- Identifies the URI path that a resource class or class method will serve requests for.

If the above application is deployed on http://localhost:9876, GET request for

http://localhost:9876/user will invoke getUserDetails method. Method getUserDetails has one

parameter userid which is a query parameter as it is annotated with @QueryParam. To access this

method we need to invoke using http://localhost:9876/user?userid=somevalue .

@GET- Indicates that the annotated method responds to HTTP GET requests

Page 139: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

139

139 REST

@Produces-Defines the media type(s) that the methods of a resource class can produce.

@Consumes-Defines the media types that the methods of a resource class can accept.

Figure-26 shows the configuration XML for the flow shown in Figure-23.

Figure-26: Configuration XML

Figure-27 shows the request and response for the REST service created when accessed using SOAPUI.

Figure-27: Request and Response

Page 140: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

140

140 REST

REST Service Consumer

Figure-28 shows the flow diagram to consume the REST service created (covered in “Creating a REST

Service using REST Component” section). Like other flows we have seen so far, this flow also will have

HTTP Listener as its message source. The connector configuration is similar to the one discussed in HTTP

Listener.

Second component is CXF component. This is optional if we do not want to expose a WSDL or do not

want to access the service in SOAP style.

Figure-28: Consumer for REST Service

Figure-29 shows the CXF configuration.

General – The operation element lets us choose from a list of options how we want to publish a service

or consume a service. Proxy service is one of the available options from the list, which lets us directly

send and receive XML data. To work with this option, few attributes (Port, Service, namespace) need to

be supplied. Values for this attributes can be found in the WSDL we supply to this configuration.

Payload, which is available for proxy-service lets us choose either body or envelope. CXF proxies

support working with the SOAP body or the entire SOAP envelope. By default only the SOAP body is sent

as payload, but the payload mode can be set via the "payload" attribute to envelope if needed.

Advanced- location of the WSDL needs to be provided.

Page 141: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

141

141 REST

Figure-29: CXF configuration General and Advanced tabs

Figure-30 shows the Choice block which helps in routing to a particular flow based on the result of

condition under test. In this example, we’ll use SOAPAction to identify a particular operation from the

service we have published. Choice router will route to a particular flow based on the incoming

SOAPAction.

Figure-30: Choice router

Page 142: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

142

142 REST

Figure-31 shows the getuser flow shown in Figure-28(highlighted in red). Three variables and one

property are set in the flow shown below.

Set UserId – sets the value of userId coming from the request into a variable.

#[xpath3('//user:userDeailsRequest/userId')]

Set Path – sets the URI which we want to invoke. This is same as defined in @Path(“uri”). For example,

if there are 2 resources (user, users) published on the same URL (http://localhost:8088), we can access

user using http://localhost:8088/user and users by http://localhost:8088/users. This path variable we

are setting here will have the path i.e. user, if we want to access user.

Set Operation- sets the HTTP method using which we want to invoke the service. The service should

support this operation. In our case we are invoking GET method (as shown in Figure-25).

Set Content Type-sets the Content-type property which is accepted by the method we are invoking. In

our example, getUserDetails method will accept either XML or JSON. So, if we have to send a content-

type, it should be one of them.

Figure-31: getUser flow

Figure-32 shows the REST service invocation using HTTP Request and values for few attributes are

dynamically set as shown in Figure-30.

Figure-32: Service invocation

Page 143: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

143

143 REST

Figure-33 shows the HTTP Request configuration. Connector Configuration for this similar to the one

shown in HTTP Request.

Values for Path and Method are set dynamically in the flow as shown in Figure-31. As shown in

Figure-25 getUserDetails method expects a QueryParam i.e. userid. Using HTTP request we can provide

the same using query-param option as shown in the below figure. Content-type header can be sent

using header option as shown in below figure.

Figure-33: HTTP Request Configuration

Figure-34 shows the configuration XML for the REST service consumer flow.

Figure-34: configuration XML

Page 144: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

144

144 REST

Figure-35 shows the request and response of the Rest service consumer.

Figure-35: Request and Response

Page 145: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

145

145 Transactions

Transactions

A transaction is an operation which must succeed or fail as a complete unit; it can never be only partially

complete. Mule applies transactions to a series of steps in a flow must succeed or fail as one unit. We

can apply transaction to a connector to enable using transactions. If a flow begins with a transaction

supported connector, mule can start a new transaction and manage entire flow as a transaction. If we

use a transactional outbound connector mule manages that outgoing operation also as part of

transaction. With both a transactional inbound and outbound connector, Mule executes the outgoing

operation as part of the transaction initiated by the inbound connector.

The following connectors in Mule support transactions:

1. JMS

2. VM

3. Database Connector

A Mule flow may begin with a non-transactional inbound connector – such as HTTP or SFTP. In such

situations, we can use Mule’s Transactional scope to combine the processors and put as one

transactional unit, so that all get succeed or failed as one unit. If any flow is beginning with any of the

connectors which support transaction, entire flow will be considered transactional including

transactional outbound connectors.

Mule supports three different types of transactions Single resource, Multiple resource, XA. In mule,

transactions can be configured either by applying transaction to a transaction supported endpoint or

wrapping message processors in mule provided transactional scope.

Each of these transactions has an action attribute that needs to be specified to work with transactions.

These actions include ALWAYS_BEGIN, ALWAYS_JOIN, BEGIN_OR_JOIN, JOIN_IF_POSSIBLE, NONE,

NOT_SUPPORTED.

ALWAYS_BEGIN - will begin a new transaction for every request.

ALWAYS_JOIN- will always join an ongoing transaction, throws an error if there is no transaction is in

progress.

BEGIN_OR_JOIN- will join if it finds any ongoing transaction, begin a new transaction otherwise.

JOIN_IF_POSSIBLE-will join an ongoing transaction if it finds any.

NONE- operates as non-transactional.

NOT_SUPPORTED - execute outside any transaction.

Page 146: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

146

146 Transactions

We can configure an exception strategy to the transactional scope. With the help of this transactional

scope specific error handling we can manage transactional exception. If we have a flow level exception

strategy, transactional exception strategy is optional as the flow level can handle all the exceptions

thrown while executing flow. If there is no exception strategy configured, mule uses default exception

strategy.

Figure-87 shows the transactional scope in mule.

Figure-87: transactional scope

Figure-88 shows a flow configuration for transaction. In this example configuration, we’ll see how

transactional block helps in maintaining the database state. To demonstrate how transactional block

works, we take a shopping cart example. We receive a request which has details of what items have

been added to the cart, the quantity of each item in the cart, total price for all items, and the account

number and account holder name.

Figure-88: Flow configuration for Transaction

Page 147: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

147

147 Transactions

In our example flow, once we receive the request for billing, we’ll see

1. If there are enough items available.

2. Enough amounts are available in the account.

If both the conditions are met, we’ll update the database tables according to the request we have

received.

If any of the conditions is not met database tables will not updated and corresponding error message

will be sent back to the user(or invoking service) stating the reason for failure.

Figure-89 shows part of the transactional flow configuration shown in Figure-88. In this flow, we’ll

retrieve the details required for processing the request such as user id, account number and the billing

amount. This is done in a sub-flow.

Figure-89: Parsing request and fetching the required data for processing

FetchItems expression-component highlighted in red is used to fetch the item details (item id, quantity

requested for) from the request and create a collection. The created collection is given as input payload

to the next processor (for-each inside transactional block) in the flow. For-each accepts a collection and

iterates over the elements in the collection.

Figure-90 shows the sub-flow to process the request and fetch the userid, account number and billing

amount.

Figure-90: Sub-flow to retrieve account details

Page 148: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

148

148 Transactions

Figure-91 shows the For-each scope (highlighted in red). Inside the “for-each” scope, we have Database

connector, configuration for this is similar to the one shown in

Database section. Using this database connector, we are calling a stored procedure to check the

quantity of items and update table as per the quantity in payload.

Figure-91: for-each scope in transactional block

Figure-92 shows the Database configuration for the one highlighted in Figure-91.

Figure-92: Database configuration.

Page 149: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

149

149 Transactions

Figure-93 shows the update_shopping_items stored procedure. This procedure gets called from the

database connector.

Figure-93: update_shopping_items stored procedure code

Figure-94 shows the flow to verify Account details of user. Call the sub-flow to set properties required to

process account information.

Figure-94: Verifying Account Details

Page 150: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

150

150 Transactions

Figure-95 shows the properties set in the sub-flow (highlighted in green in figure-94)

Figure-95: Properties set in the sub-flow

Figure-96 shows the Accounts details flow called using VM (highlighted in red) in figure-94. In the flow

shown below, account details gets verified and updated.

Figure-96: Account Details Flow

Page 151: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

151

151 Transactions

Figure-97 shows database connector to call update_account stored procedure.

Figure-97: update_account stored procedure

Figure-98 shows the update_account stored procedure to verify and update the account details.

Figure-98: update_account stored procedure

Page 152: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

152

152 Transactions

Figure-99 shows the database connector to update transaction reference number and status of the

transaction. The choice block at the beginning of the flow is used to route to one of the flows based on

the message received from Account Details flow shown in Figure-96.

If we receive a success response from account details flow, we’ll update the transaction status and

transaction reference in userinfo table. If the response we received from Account Details is a failure

response, we’ll just show the error message we received from Account Details flow.

Figure-99: update transaction status and reference number

Figure-100 shows the Database connector (highlighted in red in Figure-99) to update transaction

reference number and transaction status.

Figure-100: Update transaction ref and transaction status

Page 153: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

153

153 Data Source Configuration via Spring Bean

Figure-101 shows sample request and response for the transaction flow.

Figure-101: Request and Response

Data Source Configuration via Spring Bean

Figure-102 shows the flow configuration to configure Data source using spring bean. There is no change

in the flow, except the data source configuration using spring bean. Data source created using spring

bean (highlighted in red) is shown in the below figure. The spring bean created is supplied as the

datasource-ref to the Database connector configuration (highlighted in green)

Figure-102: Configuration XML

Page 154: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

154

154 Cache scope

Cache scope

The Cache Scope is used to store frequently called data thus saves time and processing load. We can

configure the caching strategy to store the responses and this cache scope can have any message

processors to process request. The responses contain payload of the response message produced by the

processing that occurs within the scope. We can configure caching strategy to let mule know how to

store data. If we do not specify any, mule uses default caching strategy.

When a mule message reaches cache scope, cache scope process the message and the sends the output

to the next processor and saves the output. Next time, when mule sends same kind of message into

cache scope, the cache scope offers a cached response rather than processing the message again. If

mule cache scope finds a match for the incoming request it is a “hit”. If mule does not find any match in

cache scope it is a “miss”. If mule finds a matching in cache block, the processors in cache block will not

be executed and the cached response will be sent as output. If mule does not find any matching in cache

block, the message processors placed in cache block will get executed and the response will be sent as

output put the next processing element in the flow and the response is cached.

By default, Mule stores all cached responses in an InMemoryObjectStore. If we want to provide our

own custom store, we can do so using the custom-object-store option. There are 4 ways how mule

stores cached responses.

1. In Memory object store

2. Custom Object store

3. Managed Store

4. Simple text file store

We can provide some options regarding the cache update while configuring the object store.

Below are some of the attributes we can include in object store configuration.

maxEntries – maximum number of entries that our object store can cache. If this limit exceeds, first

cached ones will be trimmed.

entryTTL - is number of milliseconds that a cached response has to live before it is trimmed.

expirationInterval - the frequency with which the object store checks for cached response events it

should trim.

Page 155: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

155

155 Cache scope

Using Default Cache

Figure-103 shows flow configuration to configure cache block with default caching. HTTP and Database

connector configurations are similar to the ones shown in previous sections.

Figure-103: Cache scope flow

Figure-104 shows cache connector configuration, we can use Default caching strategy or we can create

new caching strategy using the options provided. Click “+” (highlighted in red) to create a new reference

strategy. Using the filter configuration (highlighted in green), we can filter the incoming messages, to

filter incoming message we need to provide an expression. So that message satisfying filter expression

will get cached. Message will be processed by the message processors inside cache block, but cache

block never store the response if the message does not satisfy the filter expression.

Figure-104: Cache connector configuration

Page 156: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

156

156 Cache scope

Figure-105 shows the caching strategy configuration as shown in figure-104 (highlighted in red). We can

provide a key to store response, we can use Default key to store response in object store. Else, we can

generate a key using the Key Expression or Key Generator. In this example, we have used Key

Expression to store response, once this expression is evaluated, the result will be used as key to store

response.

Figure-105: Caching strategy configuration

Figure-106 shows Configuration XML for caching

Figure-106: Flow Configuration XML

Page 157: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

157

157 Cache scope

Figure-107 shows request and response for the cache flow.

Figure-107: Request and Response

Figure-108 shows the console output for the caching. For the example shown, time to clear the object

store is set as 3sec. In below console output we can see the service was invoked 3 times, second time

when the service was invoked mule has sent the cached response (highlighted in red) instead of fetching

from database.

Figure-108: Console output

Page 158: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

158

158 Cache scope

Using custom caching

Figure-109 shows configuration XML too use cache with Custom object store. Flow is similar to the one

shown in Default caching. Instead of default object store here we have used custom-object-store.

Figure-109: Configuration XML

Figure-110 shows sample request and response for the custom cache.

Figure-110: Custom store configuration

Page 159: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

159

159 Instructions to set up projects

Figure-111 shows sample request and response for the custom cache flow.

Figure-111: Request and Response

Figure-112 shows the console output for the above service invocation. Console output (highlighted in

red) shows the response is returned from the cache.

Figure-112: Console output

Instructions to set up projects All projects are Mule deployable archives; these can be imported to Anypoint Studio. All projects are

developed using Anypoint Studio March 2015 Release Version: 5.1.0. All SOAPUI projects can be found

in src/test/resources/soapui folder of each project. Please run all the database scripts.

All database scripts are placed as .sql files src/test/resources/ databasescripts folder in

Mule_Certification_guide_examples.zip file.

Database used: Postgresql version:1.20.0(Mar 25 2015)

SOAPUI version: 5.1.3

Page 160: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

160

160 Batch Processing

Batch Processing Batch component is used to process huge messages in batches. In batch we have 3 phases.

1. Input

2. Process Records

3. On complete

Input

Input phase is used to prepare a collection object with input message. Because process records

phase expects a collection object.

Process Records

Process Record phase expects a collection object to process the each record of collection in

individually and parallel. Here each object of collection is a record.

On Complete

On complete phase is used to summarize the flow. Following variables are available in On

Complete phase to get the status of flow.

Count of Successful records

Count of Failure records

Count of total records.

Example

In the following example, it explains how to transform CSV to XML using batch. This example

exposes a HTTP rest service.

In the main flow input csv file path sets to payload and refer to a batch job.

Page 161: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

161

161 Batch Processing

In Input phase, an expression component is used to read file (src/main/resources/input.csv)

data. This expression component refers to a file (src/main/resources/ReadFile.txt) which has code to

read file data. Another expression component has below expression to create a collection object.

payload = new com.techm.common.util.LineIterator(payload);

LineIterator is our custom class which implements Iterator Interface to implement

collection object. Below is code of LineIterator class.

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class LineIterator implements Iterator<List<String>>{ private BufferedReader reader; private boolean done = false; private String line; public LineIterator(InputStream in) throws UnsupportedEncodingException { reader = new BufferedReader(new InputStreamReader(in,"UTF8")); } @Override public boolean hasNext() { if(done) try { reader.close(); } catch (IOException e) { throw new RuntimeException(e); } return !done; } @Override public List<String> next() { try {

Page 162: Contents - gdssrao.files.wordpress.com · Scatter Gather is used to send a message to multiple endpoints concurrently. It collects the It collects the response of all the routes and

162

162 Batch Processing

List<String> buffer = new LinkedList<String>(); if (line == null) { line = reader.readLine(); } buffer.add(line); line = reader.readLine(); if (line == null) { done = true; } return buffer; } catch (IOException e) { throw new RuntimeException(e); } } @Override public void remove() { // no-op } }

In Process Records phase, we have two batch steps to transform payload from csv to xml using

datamapper and write the xml data into a file. Second batch step contains batch commit. The message

processors which are in batch commit scope get execute depends of size of batch commit.

<batch:commit size="5" doc:name="Batch Commit">

This batch commit get executed for every 5 records as configured.

On Complete phase has one logger component which logs successful, failure and total no of

records.

Note : SOAP UI project to test this application is placed in src/test/resources.