Nav

How to Create a Mule App

Learn to create a "Hello World" Mule project, then deploy it as a Mule application through Anypoint Studio’s embedded Mule server. Finally, test it by interacting with the application using a web browser.

Prerequisites

This tutorial assumes that you have downloaded, installed, and launched Anypoint Studio, which comes with an embedded Mule runtime instance.

As the objective of this quick tutorial is to get something built and running fast, this document doesn’t spend much time explaining Mule concepts or components. For more background on Mule see the section on Mule Runtime and for a more thorough look into Mule app design, see the example in the Basic Studio Tutorial.

Create a New Project

  1. In Anypoint Studio, click File and New > Mule Project.

  2. Studio opens the New Mule Project wizard. In the Name field, enter the value Basic Tutorial, then click Finish.

    new

  3. Studio opens a new blank project. The following quick reference explains the different views you use to build a Mule project:

    blank+canvas

  4. Begin designing your project by dragging and dropping an HTTP Connector from the palette onto the canvas. Notice that Studio automatically wraps the connector with a flow, thus saving you the step of manually creating one. There will be a red error dot overlaid on the HTTP Connector because it has not yet been configured.

    add+http

  5. Add a Set Payload Component to your flow, by dragging it and releasing it just to the right of the HTTP Endpoint as shown below.

    set+payload

    When you release your mouse, Anypoint Studio positions the Set Payload transformer next to the HTTP Connector with a series of arrows. The arrows signify the direction in which the Mule messages transition through the different components of the flow.

  6. Click Message Flow (Visual Editor) and then Configuration XML (XML Editor) tabs below the canvas to alternate between the editors so that you can see how Studio provides both a visual and code representation of your project.

    xml+code

  7. Click the Message Flow tab to return to the graphical editor.

  8. Click the Save icon or press Ctrl+S (Windows and Linux) or CMD+S (Mac) to save your project:

    studio-save-button

Configure the Flow Elements

Now that you’ve created a basic application in Studio, you can take the steps to configure each individual element. The first element to configure is the HTTP Connector. The HTTP Connector allows your Mule application to connect to Web resources through the HTTP or HTTPS protocol. The HTTP Connector needs to be given basic information before it can work. The following HTTP connector properties must be set to a value:

  • Path

  • Connector Configuration

  • Name

  • Host

  • Port

You also create a global element for the HTTP Connector to reference. The global element encapsulates some reusable configuration properties. Multiple connectors can reuse the global element.

To configure the HTTP connector:

  1. Click the HTTP Connector on the canvas to open its Properties Editor, and click the green plus sign to configure the host and port number:

    http+pelado

    This dialog defines the properties for a Global Element. A single global element can be referenced by multiple HTTP Connectors, saving you from having to specify these properties on each.
  2. For this example, set the host to localhost and accept the default port 8081:

    http-global-config

  3. Click OK. This returns you to the properties window.

  4. Path is already populated by the default value, slash /.

    The Connector Configuration field is now populated with a reference to the element you just created.

  5. Click the connector in the Canvas, and the red warning marks disappear from the properties editor and the representation for the connector on the canvas.

  6. Click the Set Payload Component on the canvas to open its properties editor in the console.

    The Set Payload Component simply sets the message payload to a value you define.

  7. Set the Value field to Hello World! for example.

    set+payload+hello+world

  8. Once again, click the Configuration XML to view your changes in the XML configuration tab:

    
                
             
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    <?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"
            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:listener-config name="HTTP_Listener_Configuration"
        host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
        <flow name="basic_tutorialFlow">
            <http:listener config-ref="HTTP_Listener_Configuration"
            path="/" doc:name="HTTP"/>
            <set-payload value="Hello World!" doc:name="Set Payload"/>
        </flow>
    </mule>
  9. Click the Save icon to save your changes.

You have now built a working Mule application! Although a simple application, this demonstrates with only two components how Anypoint Studio leverages a wealth of technology with simple drag-and-drop placement.

Deploy the Project Locally via Studio

Anypoint Studio embeds a server that you use for testing deployments as you code. You can deploy your application on the embedded server to see it working and perform any debugging activities before deploying to a production environment, such as an on-premise server or Runtime Manager.

  1. In the Package Explorer, right-click the project name, then select Run As > Mule Application.

    run+as

    You can also run your application from the Studio Run menu. Click your project name in Package Explorer and then click Run > Run As > Mule Application:

    run-as2

  2. Studio launches the application on the embedded server, displaying its actions in the console. When complete, the console message reads:

    Started app 'basic_tutorial'

    console output

Interacting with the Locally Deployed App

If you deployed your app locally via Studio on your local Mule runtime server, follow along here.

  1. Open your favorite Web browser. 

  2. Navigate to the URL of your application (this address is the result of the default settings of the HTTP Connector):

  3. The application accepts the request via its HTTP endpoint, then the Set Payload component sets the payload to "Hello World!", and finally the HTTP connector returns this payload as the response you see in your browser.

    hello+world

  4. In Studio, stop the application from running using the Terminate icon in the console:

    console2

See Also