Contact Us 1-800-596-4880

Basic Studio Tutorial

Walk through this tutorial to learn how to use Anypoint Studio to build a simple HTTP request-response application.

Prerequisites

Before going any further, make sure you have downloaded and installed Anypoint Studio. If you do not have any previous experience with Eclipse or an Eclipse-based IDE, review the brief introduction to the Anypoint Studio interface.

Goals

This tutorial covers these tasks:

# Task Section

1

Launch Anypoint Studio

Launching Studio

2

Create a simple application that displays a message in a browser

Constructing an Application

3

Learn about logging

Adding Logging

4

Learn about flows in your application

Working With Flows

5

Add more features to your application

Extending Your Application

Launching Studio

If you 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.

    • AnypointStudio.exe in Windows

    • AnypointStudio.app in Mac OS

    • AnypointStudio in Linux

  3. When Studio displays the Workspace Launcher dialog, click OK to accept the default workspace.

    A workspace launcher window with a text box, buttons, and instructions to select a workspace folder
  4. If this is the first time you have launched Studio, you see a Welcome screen. Click Create a Project to dismiss the screen and jump straight to the New Mule Project wizard.

    An arrow points to a button that says

Creating a New Project

  1. If the New Mule Project screen is not open, click File > New > Mule Project.

    Studio opens the New Mule Project wizard.

  2. Fill in the Project Name field with the value Basic Tutorial.

    A form for the creation of a new Mule project
  3. Click Finish to create the project.

The Studio UI, which has the following main sections, appears:

The Mule Design workspace displays a console tab, a palette for filtering and selecting building blocks, and a project structure in the package explorer

The orange area in the center is the canvas where you build a visual representation of your Mule program by dragging building blocks from the palette outlined in green to the canvas. For more information about how to use the visual editor, see this quick overview.

At the bottom of the canvas are Message Flow and Configuration XML tabs for alternating between visual and XML editing of the application.

Text labels for Message Flow, Global Elements, and Configuration XML

On the Global Elements tab you set properties for configuration elements having global scope, such as the HTTP Listener Configuration introduced later.

More on the XML File

When you create a Mule Project, Studio creates an XML file in the src/main/app folder, which appears in the Package Explorer. This file stores flow information for the visual editor. Studio derives the file name basictutorial.xml from the project name, in this case "Basic Tutorial".

A file explorer window displaying a project’s folder structure and files

The mule-project.xml file at the bottom of the Package Explorer is the Mule project descriptor file. Open this file to change the project runtime, set or edit environment variables, or edit the project description. After creating a project, you can commit this file, along with the <project_name>.xml file to your source control systems to avoid errors when sharing your project.

Constructing an Application

In this section of the tutorial, you build an application that displays a message in the browser.

This section uses these building blocks:

HTTP Connector

Allows your Mule application to connect to Web resources through the HTTP or HTTPS protocol. Find this in the Connectors section of the palette.

Set Payload Transformer

Modifies your payload into a "Hello, World" message. Find this in the Transformers section of the palette.

Tip: Use Studio Search

A search bar for locating building blocks, categories, or templates within Anypoint Exchange

Note: Building blocks in each category of the palette are organized alphabetically. To avoid scrolling, use the search tool in the upper right corner of the palette to find the building blocks that you want more quickly. You can also use this area to view more information about the building block in Anypoint Exchange.

  1. Search for http in the palette and drag the HTTP connector to the canvas. This action creates the flow named basictutorialFlow.

    An orange arrow points to an HTTP connector in the Mule Palette
  2. Click on the https listener canvas representation and in In General Settings, click the green plus sign to the right of Connector Configuration:

    A plus button for creating a new configuration

    The Global Element Properties dialog appears:

    A window with an OK button to confirm the HTTP Listener configuration
  3. Click OK to accept the defaults: Host 0.0.0.0 and Port 8081.

  4. Search for set payload and drag the Set Payload transformer to the Process section of the flow.

    A form that includes fields to specify a payload, encoding, and MIME type

    Later, you use the host IP address and port number 0.0.0.0:8081 in a browser to view the application response.

  5. Click the HTTP connector. In Basic Settings in the properties editor, set the Path value to an asterisk (*). The Path value indicates how you can access your application from a browser. With the "✱" value, you can specify alternate values after a slash in the URL, for example, 0.0.0.0:8081/Polly or 0.0.0.0:8081/Jane.

    HTTP settings configuration form
  6. Click the Set Payload transformer to set its properties, and in the Value field, replace the #[] value with Hello MuleSoft:

    A form with fields for the display name, value, encoding, and MIME type

    With just a few clicks, you have created an application.

