In this lesson the student will learn to build a hello world ESB Mule application. We will walk through the example in order to understand the basics of the Mule framework, how service components are built and how they communicate with each others. Finally, the student will develop and integrate a service component to the application.
Introduction to ESB
Installation and configuration of Mule Framework and Eclipse Plug-in
Creation of a ESB application
Overview of the XML configuration (schema,namespace,global connectors, model, services)
Inbound Routers
Components
1) Install Mule Community Edition
1.1) Download Mule ESB from:
Mule standalone version 3.1.1
Windows system: http://project-icebar.net/bpm/bpm2011/mule-standalone-3.1.1.zip
Linux/Unix systems: http://project-icebar.net/bpm/bpm2011/mule-standalone-3.1.1.tar.gz
1.2) Follow the instructions for installing/configuring Mule ESB Community standalone version and Mule IDE http://www.mulesoft.org/documentation/display/MULE3INTRO/Installing+Mule
For the JDK, you can find for some distributions in our local server at:
JDK Linux x64: http://project-icebar.net/bpm/bpm2011/jdk-6u24-linux-x64-rpm.bin
JDK Windows x64: http://project-icebar.net/bpm/bpm2011/jdk-6u24-windows-x64.exe
JDK Windows i586: http://project-icebar.net/bpm/bpm2011/jdk-6u24-windows-i586.exe
1.3) Import the following project into Eclipse: http://project-icebar.net/bpm/bpm2011/Lesson1.zip
File->Import->Existing Projects into Workspace
Select the folder Lesson1 (uncompress the zip first)
Finish
1.4) Make sure the java build path is correctly set
Right click on the project->Build Path->Configure Build Path
Check that Mule Libraries and JRE are pointing correctly, if not remove it and add the library again by
Add Library->JRE System Library or Add Library->Mule Classpath (select all transport)
1.5) Run the application
Right click on the config file->Run as Mule Server
Start the process by invoking a GET request with your browser to the following address http://localhost:8888?name=Diego
2) Walking through the Hello World Example
The example consist of two main flows/service components:
Greeter: receives the name of the person from HTTP request, append the word "hello" to the data and send it to the ChitChat service component.
ChitChat: receives the payload and append "How are you?" to the data and it deliver backs to the caller.
Creating a mule application consist basically on creating a configuration file that points to the java classes that implement any custom logic on the message that come through. Lets open the configuration file of the example (mule-config.xml) and walk through the configuration.
The configuration file consist of:
2.1) XML declaration
<?xml version="1.0" encoding="UTF-8"?>
2.2) Namespaces declaration
Each Mule transport or module has its own xml schema and namespace.
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.1/mule.xsd">
Obs: You can add new schemas automatically by creating a new Mule config file and select the component/modules you want to use :
Right Click on the project->New->Other->Mule->Mule Configuration and then select the modules or transport you want to work with.
2.3) Transformers
Each service component can expect as input different data types, and we can transform transparently the payload to the component's expected data type. To achieve this, Mule has defined the notion of transformers, which can be referenced before the message reach to the component (inbound-endpoint) or after being processed. Mule provides a set of standards transformer (http://www.mulesoft.org/documentation/display/MULE3USER/Using+Transformers#UsingTransformers-AvailableTransformers) out of the box, but in this example we will see custom implementation of transformers in order to learn how to implement our own transformers. Custom transformer must be declared outside the model element:
<custom-transformer name="NameStringToChatString" class="org.mule.example.hello.NameStringToChatString"/>
<custom-transformer name="NameStringToChatString" class="org.mule.example.hello.NameStringToChatString"/>
2.4) Flow
A flow is a chain of message processors. In the flow is defined the message source which is what starts the chain of processors. In the example is used the <composite-source> because it contains more than one possible inbound-endpoint to start the process. In the same way is defined the outbound-endpoint using the <choice> directive which sends the message to the first match.
More info: http://www.mulesoft.org/documentation/display/MULE3USER/Flows
Between the inbound and outbound the component is defined. The <component> element configures the service component that will be invoked after the inbound message is processed by the inbound routers. If no component is configured, the service acts as a bridge and simply passes messages through to the outbound router. The component contain the business logic for working with the messages passed through Mule ESB. This service component can be any type of object, including POJO, script, Spring bean, web service, or REST call. In the ChitChat service the component is a simple Java Object which implements the business logic. When no entry point is defined, Mule uses an entry point resolver to dynamically choose the method to invoke based on the payload type of the message.
<flow name="Hello World">
<composite-source>
<!-- Incoming HTTP requests -->
<inbound-endpoint address="http://localhost:8888" transformer-refs="HttpRequestToNameString" exchange-pattern="request-response">
<not-filter>
<wildcard-filter pattern="/favicon.ico"/>
</not-filter>
</inbound-endpoint>
<!-- Incoming VM requests -->
<vm:inbound-endpoint path="greeter" transformer-refs="StringToNameString" exchange-pattern="request-response"/>
</composite-source>
<component class="org.mule.example.hello.Greeter"/>
<choice>
<when expression="payload instanceof org.mule.example.hello.NameString" evaluator="groovy">
<vm:outbound-endpoint path="chitchatter" exchange-pattern="request-response"/>
</when>
<when expression="payload instanceof java.lang.Exception" evaluator="groovy">
<vm:outbound-endpoint path="userErrorHandler" exchange-pattern="request-response"/>
</when>
</choice>
<!-- Route unexpected errors to separate error handler -->
<default-exception-strategy>
<vm:outbound-endpoint path="systemErrorHandler" exchange-pattern="one-way"/>
</default-exception-strategy>
</flow>
2.5) Inbound
The inbound element is used to configure inbound endpoints and inbound routers. Endpoints are used to receive incoming messages, and inbound routers determine how these messages are routed. Inbound endpoints and routers are configured separately within the <inbound> element. In the ChitChat service is defined an inbound-endpoint which indicates which transport uses (vm), the path/address to receive messages from (path="chitchatter"), and the current transformer to apply before past the payload to the component. We can define different inbound-endpoint for each service component with different transport such as jms, http, axis, bpm, among others.
<inbound>
<vm:inbound-endpoint path="chitchatter" transformer-refs="NameStringToChatString"/>
</inbound>
More info: http://www.mulesoft.org/documentation/display/MULE3USER/Inbound+Routers
2.6) Outbound
The <outbound> element configures outbound routers and their endpoints. Because outbound routers are used to determine which endpoints to use for dispatching messages after being processed, outbound endpoints are configured on the outbound routers, not directly on the <outbound> element. Outbound routers allow the developer to define multiple routing constraints for any given message.
In the example of the Hello World flow, one of the outbound is used to send the message to the chitchatter flow.
<vm:outbound-endpoint path="chitchatter" exchange-pattern="request-response"/>
More info: http://www.mulesoft.org/documentation/display/MULE3USER/Outbound+Routers
Tasks
Add a new service/flow component called "ChitPresenter" to the model. The new service component will be listening the channel that the Greeter component uses to send its message. The new component will add a presentation phrase like "My name is Diego" to the message payload, and send it to the channel where the ChitChat component is listening to.
Add the possibility to start the process from the standard input (stdio) and also sends the processed message to the standard output.
For Homework
Change the expected data type of the service component you have already developed (if you have not already done) and build your own transformers.
Lab 2
Topics to be covered
Transformers
Filtering
External service integration (webservice)
Temperature converter - Scenario
A public webservice that provides temperature conversion is available. We have to define a bus that interacts with the service by carrying a message sent from the standard input. The message will pass to a filter and transformer before being sent to the webservice, and finally the result will be printed to the standard output.
The webservice description is available here: http://www.webservicex.net/ConvertTemperature.asmx?WSDL
The service expects 3 variables (as an array of objects) in which we send:
Degree
FromTemperatureUnit
ToTemperatureUnit
Possibles values for FromTemperatureUnit and ToTemperatureUnit are: degreeCelsius | degreeFahrenheit | degreeRankine | degreeReaumur | kelvin
Those variables should be send to the standard input separated by semi-colon. For example > 10;degreeCelsius;degreeFahrenheit
Then the message should be transformed to an array of objects which is the data type the webservice expects as parameter. The message should pass to a filter in order to make sure the person has typed in the correct format (semi-colon) before the message is delivered to the webservice.
Finally, the response should be delivered to the standard output with the corresponding conversion.
Hints:
- Use the following config for connecting to the webservice inside your flow or service:
<outbound-endpoint address="wsdl-cxf:http://www.webservicex.net/ConvertTemperature.asmx?WSDL&method=ConvertTemp"/>
Create a new application from scratch
File->New->Other->Mule->Mule Project (do not put white spaces in the name)
Right click on the project folder->New->Source Folder (Called conf)
Right click on the conf source folder->New->Other->Mule->Mule Configuration
Select the following modules: CXF, HTTP, Spring Config, System I/O, VM
Right click on the src folder->New->package (Name your package, for example lastname.local)
Useful Links
Transformers: http://www.mulesoft.org/documentation/display/MULE3USER/Using+Transformers
Filters: http://www.mulesoft.org/documentation/display/MULE3USER/Using+Filters
Webservices: http://www.mulesoft.org/documentation/display/MULE3USER/Using+Web+Services