Start an App from a Browser Example - Mule 4
The following example illustrates how to configure the Anypoint Connector for HTTP (HTTP Connector) Listener source to initiate a flow. In this example, the HTTP Listener source listens on a specific port for every network interface on the server. Then, the Logger component logs the message Hello
.
To accomplish this example, you must create the Mule app, configure an HTTP global element, run, and test the app with curl commands.
The following screenshot shows the Anypoint Studio app flow for this example:
Create the Mule Application
To create the Mule flow:
-
In the Mule Palette view, select the HTTP Listener source and drag it on to the canvas.
The source initiates the flow by listening for incoming HTTP message attributes. -
On the Listener configuration screen, optionally change the value of the Display Name field.
-
Set the Path field to
/trigger
to start the app from the web browser. -
Click the plus sign (+) next to the Connector configuration field to configure a global element that can be used by all instances of the HTTP Listener in the app.
-
On the General tab, configure the following fields:
-
Host
All Interfaces [0.0.0.0] (default)
-
Port
8081
-
-
Click OK.
-
Drag a Logger component to the right of HTTP Listener.
-
In the Logger configuration screen, set the Message field to
Hello
. -
Save your Mule app.
-
Click the project name in Package Explorer and then click Run > Run As > Mule Application.
Ensure that the console messages state that the application started correctly by looking at the messageMule is up and kicking
. -
Open a browser and type
http://0.0.0.0:8081/trigger
.
The HTTP Listener source listens to the request on port8081
, and the app starts. -
In the Studio Console, scroll through the logs to see the
Hello
message, for example:
INFO 2020-01-02 13:00:00,438 [[MuleRuntime].cpuLight.15: [http].httpFlow.CPU_LITE @169a1097]
[event: id] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Hello
XML for Starting an App from a Browser
Paste this code into your Studio XML editor to quickly load the flow for this example into your Mule app:
<?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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="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_config" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="flow" >
<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/trigger"/>
<logger level="INFO" doc:name="Logger" message="Hello"/>
</flow>
</mule>