Your First Mule Application

Building your first Mule application is a hands-on way to understand how MuleSoft works. In this topic, you create a simple application that listens for an HTTP request and returns a greeting message. This "Hello World" application introduces the core building blocks you will use in every future project.

What You Will Build

You will build an application that:

  1. Listens for a GET request at the URL http://localhost:8081/hello
  2. Receives the request
  3. Returns the text "Hello from MuleSoft!" as the response

Application Flow Diagram

Browser / Postman
      |
      | GET http://localhost:8081/hello
      v
[HTTP Listener]         <-- Receives the incoming request
      |
      v
[Set Payload]           <-- Sets response text to "Hello from MuleSoft!"
      |
      v
HTTP Response           <-- Sends the response back to the browser
      |
      v
Browser / Postman
      Displays: "Hello from MuleSoft!"

Step 1: Create a New Mule Project

Open Anypoint Studio. Go to File > New > Mule Project. A dialog box appears with project settings:

  • Project Name: Type hello-world
  • Mule Runtime: Leave the default selected runtime (4.x)
  • Maven: Leave the default Maven settings

Click Finish. Studio creates your project and opens a new blank canvas with a file called hello-world.xml.

Step 2: Add an HTTP Listener

The HTTP Listener is the starting point of your flow. It waits for incoming HTTP requests, like a receptionist waiting for visitors.

  1. In the Mule Palette on the right, find the HTTP section
  2. Drag the Listener component onto the canvas
  3. A flow appears on the canvas with the Listener as the first element

Now configure the Listener:

  1. Click on the Listener in the canvas
  2. The Properties Panel appears at the bottom
  3. Click the green plus icon next to the Connector configuration field
  4. In the dialog, set Host to 0.0.0.0 and Port to 8081
  5. Click OK
  6. Back in the Properties Panel, set Path to /hello

Step 3: Add a Set Payload Component

The Set Payload component sets the content of the response message. It is like writing the reply on a piece of paper before handing it back.

  1. In the Mule Palette, find Core > Transformers
  2. Drag Set Payload onto the canvas, after the HTTP Listener
  3. Click on Set Payload in the canvas
  4. In the Properties Panel, find the Value field
  5. Type: Hello from MuleSoft!
  6. Make sure the mode is set to Value (not expression)

Step 4: Run the Application

Now run the application to test it on your local machine:

  1. Right-click the hello-world project in the Package Explorer
  2. Select Run As > Mule Application
  3. Watch the Console panel at the bottom
  4. Wait for the message: Mule application started successfully

Console Output When App Starts Successfully

INFO  2024-01-15 09:32:14 [main] Starting Mule Runtime...
INFO  2024-01-15 09:32:17 [main] Deploying artifact: hello-world
INFO  2024-01-15 09:32:19 [main] Starting flow: hello-worldFlow
INFO  2024-01-15 09:32:19 [main] HTTP listener listening on: 0.0.0.0:8081
INFO  2024-01-15 09:32:20 [main] Mule application started successfully.

Step 5: Test the Application

Open a browser or a tool like Postman. Enter the URL http://localhost:8081/hello and press Enter or send a GET request.

You receive the response: Hello from MuleSoft!

Congratulations — you just built and ran your first MuleSoft application.

Understanding the XML Behind the Flow

Every flow you build visually in the canvas is stored as XML. Click on the hello-world.xml tab and then click Configuration XML at the bottom of the canvas to see the underlying code.

Generated XML for Hello World Flow

<mule>
  <http:listener-config name="HTTP_Listener_config">
    <http:listener-connection host="0.0.0.0" port="8081" />
  </http:listener-config>

  <flow name="hello-worldFlow">
    <http:listener config-ref="HTTP_Listener_config" path="/hello" />
    <set-payload value="Hello from MuleSoft!" />
  </flow>
</mule>

The visual canvas and the XML always stay in sync. When you drag a component onto the canvas, Studio adds the corresponding XML automatically.

Adding a Logger

A Logger component writes messages to the console. Add it between the Listener and the Set Payload to see when requests arrive.

  1. Drag a Logger from the Mule Palette onto the canvas, between the Listener and Set Payload
  2. In the Properties Panel, set the Message field to: Request received at /hello
  3. Set Level to INFO

Updated Flow Diagram with Logger

[HTTP Listener /hello]
        |
        v
[Logger: "Request received at /hello"]    <-- Logs to console
        |
        v
[Set Payload: "Hello from MuleSoft!"]
        |
        v
HTTP Response sent to client

Run the application again. Each time you call http://localhost:8081/hello, the console shows the log message. Loggers are essential for debugging flows.

Stopping the Application

To stop the running application, click the red square Stop button in the toolbar at the top of Studio, or press Ctrl+C in the Console panel. Always stop an application before making changes and rerunning it to avoid port conflicts.

What You Learned

You created a new Mule project, added an HTTP Listener to receive requests, used Set Payload to set the response, added a Logger for debugging, and ran the application locally. These four components — Listener, Payload, Logger, and Flow — appear in almost every MuleSoft application you will ever build.

Leave a Comment

Your email address will not be published. Required fields are marked *