Friday, 1 November 2013



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:
  1. 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.
  2. Run the application on a local runtime embedded in Mule Studio.
  3. Test the application using your browser. 
  4. Stop the application.
  5. Edit the application and rerun it.
  6. (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.
  1. Navigate to the directory on your local drive in which you installed Studio.
  2. Double-click the executable file to launch Studio.
    • MuleStudio.exe in Windows
    • MuleStudio.app in Mac OS
    • MuleStudio in Linux
       
  3. When Studio presents the Workspace Launcher dialog, click OK to accept the default workspace. 
  4. 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

  1. Unless the New Project Wizard is already open, click File > New > Mule Project.
  2. Studio opens the New Mule Project wizard. Fill in the Name field with the value Basic Tutorial.
  3. 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.

  4. Click Next to proceed.
  5. 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.
  6. In the Package Explorer, Basic Tutorial.mflow appears inside the flows folder and mule-project.xml appears underneath the src 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.

 Find out more about the .mflow and mule-project.xml files

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. 
  • 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. 
Drag and drop these three building blocks into place on the canvas to visually construct, or model, your flow, in the order shown:


 More detailed instructions
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.
With just a few clicks, you have modeled your entire application on the Studio canvas.
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
The following instructions introduce you to configuring in both editors.
  1. 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 to 8081.

  2. 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"/>
    The doc:name attribute corresponds to the display name that appears underneath the building block icon on the Message Flow canvas. 
  3. Still in the XML configuration view, change the value of the port attribute to 8084
  4. Click Message Flow to switch back to the visual editor.

      
  5. 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.

  6. Click the Logger building block (or press the right arrow on your keyboard) to switch the properties tab to the Logger component.
  7. 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.
  8. Click the Set Payload building block (or press the right arrow on your keyboard) to switch the properties tab to the Set Payload transformer.
  9. 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.
  10. In the Value field, enter:
    #['Hello, ' + message.payload + '. Today is ' + server.dateTime.format('dd/MM/yy') + '.' ]

    Note that message.payload and server.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.
  11. Save your application by clicking File > Save.


Your complete application XML, once configured, should look like this:
<?xml version="1.0" encoding="UTF-8"?>
    <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).
  1. 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.)

  2. 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

  1. Open any Web browser and go to http://localhost:8084/world
  2. Your browser presents a message that reads, Hello, /world. Today is [today's date].
  3. In your browser’s address bar, replace the word world with your own name, then press enter.
  4. Your browser presents the same message, but includes your name instead of "world".

  5. Next, check your console in Mule Studio to verify that Mule logged the payload before the message reached the expression transformer. 
  6. 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.
Congratulations! You've run and tested your application. You submitted a request to the inbound endpoint of your application via your Web browser. The Mule application received your request and logged the payload (whatever you typed after "http://localhost:8084/") to the console. The application then sent the message on to the Set Payload transformer, which transformed the payload per its instructions and returned the results to your HTTP endpoint.

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:
  1. 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.

  2. 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.
  3. Save your application and run it again. (Right-click Basic Tutorial.mflow in your Package Explorer, then click Run As > Mule Application.)
  4. Wait for the message in the console that reads, Started app 'basic_tutorial'.
  5. Return to your Web browser and go to http://localhost:8084/world
  6. Replace "world" with another word of your choice and refresh. Repeat this step several times with different words.
  7. 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".
  8. 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"?>
    <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: