Contact Us 1-800-596-4880

Echo Example

The Echo example illustrates how to use Mule ESB to build a simple HTTP request-response application.

In this example, a user invokes the Mule application by submitting a request via her browser (i.e. entering a specific URL, http://localhost:8084/echo). The Mule Echo example application receives the request and returns the same payload, or "echoes", the response to the end user. In other words, when a user types http://localhost:8084/echo into the address bar of her browser, Mule returns a message in the browser that reads, /echo (see image below, left); if she enters http://localhost:8084/moon, Mule responds with /moon (below, right).

responses

There are two functions the Echo example application illustrates:

  1. it receives HTTP requests and returns HTTP responses

  2. it logs the requests it receives

After reading this document, and creating and running the Echo example in Mule ESB, you should be able to leverage what you have learned to create a very simple HTTP request-response application. To understand more about Mule ESB’s ability to integrate services and systems, access the Mule examples and see other applications in action.

Assumptions

This document assumes that you are familiar with Mule ESB and the Mule Studio interface. To increase your familiarity with Studio, consider completing one or more Mule Studio Tutorials.

This document describes the details of the example within the context of Mule Studio, Mule ESB’s graphical user interface (GUI). Where appropriate, the XML configuration follows the Studio interface screenshot in an expandable section.

Set Up

As with this Echo example, you can create template applications straight out of the box in Mule Studio or Mule Standalone (Mule ESB without Studio). You can tweak the configurations of these use case-based templates to create your own customized applications in Mule.

Follow the procedure below to create, then run the Echo application in Mule ESB.

  1. Complete the procedure in Examples and Exercises to create, then run the Echo template in Mule Studio, or the Echo example in Mule Standalone (Mule ESB without Studio).

  2. Open your Web browser, type http://localhost:8084/echo in the address bar, then press enter.

  3. Your browser presents a message that reads, /echo.

  4. In your browser’s address bar, replace the word echo with the word moon, then press enter.

  5. Your browser presents a message that reads, /moon.

How it Works

The Echo example application consists of one, simple EchoFlow which receives end user HTTP requests, logs messages’ payloads, and returns the payloads to end users as HTTP responses.

The sections below elaborate further on the configurations of the Echo application and how it works to respond to end user requests.

For Mule Studio Users

In Mule Studio, double-click an icon representing a building block to open its Properties Panel and examine the configuration details. Alternatively, you can click the Configuration XML tab to access and examine the application’s XML config file.

config_tab

EchoFlow

echoFlow

View the XML

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="EE-3.3.0" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
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 ">
    <flow name="EchoFlow" doc:name="EchoFlow">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8084" doc:name="HTTP" doc:description="Process HTTP requests or responses."/>
        <logger message="About to echo #[message:payload]" level="INFO" doc:name="Logger"/>
        <echo-component doc:name="Echo"/>
    </flow>
</mule>

The EchoFlow makes use of three building blocks to receive, process and respond to an end user request. When an end user request encounters the Echo application, the first building block it meets is the request-response HTTP Inbound Endpoint. Because it has a two-way message exchange pattern, this HTTP endpoint is responsible for both receiving requests from, and send sending responses to, the end user.

Two-Way vs. One-Way

Notice that the HTTP endpoint has a two-way message exchange pattern (as indicated by the small double-arrow icon — below, left). Because it must respond to the requester, the HTTP endpoint in this example has a request-response message exchange pattern.

If an HTTP endpoint has only to input information into an application, it requires a one-way message exchange pattern (below, right).

http_endpoints

Next, the flow uses a Logger Component to log the message payload in the application’s log files. The logger component uses an Expression to determine what information in, or about, the message that it should log. In this case, because it needs to log the message payload, the instructions to log read About to echo #[message:payload].

logger_properties

Finally, Mule moves the message to an Echo Component which simply returns the message payload as the response to an end user. In other words, the response echoes the request.