Basic Studio Tutorial In Mule Studio
This exercise is designed to introduce you to Mule Studio and help you get comfortable building applications.
This tutorial walks you through how to use Mule Studio to build a simple HTTP request-response application. After creating and running the example locally, you will be able to apply what you have learned to create more complex applications.
Assumptions
This tutorial assumes that you have downloaded and installed Mule Studio. If you do not have any previous experience with Eclipse or an Eclipse-based IDE, please review the brief introduction to the Mule Studio interface.Goals
In this tutorial, your goals are to:- Create an application in Mule Studio containing a single flow that
- receives an HTTP request,
- logs the message payload, and
- returns a modified payload as an HTTP response.
- Run the application on a local runtime embedded in Mule Studio.
- Test the application using your browser.
- Stop the application.
- Edit the application and rerun it.
- (Optional) Apply your knowledge to an extra credit challenge.
Skip to the code
Launching Studio
If you have already launched Studio as part of the installation process, and it is now running, skip the rest of this section and proceed directly to Creating a New Project.- Navigate to the directory on your local drive in which you installed Studio.
- Double-click the executable file to launch Studio.
MuleStudio.exe
in WindowsMuleStudio.app
in Mac OSMuleStudio
in Linux
- When Studio presents the Workspace Launcher dialog, click OK to accept the default workspace.
- If this is the first time you have launched Studio, you may see a Welcome screen. Click Create a Project to dismiss the screen and jump straight to the New Mule Project wizard.
Creating a New Project
- Unless the New Project Wizard is already open, click File > New > Mule Project.
- Studio opens the New Mule Project wizard. Fill in the Name field with the value Basic Tutorial.
- Select the Server Runtime
to which you will want to deploy your project. Mule Studio
automatically populates the Server Runtime field with one of
the default servers that comes bundled with your download. You can
change to a different runtime if you wish, but this project will work
with any runtime. For more about installing additional runtime versions,
see Installing Extensions.
- Click Next to proceed.
- Studio prompts you to create a new flow in your new project. It auto-fills the flow Name field and flow File name field using the name of the Mule project you entered. Click Finish.
- In the Package Explorer,
Basic Tutorial.mflow
appears inside theflows
folder andmule-project.xml
appears underneath thesrc
folder.
The.mflow
file stores flow information for the visual editor. When your new project opens for the first time, Mule Studio automatically opens the.mflow file
and presents you with a blank canvas.
Modeling a Mule Flow
In this tutorial, your goal is to build an application that receives an HTTP request, logs the message payload, and then modifies the payload before returning it as an HTTP response. You can build this application using three building blocks in Studio:- an HTTP endpoint, which allows your Mule application to connect to Web resources through the HTTP or HTTPS protocol. Find this in the Endpoints section of the palette at the right of the screen.
- a Logger, which logs messages or activity based on the value of a Mule expression. Find this in the Components section of the palette at the right of the screen.
- a Set Payload Transformer, which modifies your payload into a "Hello, World" message. Find this in the Transformers section of the palette at the right of the screen.
Tip: Use the Palette Filter Note that building blocks in each category of the palette are organized alphabetically. To avoid scrolling, use the Filter tool in the upper right corner of the palette to find the building blocks that you want more quickly. |
Once you configure the individual elements within it, which you will do in the next section, this flow will accomplish the goals that you set out to achieve with this application. Each building block that you selected and placed on the canvas will perform part of the functionality of your application, as shown in the image below.
Configuring the Flow Elements
Nearly all Mule elements provide configuration options, which you can set in one of two ways:- via the building block Properties tabs in Studio's visual editor
- via XML code in Studio's XML editor
- Click the HTTP building block in your flow to open its properties editor. (Depending on your settings,
this may open in a pop-up or in the console.) By default, the endpoint
is set to the request-response exchange pattern. This means that Mule
will return a response to the endpoint after processing is complete in
the flow. By default, Host is set to
localhost
and Port to8081.
- Click Configuration XML at the bottom of the canvas to switch to the XML editor view.
Observe that the default configurations shown on the Message Flow canvas and the Configuration XML view are the same:
<
http:inbound-endpoint
exchange-pattern
=
"request-response"
host
=
"localhost"
port
=
"8081"
doc:name
=
"HTTP"
/>
doc:name
attribute corresponds to the display name that appears underneath the building block icon on the Message Flow canvas. - Still in the XML configuration view, change the value of the port attribute to
8084
. - Click Message Flow to switch back to the visual editor.
- Click the HTTP building block to reopen its properties tab, and note that the Port field now reflects the updated value of
8084
. You can change this value in either editor; Studio's two-way editor automatically updates the configuration as you switch back and forth. - Click the Logger building block (or press the right arrow on your keyboard) to switch the properties tab to the Logger component.
- In the Message field, type:
Current payload is #[message.payload]
.
The string#[message.payload]
is a simple Mule expression which evaluates to the current payload of the message as it passes this point in the flow. Including this message here instructs Mule to log this information in the application log files, which can be useful in more complex use cases, when you need to track the payload at different points in your flow. - Click the Set Payload building block (or press the right arrow on your keyboard) to switch the properties tab to the Set Payload transformer.
- Notice that the Value field contains these characters: #[]
This indicates that this field supports Mule expressions, which take the form of a string enclosed with "#[]". If you enter a Mule expression here, Mule evaluates it at runtime and returns or uses the results for further processing. Note that this field also accepts literals, so you can enter any string here to instruct Mule to set that string as your new payload. In this tutorial, however, you will use an expression to create a dynamic message. - In the Value field, enter:
#['Hello, ' + message.payload + '. Today is ' + server.dateTime.format('dd/MM/yy') + '.'
]
Note that message.payload
andserver.dateTime.format('dd/MM/yy')
are both Mule expressions. Because you are entering them within a larger Mule expression, the #[] syntax is not required around these individual expressions. Anything that you enter inside of the "#[]" syntax which is not a Mule expression must be enclosed with quotes so that Mule reads it as a string. - Save your application by clicking File > Save.
Your complete application XML, once configured, should look like this:
<? xml version = "1.0" encoding = "UTF-8" ?> < mule xmlns:http = "http://www.mulesoft.org/schema/mule/http" xmlns = "http://www.mulesoft.org/schema/mule/core" xmlns:doc = "http://www.mulesoft.org/schema/mule/documentation" xmlns:spring = "http://www.springframework.org/schema/beans" version = "EE-3.4.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd"> < flow name = "Basic_TutorialFlow1" doc:name = "Basic_TutorialFlow1" > < http:inbound-endpoint exchange-pattern = "request-response" host = "localhost" port = "8084" doc:name = "HTTP" /> < logger message = "Current payload is #[message.payload]" level = "INFO" doc:name = "Logger" /> < set-payload value = "#['Hello, ' + message.payload + '. Today is ' + server.dateTime.format('dd/MM/yy') + '.']" doc:name = "Set Payload" /> </ flow > </ mule > |
Running the Application
Having built, configured, and saved your new application, you are ready to run it on the embedded Mule server (included as part of the bundled download of Mule Studio).- In the Package Explorer pane, right-click the
Basic Tutorial.mflow
file, then select Run As > Mule Application. (If you have not already saved, Mule prompts you to save now.) - Mule
immediately kicks into gear, starting your application and letting it
run. When the startup process is complete, Studio displays a message in
the console that reads,
Started app 'basic_tutorial'
.
Using the Application
- Open any Web browser and go to
http://localhost:8084/world
- Your browser presents a message that reads,
Hello, /world. Today is [today's date].
- In your browser’s address bar, replace the word
world
with your own name, then press enter. - Your browser presents the same message, but includes your name instead of "world".
- Next, check your console in Mule Studio to verify that Mule logged the payload before the message reached the expression transformer.
- Place your cursor in the console window, press CTRL+F or COMMAND+F to open a Find dialog, and search for "
Current payload
". Look for results corresponding to the payloads that you entered in your browser. For example:INFO 2013-08-21 09:22:26,446 [[basic_tutorial].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: Current payload is /Aaron
You may also see some other results for a payload of "/favicon.ico
". These results are automatically generated by your browser. You can filter out these results in the Editing the Application section, below.
Stopping the Application
To stop the application, click the red, square Terminate icon above the console panel.Editing the Application
You may have noticed that your browser inserts an additional payload of "/favicon.ico
" upon each refresh. You can add a filter element into your flow to exclude these payloads. Here's how:- Drag and drop an Expression Filter from the Filters section of the palette onto your canvas, positioning it between the HTTP endpoint and the Logger.
- Click the Expression Filter to open its properties tab and enter the following in the Expression field:
#[message.payload != '/favicon.ico']
This expression tells Mule to check that the payload is not equal to the string '/favicon.ico'. If the expression evaluates to true, Mule passes the message on to the next step in the flow. If the expression evaluates to false, Mule stops processing the message. - Save your application and run it again. (Right-click Basic Tutorial.mflow in your Package Explorer, then click Run As > Mule Application.)
- Wait for the message in the console that reads,
Started app 'basic_tutorial'
. - Return to your Web browser and go to
http://localhost:8084/world
- Replace "world" with another word of your choice and refresh. Repeat this step several times with different words.
- Check
the logged payloads in your console: place your cursor in the console
window, press CTRL+F or COMMAND+F to open a Find dialog, and search
for "
Current payload
". - All payloads that you see should correspond to the words you entered in your browser, and "/favicon.ico" should no longer be included.
Your complete application XML, once edited, should look like this:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
mule
xmlns:http
=
"http://www.mulesoft.org/schema/mule/http"
xmlns
=
"http://www.mulesoft.org/schema/mule/core"
xmlns:doc
=
"http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring
=
"http://www.springframework.org/schema/beans"
version
=
"EE-3.4.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<
flow
name
=
"Basic_TutorialFlow1"
doc:name
=
"Basic_TutorialFlow1"
>
<
http:inbound-endpoint
exchange-pattern
=
"request-response"
host
=
"localhost"
port
=
"8084"
doc:name
=
"HTTP"
/>
<
expression-filter
expression
=
"#[message.payload != '/favicon.ico']"
doc:name
=
"Expression"
/>
<
logger
message
=
"Current payload is #[message.payload]"
level
=
"INFO"
doc:name
=
"Logger"
/>
<
set-payload
value
=
"#['Hello, ' + message.payload + '. Today is ' + server.dateTime.format('dd/MM/yy') + '.']"
doc:name
=
"Set Payload"
/>
</
flow
>
</
mule
>
No comments:
Post a Comment