Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Composite Source With Mule ESB

DZone's Guide to

Composite Source With Mule ESB

This walkthrough will show you how to use the Composite Source with Mule Flow, for when you want to define multiple sources to receive incoming messages.

· Integration Zone
Free Resource

Share, secure, distribute, control, and monetize your APIs with the platform built with performance, time-to-value, and growth in mind. Free 90-day trial of 3Scale by Red Hat

Composite source is a scope available with mule ESB which listens to multiple channels for incoming messages. If any of the receivers or inbound endpoints receive the message, it passes to the next processor and results in triggering the flow. A scope in Mule is also known as a wrapper.

There can be a case where you need to define multiple sources to receive the messages. For example, you can receive purchase orders from various customers and every one has to send a message through different channels like File Share, Web Service or REST Call, JMS, SFTP, etc. But the way of handling or business process defined for each customer is same, so we can use the Composite source in such a case.

Let's walk through how to use Composite source with Mule Flow.

Search for Composite source in Mule Pallette and place it to the source region of flow.

 <flow name="composite-flowFlow">
        <composite-source doc:name="Composite Source"/>
    </flow>

Now you can drag and drop multiple connectors under composite source (i.e. wrap the multiple connectors with Composite source). In this case, we will use two HTTP Listeners and one File Connector as inbound endpoints to receive messages from different sources.

  <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:listener-config name="HTTP_Listener_Configuration1" host="0.0.0.0" port="8082" doc:name="HTTP Listener Configuration"/>
    <flow name="composite-flowFlow">
        <composite-source doc:name="Composite Source">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/cs" doc:name="HTTP" allowedMethods="POST"/>
            <http:listener config-ref="HTTP_Listener_Configuration1" path="/cs" doc:name="HTTP" allowedMethods="POST"/>
            <file:inbound-endpoint path="src/test/resources/in" responseTimeout="10000" doc:name="File"/>
        </composite-source>
    </flow>

Image title

Now, drag and drop the File connector in message processor region. Whenever an incoming message is received by any listener, it will pass to next processor. In this case, the message will be passed to File connector in message processor region.

<?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"/>
    <http:listener-config name="HTTP_Listener_Configuration1" host="0.0.0.0" port="8082" doc:name="HTTP Listener Configuration"/>
    <flow name="composite-flowFlow">
        <composite-source doc:name="Composite Source">
            <http:listener config-ref="HTTP_Listener_Configuration" path="/cs" doc:name="HTTP" allowedMethods="POST"/>
            <http:listener config-ref="HTTP_Listener_Configuration1" path="/cs" doc:name="HTTP" allowedMethods="POST"/>
            <file:inbound-endpoint path="src/test/resources/in" responseTimeout="10000" doc:name="File"/>
        </composite-source>
        <file:outbound-endpoint path="src/test/resources/out" responseTimeout="10000" doc:name="File"/>
    </flow>
</mule>

Image titleTesting The Application

You can use Postman to test the application. In this case, there are two HTTP Listeners and one File endpoint listening to incoming messages.

Image title

Similarly, you can use other receivers to trigger the flow.

Now, you know how to use the Composite source in Mule Flow.

Explore the core elements of owning an API strategy and best practices for effective API programs. Download the API Owner's Manual, brought to you by 3Scale by Red Hat

Topics:
mule esb ,mulesoft ,tutorial ,integration ,composite source

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}