Verifying That Your Application Works

To test the application:

  1. Right-click the project name in Package Explorer and click Run > Run As > Mule Application:

    Dropdown menu featuring options to execute a Mule application

    Click OK to save and launch the application.

    The console lists output that looks something like this - Look for a message with the project name that says it started, such as:

    Started app 'basic-tutorial'

    Console output indicating the successful launch of the
  2. Open a browser and type the address 0.0.0.0:8081. You see the payload, Hello MuleSoft:

    A browser window displaying

Next, learn about logging.

Adding Logging

As soon as you run the application, Studio starts logging error, warning, tracing, and debugging information. Studio uses Apache log4j 2-based logging. You can use the default log4j 2 settings or modify settings in the log4j2.xml file located in the project’s src/main/resources path. You can view this path in the Package Explorer:

A file explorer window displaying project files and folders

The palette contains a Logger component that you can include in your flow to manage logging. Default logging occurs whether you add a Logger or not.

To add a Logger component:

  1. Search for logger in the palette.

  2. Drag the Logger to the right of Set Payload:

A Mule Palette with the Logger component highlighted

In the next section, you can change the default logger settings, but for now, just accept the defaults.

Where does the logging output appear?

A file explorer window with a file named log4j2.xml

The log4j2.xml file defines where the logging output appears:

<Appenders>
  <RollingFile name="file" fileName="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}basictutorial.log"
           filePattern="${sys:mule.home}${sys:file.separator}logs${sys:file.separator}basictutorial-%i.log">
    <PatternLayout pattern="%d [%t] %-5p %c - %m%n" />
    <SizeBasedTriggeringPolicy size="10 MB" />
    <DefaultRolloverStrategy max="10"/>
  </RollingFile>
</Appenders>

This code specifies the location of the log in the .mule directory of your Studio workspace as follows:

  • The mule.home value is the path to your workspace.

  • The file.separator value is a backslash (\) in Windows or a forward slash in Mac and Linux.

  • The base file name is the project name and the extension is .log.

For example on a Mac, the location of the basictutorial.log is /Users/userName/AnypointStudio/workspace/.mule/logs.

The following snippet shows the first few entries in the basictutorial.log:

2016-05-01 11:07:28,951 [main] INFO  org.mule.module.launcher.application.DefaultMuleApplication -
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Initializing app 'basictutorial'                        +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2016-05-01 11:07:29,065 [main] INFO  org.mule.lifecycle.AbstractLifecycleManager - Initialising RegistryBroker
2016-05-01 11:07:29,184 [main] INFO  org.mule.module.extension.internal.manager.DefaultExtensionManager - Starting discovery of extensions
2016-05-01 11:07:29,313 [main] INFO  org.mule.module.extension.internal.manager.DefaultExtensionManager - Discovered 1 extensions
2016-05-01 11:07:29,313 [main] INFO  org.mule.module.extension.internal.manager.DefaultExtensionManager - Registering extension validation (version 3.7)

After running the application, the following default Info: Basic Tutorial Logger component message value appears in the log:

2016-05-01 10:26:31,842 [[basictutorial].HTTP_Listener_Configuration.worker.01] INFO org.mule.api.processor.LoggerMessageProcessor - Info: Basic Tutorial

More information on logging is at Logging in Mule and Logging.

Working With Flows

Most Mule elements provide configuration options, which you can set in one of the following ways:

  • Using the building block Properties tabs in Studio visual editor

  • Using XML code in Studio XML editor

Use the Configuration XML at the bottom of the canvas to switch to the XML editor view.

An orange button labeled Configuration XML

XML tags representing the flow have a flow element parent. The child elements represent the components you dragged to your canvas from the palette.

The doc:name attribute on each element corresponds to the display name that appears below the building block icon on the Message Flow canvas. The http:listener element path property is * and its property config-ref points to the Configuration Element that you created.

The Configuration Element sits above the flow you built, outside it, as it could potentially be referenced by other HTTP connectors on other flows. The configurations you set in the Configuration Element and those in the XML view of an element are the same.

Studio two-way editor automatically updates the configuration as you switch back and forth. For example, if in the XML configuration view you change the value of the port attribute on the http-listener-config, this change is reflected in the visual editor when you click Message Flow to switch back to the visual editor.

A tab with options for message flow, global elements, and configuration XML

Extending Your Application

