Mule ESB – Getting Started

Filed Under: Java EE

What is Mule ESB?

Mule, the runtime platform and a nimble Java-based enterprise service (ESB) which allows developers to easily build high-performance, secure, multi-protocol interactions between heterogeneous systems and services. It enables easy integration of existing systems, regardless of the different technologies that the applications use. The exchange of data between applications is carried by different messaging format.

Following are the powerful capabilities of Mule ESB:

  • Data Transformation
  • Service Mediation
  • Service creation and hosting
  • Message Routing

Advantages of Mule ESB

  • Lightweight but scalable, permitting you to start small and connect more applications in future.
  • Well suited to the scenarios where loose coupling, robustness, and scalability required.
  • It has support for more than 30 protocols and technologies.
  • Mule platform encourages component reuse profusely; no need to run or add Mule specific code in any of the component. Also, the business logic is separated from messaging logic.
  • Mule does not force any design constraints while choosing messaging format. It varies from SOAP to Binary image files.

Mule ESB Architecture

Mule ESB Architecture

Mule ESB Architecture

Mule Message Structure

It is the data format which is internally used by Mule to communicate between different applications via one or more flows.

Mule Message Structure

Mule Message Structure

Primarily it consists of two main parts:

  1. Header — which is nothing but the metadata of message.
  2. Payload — which is the actual business message.

Message Object is used to envelop the Mule Message along with some extra attributes like Attachment, Variable, and Exception Payload — which may not appear in every case.

The metadata is represented by properties— which provides useful information in order to facilitate processing and avoid errors when the message enters new flow or being transmitted to another application. Properties and Variables, contained within Mule Object, keep data in key-value format.

Properties have two main scopes:

  • Inbound Properties:
    1. These are immutable, automatically generated by the message source.
    2. They deter scrambling of data formats and any other manipulation in message lifecycle.
    3. Inbound properties’ scope is limited to only a flow; once message whisks to another flow, its inbound properties are disappeared.
    4. Mule Expression Language (MEL) Example: #[message.outboundProperties]
  • Outbound Properties:
    1. These are mutable and can be set by Mule or a user can alter them by using transformer elements in the flow.
    2. They can become inbound properties when message passed outbound endpoint of one flow to inbound endpoint of another flow.
    3. In one case—if message is passed to any flow-ref, then the outbound properties remain same as outbound properties.
    4. MEL Example: #[message.inboundProperties]

Variables have three scopes:

  1. Flow Variables— can be accessed within the same flow.
  2. Session Variables— apply across the flows within the same application.
  3. Record Variables— apply to only to records processed under batch

Message Payload contains the data your Mule application processes. Payload changes during its journey through different flow. It is set, enriched, or transformed into the new format by various of a Message processor in a Mule flow.

MEL Example to extract payload information: #[payload]

Mule Runtime Setup

Anypoint Studio Hello World Project

Let’s construct an application step by step which returns “Hello Mule” message to the browser when you hit the service.

  1. Launch the Anypoint Studio platform by navigating to a directory on your local machine where you installed.
  2. Create new Mule project, either by hitting “Create Project” link appeared on the Welcome screen or by navigating to File > New > Mule Project.
  3. Enter the name of your project in “Project Name” section.
  4. Anypoint Studio Create New Project

    Anypoint Studio Create New Project

  5. Click on Finish, and you will see Studio Canvas screen as following:
  6. Anypoint Studio Hello World Project Structure

    Anypoint Studio Hello World Project Structure

  7. At the bottom of canvas there are three tabs—Message Flow, Configuration XML and Global Elements. Message Flow and Configuration XML are the two tabs to show or edit Mule flow in visual and xml format, respectively. Global Elements tab exudes properties for configuration elements having scope global.
  8. Select a HTTP connector from Mule Palette and drag it into canvas.
  9. Anypoint Studio Hello Mule HTTP Connector

    Anypoint Studio Hello Mule HTTP Connector

  10. Go to the HTTP Listener configuration panel – clicking on green plus symbol under General settings in HTTP Connector – and enter Host and Port values.
  11. Mule ESB HTTP Connector Settings

    Mule ESB HTTP Connector Settings

    Mule ESB HTTP Listener Configurations

    Mule ESB HTTP Listener Configurations

  12. Search for the Set Payload and drag the transformer to the flow.
  13. Mule Message Set Payload

    Mule Message Set Payload

  14. Click on Set Payload transformer and set the Value field as “Hello Mule”.
  15. Mule Message Payload Value

    Mule Message Payload Value

  16. Drag the Logger and set the payload value received from Set Payload transformer into Message.
  17. Anypoint Studio Logger

    Anypoint Studio Logger

  18. After connecting all the Mule components, you flow, in XML format, looks like this:
    
    <flow name="hellomuleexampleFlow">  
        <http:listener config-ref="HTTP_Listener_Configuration" path="/message"  doc:name="HTTP"/>  
        <set-payload value="Hello Mule" doc:name="Set Payload"/>  
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>  		
    </flow>
    
  19. To test your application — run it as Mule Application. You can see console logs as below to verify the application is deployed and started successfully.
  20. Anypoint Studio Run As Mule Application

    Anypoint Studio Run As Mule Application

  21. Fire up the browser and hit the address http://localhost:8081/message. You can see response as “Hello Mule”.
  22. Mule Hello World Web Application

    Mule Hello World Web Application

You can find the complete code on our GitHub Repository.

Comments

  1. velama says:

    Thanks for such a valuable informative post on Mulesoft. By going through it most of my ongoing doubts regarding Mulesoft have been cleared. Looking forward for more of such informative posts.

  2. Ravindar says:

    Hi Pankaj,

    Can you please add some more topis and examples here. Waiting for some more examples like cloud hub and batch processing.

    Thanks,
    Ravindar.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages