Mule composite source example

0

When a message source contains more than one inbound endpoint to feed messages we useĀ <composite-source>.

In our example, we have combined a vm with an http inbound-endpoint. Each endpoint in a composite message source will start a new flow execution as soon as it receives a message.

Both vm and http endpoints use request-response exchange pattern but there is no constraint on exchange-pattern. The inbound-endpoints can use whichever exchange pattern they need to.

For simplicity sake, we just echo the message we receive.

muleContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
	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://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">

	<flow name="echo">
		<composite-source>
			<vm:inbound-endpoint path="in"
				exchange-pattern="request-response"/>
			<http:inbound-endpoint address="http://localhost:${port}" path="in" exchange-pattern="request-response"/>
		</composite-source>
		<echo-component />
	</flow>

</mule>

In order to kick in the flow, we send messages to vm:/in as well as http://localhost:8888/in.

MuleCompositeSourceExample:

package com.javarticles.mule;

import org.mule.DefaultMuleMessage;
import org.mule.api.MuleContext;
import org.mule.api.MuleMessage;
import org.mule.api.context.MuleContextBuilder;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.DefaultMuleConfiguration;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextBuilder;
import org.mule.context.DefaultMuleContextFactory;
import org.mule.module.client.MuleClient;

public class MuleCompositeSourceExample {
    public static void main(String[] args) throws Exception {
        System.setProperty("port", "8888");
        DefaultMuleConfiguration dmc = new DefaultMuleConfiguration();
        dmc.setId("muleexample");
        dmc.setWorkingDirectory("/esb/mule");
        SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
                "muleContext.xml");
        MuleContextBuilder contextBuilder = new DefaultMuleContextBuilder();
        contextBuilder.setMuleConfiguration(dmc);
        MuleContextFactory contextFactory = new DefaultMuleContextFactory();
        MuleContext ctx = contextFactory.createMuleContext(configBuilder,
                contextBuilder);
        ctx.start();
        try {
            MuleClient muleClient = new MuleClient(ctx);
            MuleMessage message = new DefaultMuleMessage("<html><body>Hello</body></html>", ctx);
            MuleMessage response = muleClient.send("http://localhost:8888/in", message);            
            System.out.println("Response: " + response.getPayloadAsString());
            
            response = muleClient.send("vm:/in", "Hello", null);  
            System.out.println("Response: " + response.getPayloadAsString());
        } finally {
            ctx.dispose();
        }
    }
}

Output:

********************************************************************************
* Message received in service: echo. Content is:                               *
* 'Hello'                                            *
********************************************************************************
Response:Hello
[INFO ] 2018-05-06 20:43:41.534 [main]AbstractLifecycleManager - Initialising: 'connector.VM.mule.default.dispatcher.2076627578'. Object is: VMMessageDispatcher
[INFO ] 2018-05-06 20:43:41.534 [main]AbstractLifecycleManager - Starting: 'connector.VM.mule.default.dispatcher.2076627578'. Object is: VMMessageDispatcher
[INFO ] 2018-05-06 20:43:41.534 [main]LogComponent - 
********************************************************************************
* Message received in service: echo. Content is: 'Hello'                       *
********************************************************************************
Response: Hello

Download the source code

This was an example about Mule’s composite-source element.

You can download the source code here: muleCompositeSourceExample.zip
Share.

Comments are closed.