At this point, your application simply returns the payload literal, Hello MuleSoft, as an HTTP response in a browser. Revise the application to perform the following actions:

  • Log a unique message ID that Mule automatically assigns to each message that passes through a flow.

  • Create a dynamic, inbound message payload that appears in your browser when you send a request to the application.

  • For each request, write a file to a specified location that contains the dynamic inbound message.

You need to add another message processor to your flow to direct Mule to write a payload to a file. Use a Mule expression to log and use a unique message ID. The following procedure provides step-by-step instructions:

  1. Click the existing Logger component in your flow to open the properties editor.

  2. Enter the following text and expression in the Message field to read:

    Current path is #[message.inboundProperties.'http.request.path']

    A message field displays the current path with a code snippet

    The String Current path is #[message.inboundProperties.'http.request.path'] is a Mule expression that evaluates to the requested property 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.

  3. Click the Set Payload building block to open its properties editor.

  4. In the Value field, enter the following dynamic expression:

    #['Hello, ' + message.inboundProperties.'http.request.path' + '. Today is ' + server.dateTime.format('MM/dd/yy')]

    A settings panel with a code snippet for the value field

    The Value field supports Mule expressions, as well as literals such as Hello MuleSoft, which you used previously. Mule expressions are formatted using square brackets prefaced with a #, #[]. If you enter a Mule expression here, Mule evaluates it at runtime and returns or uses the results for further processing.

    The message.inboundProperties.'http.request.path' and server.dateTime.format('MM/dd/yy') are both Mule expressions. Because you are entering the expressions within a larger Mule expression, the [] syntax is not required around these individual expressions. Anything that you enter inside of the [] syntax that is not a Mule expression must be enclosed in quotation marks. Mule interprets an expression in quotation marks as a string.
  5. Slide the Logger component to the left of Set Payload transformer.

  6. Drag and drop a File endpoint onto your canvas immediately after the Set Payload transformer.

    A diagram that illustrates the steps in an application flow
  7. Click the File endpoint to open the properties editor, and browse to set the Path for saving the file. For example: /Users/userName/Downloads.

  8. Enter a File Name/Pattern that defines the file name and type, as follows:

    #[message.id].txt

    The #[message.id] string is the Mule expression that resolves to the unique message ID of the current message.

    A form with fundamental settings for a file, such as name, path, and pattern
  9. Click the Configuration XML tab at the bottom of the Canvas to view the code of the revised application:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:file="http://www.mulesoft.org/schema/mule/file"
    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"
    	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/core
    http://www.mulesoft.org/schema/mule/core/current/mule.xsd
    http://www.mulesoft.org/schema/mule/http
    http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
    http://www.mulesoft.org/schema/mule/file
    http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
        <http:listener-config name="HTTP_Listener_Configuration"
        host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="basic_tutorialFlow">
            <http:listener config-ref="HTTP_Listener_Configuration"
            path="*" doc:name="HTTP"/>
            <logger
            message="Current path is #[message.inboundProperties.'http.request.path']"
            level="INFO" doc:name="Logger"/>
            <set-payload value="#['Hello, ' +
            message.inboundProperties.'http.request.path' +
            '. Today is ' + server.dateTime.format('MM/dd/yy')]"
            doc:name="Set Payload"/>
            <file:outbound-endpoint path="/Users/<ME>/Downloads"
            outputPattern="#[message.id].txt"
            responseTimeout="10000" doc:name="File"/>
        </flow>
    </mule>
  10. Save your application, then run it again. Right-click basictutorial.xml in your Package Explorer, then click Run As > Mule Application. In the console, a message appears that looks something like this:

    *************************************************************
    * basictutorial        * default            * DEPLOYED     *
    *************************************************************
  11. Return to your Web browser and go to http://0.0.0.0:8081/world. Replace world with another word of your choice and refresh.

    You see the following results:

    • In the console, the logging of the unique message ID appears. For example:

      INFO  2016-06-29 15:22:43,080 [[basictutorial].HTTP_Listener_Configuration.worker.01] org.mule.api.processor.LoggerMessageProcessor: Current path is /world
    • The dynamic, inbound message appears in your browser when you send requests. For example: Hello, /world. Today is 06/29/16.

    • The directory you specified for writing files contains <UUID>.txt files. For example: 42424242-4242-4242-4242-acbc4242ecbd.txt

    • The contents of each .txt file match the dynamic, inbound message specified by the payload.

Next

The following topics help you augment your knowledge of Studio: