3. About this course...
» Goals:
» Provide basic knowledge and understanding of Mule ESB
» Give some practical experience on running Mule and developing components
» Who can benefit:
» Developers of integration solutions that involve Mule
» Prerequisites :
» Basic knowledge of Java (SE), Spring, and XML
» Last minute notes:
» This course is targeted at Mule 2.2.x only
3 Introduction to Mule 2
4. About Mule…
» Project was started in 2003 by Ross Mason
» Transferred to MuleSource in 2006, changed name to MuleSoft as of 2009
» Both freely (Mule CE) and commercially (Mule EE) available
» Latest version is 2.2.1 (as of 25-03-2009)
» Used by ao. Walmart, Nespresso, JP Morgan, Deutsche Bank
» Even Atos Origin is involved:
4 Introduction to Mule 2
5. What is Mule ?
» Lightweight integration platform and service container with support for routing,
transformation and validation of messages
» Support for many different protocol and message formats
» Can easily be integrated in (web) applications and application servers, but can
be used "stand alone" just as well
» Based on the ESB architecture and EIP patterns (by Hohpe and Woolf)
» Or for short: an Enterprise Service Bus
» Mule is all about connecting applications
5 Introduction to Mule 2
6. Mule Community vs Enterprise Edition
» Main differences are :
» Support
» Management tooling
» High Performance
» Community version is free/open
source
» Enterprise Edition can be
downloaded as 30-day trial
6 Introduction to Mule 2
7. Information (1)
» Information, documentation, whitepapers etc. on http://www.mulesoft.org
» Note: registration is required for certain content, but free.
» Documentation is available on the course CD
» Other information :
» "Open Source ESBs in Action" by Jos Dirksen & Tijs Rademakers
» http://www.manning.com/rademakers/
7 Introduction to Mule 2
8. Information (2)
» For your convenience :
» User Guide:
- http://www.mulesoft.org/display/MULE2USER/Home
» API documentation :
- http://www.mulesoft.org/docs/site/current2/apidocs/
» Examples :
- http://www.mulesoft.org/display/MULE2INTRO/Examples
» Blog "From the Mule's mouth"
- http://blogs.mulesoft.org/
» Free online course: Mule ESB Fundamentals
- http://www.mulesoft.com/virtual-course-mule-esb-fundamentals
» Mule ESB & Integration resources
- http://www.mulesoft.com/esb-integration-resources
Note:
There are two Mule sites: mulesoft.org and mulesoft.com
8 Introduction to Mule 2
9. Getting started (1)
» Running Mule stand-alone :
» Download and install Mule
- http://www.mulesoft.org/display/MULE/Download
» Set environment variable %MULE_HOME%
» Start > Settings > Control Panel > System > Advanced > Environment
Variables
» Run Mule from command prompt
- cd %MULE_HOME%bin
- mule.bat
» See what happens…
» Now try to run an example
» cd %MULE_HOME%examplesecho
» echo.bat
» Select option "1" Tip:
Always include a configuration for running Mule
9 Introduction to Mule 2
10. Getting started (2)
» Running Mule from Eclipse :
1. Install Eclipse and Mule IDE plugin
- Use Eclipse 3.4 or 3.5
- http://www.eclipse.org/downloads/
- Install Mule IDE 2.0 plugin through update site
- http://dist.muleforge.org/mule-ide/updates/3.4/
2. Create a Mule application
- In Eclipse, choose File | New | Project and select Mule Project
- Enter a name
- Click Add sample project content and select the Hello example
3. Run the application
- In the Package Explorer, expand the conf directory, and right-click the hello-
config.xml configuration file
- Choose Run As > Mule Server
10 Introduction to Mule 2
11. Getting started (3)
» Time to get your hands dirty!
» Take a look at assignments 1 and 2 at the end of this presentation
» Assignment 1 can be found here
» Assignment 2 can be found here
11 Introduction to Mule 2
13. Mule messaging architecture (1)
» The messaging architecture is the core of the Mule platform
» The messaging architecture is based on ESB principles
» The general Mule architecture has been designed according to the principles of
Hohpe and Woolf’s book "Enterprise Integration Patterns"
» Example: Mule receives a message from an application, transforms it into a format
the receiving application understands, and sends the transformed message to the
application
13 Introduction to Mule 2
19. Endpoints (1)
» An endpoint description consists of :
» Endpoint URI
» Connector
» Filter
» Transaction
» Properties
19 Introduction to Mule 2
20. Endpoints (2)
» Endpoint scopes:
» Global
- Defined only once in mule-config.xml
- Can be used everywhere
» Local
- Defined per service
- Limited to that service description
20 Introduction to Mule 2
21. Endpoints (3)
» Notation style :
» All XML
» namespaces per transport
» Specific elements per transport
» XML notation :
» <[transport ns]:[inbound-endpoint|outbound-endpoint] [transport details] />
» Examples:
» <jms:inbound-endpoint queue="in.queue"/>
» <file:outbound-endpoint path="directory/temp"/>
» <file:inbound-endpoint path="C:/indir" transformer-ref="FileToString"/>
» <cxf:inbound-endpoint address="http://atosorigin.com/webservice"/>
» <vm:inbound-endpoint path="jvmqueue.in"/>
» <http:inbound-endpoint url="http://localhost:8080/MyServletApp/>
21 Introduction to Mule 2
24. Transports (1)
» Responsible for message traffic between source and target systems
» A transport consists of (a.o.) :
» Message receiver/dispatcher
» Connector
» Transformer
24 Introduction to Mule 2
25. Transports (2)
» Large number of available transports, such as :
AS400 JCR SMTPS FTP POP3S XMPP
Axis JDBC SOAP HTTP Quartz SMTP
BPM Jetty SSL HTTPS RMI IMAPS
CXF Jetty SSL STDIO IMAP Servlet VM
EJB JMS UDP File POP3 Email
» Check http://www.mulesoft.org/display/MULE2USER/Available+Transports for
a complete overview
» Check http://muleforge.org for community efforts on transformers and
components
25 Introduction to Mule 2
27. Services & Components (1)
» A service describes a complete end-to-end coupling
» Parts :
» Description : some user friendlyness
» Inbound : inbound router with endpoint(s) and transformer(s)
» Component : contains business logic of the service component
» Outbound : outbound router with endpoint(s) and transformer(s)
» Exception strategy: exception handling
» A service component is a Java class, (web)service or even an application that is
plugged into the ESB.
27 Introduction to Mule 2
28. Services & Components (2)
» A service component can be any type of object, such as Spring bean, POJO,
script, web service, or a REST call
» Writing custom components is rather easy
» Mule has a number of standard components :
28 Introduction to Mule 2
29. Services & Components (3)
» Other standard components :
» Note: a component may or may not return a message. For example, a log
component will not return anything.
29 Introduction to Mule 2
30. Services & Components (4)
» POJO example :
package com.atosorigin.muledemo.beans.MessageBean;
public class CustomLogComponent implements Serializable {
public void accept(String payload) {
System.out.println("Current payload: " +payload);
}
}
» Mule uses reflection to determine what method to invoke
» Often based on data type of payload
» No return value here…
30 Introduction to Mule 2
31. Services & Components (5)
» SpringBean example :
package com.atosorigin.muledemo.beans;
…
import java.io.Serializable;
<model name="FileTransfer">
<service name="FileTransferService">
public class MessageBean {
<inbound>
private String message;
<file:inbound-endpoint path="C:in" />
</inbound>
public String getMessage() {
<component>
return message;
<spring-object bean="messageBean">
}
</spring-object>
</component>
public void setMessage(String message) {
<outbound>
this.message = message;
<pass-through-router>
}
<file:outbound-endpoint path="C:out" />
</pass-through-router>
public String message(String msg) {
</outbound>
setMessage("Original content: " +msg);
</service>
return getMessage();
</model>
}
} …
» Remember, Mule uses reflection to determine what method to invoke
31 Introduction to Mule 2
32. Services & Components (6)
» Entry point resolving :
» A component is often is POJO or bean class with multiple methods
» Mule uses the following steps to determine which method to invoke on POJOs:
1. If a property with the name "method" is specified, the value of that property
is used to determine the method to invoke on your component.
2. When implementing the org.mule.api.lifecycle.Callable interface, Mule will
invoke the onCall() method of this interface.
3. If there is a transformer configured, Mule will use the return type of this
transformer to try and determine if there is a method which accepts this
type.
4. If there is still no unique match Mule will check the type of the payload to
see if that matches any of the methods in the bean
32 Introduction to Mule 2
34. Transformers
» Transformers are components for transforming one message format to another, or
for enriching message contents.
» Transformers are configured on endpoints
» Transformers are automatically applied
» It is possible to combine several transformers ("chaining")
34 Introduction to Mule 2
35. Internal transformation
» Mule applies implicit transformers for internal communication
» But always to or from JMS…
35 Introduction to Mule 2
36. Standard transformers
» Mule has several standard transformers, such as :
» FileToString, ObjectToString, StringToEmailMessage
» Other standard transformers include :
» XML
- XmlToObject / ObjectToXml
- DomToXml / XmlToDom
- JXPath
» Encryption / Decryption
» GZipCompress / GZipUncompress
» Base64Encoder / Base64Decoder
» StringToByteArray / ByteArrayToString
36 Introduction to Mule 2
37. Custom transformers
» Writing custom transformers is easy :
» Extend from
- org.mule.transformer.AbstractTransformer
» Implement
- protected Object doTransform(Object payload, String encoding) {}
» Define expected input and output data types in contructor
37 Introduction to Mule 2
38. Custom transformers
» Custom FileToString transformer, based on another transformer :
public class FileToString extends FileToByteArray {
public FileToString() {
//define accepted input type
registerSourceType(byte[].class);
//define return type
setReturnClass(String.class);
}
@Override
public Object doTransform(Object payload, String encoding) throws TransformerException {
byte[] bytes;
String output;
//call transform method from super class
bytes = (byte[]) super.doTransform(payload, encoding);
output = new String(bytes, encoding);
return output;
}
}
38 Introduction to Mule 2
39. Mule Data Integrator
» Mule Data Integrator allows you to define and execute data transformations (called
maps) between data records, documents, or other payload objects in a Mule
message
» Each map specifies how data fields in an input document are mapped to fields in
an output document
» such as mapping the Purchase Order Number field in the input document to the
PO# field in the output document
» More information on :
» http://www.mulesoft.org/display/MDI/Getting+Started+with+Mule+Data+Integrat
or
» Install (as Eclipse update site):
» http://www.mulesoft.com/eclipse-updates/mule-data-integrator/
39 Introduction to Mule 2
41. Filters
» Filters are conditions that a message (or its payload) has to meet in order to be
routed to a service
» Filters return a boolean to determine message passing
» Standard filters include :
» Payload Type Filter
» RegEx Filter
» Wildcard Filter
» XPath Filter
» Logic Filters
» Message Property Filter
» Once again, writing your own filters is not that hard..
41 Introduction to Mule 2
42. Payload type filter
» Allow only a certain type of message to be accepted
<!-- Globally defined filter with name attribute -->
<payload-type-filter name="payloadFilter" expectedType="java.lang.String">
<model>
<service>
<inbound>
<tcp:inbound-endpoint host="localhost" port="1234"/>
<filter ref="payloadFilter"/>
</inbound>
<echo-component/>
<service>
</model>
42 Introduction to Mule 2
43. RegEx type filter
» Accept only messages whose content meets an regular expression
…
<outbound>
<filtering-router>
<stdio:outbound-router system="OUT"/>
<not-filter>
<regex-filter pattern="(.*)bla(.*)"/>
</not-filter>
</filtering-router>
</outbound>
43 Introduction to Mule 2
44. JXpath filter
» Apply XPath expressions on XML Dom objects
<expression-filter
evaluator="jxpath"
expression="(msg/header/resultcode)='success'">
<msg>
<header>
<resultcode>success</resultcode>
</header>
<body>
…
</body>
</msg>
44 Introduction to Mule 2
45. OGNL filter
» Simple expressions using the OGNL language, applied on Java objects
» Example:
» expects an array whose first element has the value of 42
<expression-filter
evaluator="ognl"
expression="[MULE:0].equals(42)">
45 Introduction to Mule 2
46. Logic filter (1)
» Applies logical operators on multiple filters
» Example :
<filter className="org.mule.routing.filters.logic.AndFilter">
<left-filter
expectedType="java.lang.String"
className="org.mule.routing.filters.PayloadTypeFilter"/>
<right-filter
pattern="the quick brown (.*)"
className="org.mule.routing.filters.RegExFilter"/>
</filter>
46 Introduction to Mule 2
47. Logic filter (2)
» Or even easier :
<not-filter>
<filter ref="myCustomFilter"/>
</not-filter>
<and-filter>
<payload-type-filter expectedType="java.lang.String">
<filter ref="anotherCustomFilter"/>
</and-filter>
<or-filter>
<payload-type-filter expectedType="java.lang.String">
<payload-type-filter expectedType="java.lang.StringBuffer">
</or-filter>
47 Introduction to Mule 2
48. Writing custom filters
» Implement the "Filter" interface, which has a single method:
» public boolean accept(MuleMessage message);
» Configure your filter using the <custom-filter> tag in the Mule configuration
48 Introduction to Mule 2
50. Interceptors
» Interceptors allow developers to intercept processing on an object and potentially
alter the processing and outcome
» Mule has two kinds of interceptors :
» Envelope interceptor
- Will execute before and after the component is invoked
- Good for logging and profiling
» Interceptor
- Intercepts the message and then forwards it for processing to the next
element
- Can stop flow by not forwarding control to the next element
- Good for permission checking
» Interceptors can be bundled in interceptor-stacks
50 Introduction to Mule 2
53. Developing interceptors
» Custom message interceptors :
» Implement "Interceptor" interface, having an "intercept" method :
- MuleMessage intercept(Invocation invocation) throws MuleException;
» Must return a "MuleMessage" that will be passed to next component
» Custom envelope interceptors :
» Implement "EnvelopeInterceptor" interface, having two methods :
- MuleMessage before(Invocation invocation) throws MuleException;
- MuleMessage after(Invocation invocation) throws MuleException;
» Configure your custom interceptor using the <custom-interceptor> tag
53 Introduction to Mule 2
55. Routers
» Routers determine how and where messages will be sent or received
» Most important types of routers :
» Outbound router
- Where to send outgoing messages
» Inbound router
- Where to listen for incoming messages
» Declared in <inbound></inbound> and <outbound</outbound>
» Examples will follow…
55 Introduction to Mule 2
58. Configuration
» The Mule configuration consists of one or more XML files, usually having the
name "mule-config.xml"
» Structure of a configuration file :
Mule Configuration
Compare to : Transport
Schema Import Statements
Application
Spring Beans Definition Connector
Channel
Mule Connectors
Mule Filters Service
Mule Transformers
Inbound Outbound
Component
Router Router
Mule Global Endpoints
Mule Model
Mule Service
Transport
Inbound Router
Component Application
Connector
Outbound Router
Channel
58 Introduction to Mule 2
59. Examples (1)
» Basic example of a service component :
…
<service name="YourService">
<inbound>
<jms:inbound-endpoint queue="queue.in"/>
</inbound>
<component class="YourComponent"/>
<outbound>
<outbound-pass-through-router>
<jms:outbound-endpoint topic="topic.out"/>
</outbound-pass-through-router>
</outbound>
</service>
…
59 Introduction to Mule 2
63. Messaging Styles
» Messages can be sent
» Asynchronously
- Each stage of the message is on a different thread
» Synchronously
- After the message is received by the component, a single thread is used
throughout the rest of its lifecycle and supports request-response
» Types of message routing:
» Asynchronous
» Request/Response
» Synchronous
63 Introduction to Mule 2
64. Asynchronous (1)
» Description :
» Receives a message and puts it on a SEDA queue.
» The callee thread returns and the message is processed by the SEDA thread
pool
» Nothing gets returned from the result of the call
» Configuration :
» The Mule service must have an asynchronous inbound endpoint.
64 Introduction to Mule 2
66. Request/Response (1)
» Description:
» Receives a message and the component returns a message.
» If the component call returns null, then a MuleMessage with a NullPayload is
returned.
» If the call method is void the request message is returned.
» Configuration:
» The Mule service must have a synchronous inbound endpoint and no outbound
endpoint configured.
» Set an endpoint as synchronous using synchronous="true"
» Note: HTTP/S, SSL, TCP, and Servlet endpoints are synchronous by default
and do not require this setting
66 Introduction to Mule 2
68. Synchronous (1)
» Description
» Receives a message and the component processes before sending it out on
another endpoint.
» The request happens in the same thread.
» Mule blocks on the outbound endpoint to wait for a response from the remote
application (if applicable) until the responseTimeout threshold is reached
» If no response is received, it returns null
» The synchronous call must be used if transactions are being used on the
inbound endpoint
» A synchronous call always returns a result, even if there is an outbound
endpoint
» Configuration:
» The Mule service must have a synchronous inbound endpoint and an outbound
endpoint configured.
» Set an endpoint as synchronous using synchronous="true"
68 Introduction to Mule 2
69. Synchronous (2)
» Situation :
» Note :
» Setting the synchronous inbound endpoint on the first service means that the
message is treated synchronously throughout all subsequent services, so you
do not need to set the synchronous flag on the remaining services.
69 Introduction to Mule 2
72. Assignment 1: Set up your environment
» Requirements :
» Java SDK (1.5 or higher)
» Mule CE (2.2.1)
» Eclipse (3.4 or 3.5)
» MuleIDE plugin (http://dist.muleforge.org/mule-ide/updates/3.4/)
» Steps :
» Install or unpack the mentioned components (supplied on CD)
» Set JAVA_HOME and MULE_HOME environment variables
» Configure MuleIDE plugin (Window | Preferences | Mule)
» Try to start Mule from command line: %MULE_HOME%binmule.bat
- Note: when starting Mule for the first time, some terms must be accepted
- What happened? Is this expected behaviour?
72 Introduction to Mule 2
73. Assignment 2: Install & run Mule demo project
» Unpack Mule-demo.zip in your Eclipse workspace directory and import it
» File | Import… | Existing Projects into Workspace
» Unpack Mule-dirs.zip in C:Temp
» Create this directory if necessary
» Add Mule libraries to Eclipse project settings
» Tip: create a User Library and add jars from :
- %MULE_HOME%libboot
- %MULE_HOME%libmule
- %MULE_HOME%libopt
» Run the demo project using right-click | Run | Run As… | Mule server on a
configuration file
» These can be found in configmule-config_XXX.xml
» Demo project contains examples of transports, transformers, components etc.
73 Introduction to Mule 2
74. Assignment 3: Mule Basic Tutorial
» Please see files :
» Mule_basics_1.doc
» Mule_basics_2.doc
» Located on the CD in folder "Assignments"
» Execute all tasks in this document
74 Introduction to Mule 2
75. Assignment 4: Mule Case
» Please see file :
» Mule_advanced.doc
» Located on the CD in folder "Assignments"
» Execute all tasks in this document
75 Introduction to Mule 2