Mule Component Binding Example

0

In this article we will show how a mule component can be bound to an outbound endpoint thru an interface. The actual implementation of the interface would be some proxy class set using reflection.

Here for simplicity sake, we bind to a vm outbound endpoint else it can be any outbound endpoint that gets the response from an external service.


The bound interface is part of the component as a field with the bean getter and setter methods.

public class SomeQueryComponent {
    private SomeQueryInterface someQueryInterface;
...
    public SomeQueryInterface getSomeQueryInterface() {
        return someQueryInterface;
    }

    public void setSomeQueryInterface(SomeQueryInterface someQueryInterface) {
        this.someQueryInterface = someQueryInterface;
    }
}

In the binding configuration for the component, we bind the interface along with a method in the interface to a Mule endpoint. In the below configuration call to SomeQueryInterface.answerQ1() results in a call to the outbound endpoint vm:answerVm.

<binding interface="com.javarticles.mule.SomeQueryInterface" method="answerQ1">
    <vm:outbound-endpoint address="vm://#[string:answerVm]" exchange-pattern="request-response"/>
</binding>

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:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
      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/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
        http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
        http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

    <flow name="test">
        <http:inbound-endpoint address="http://localhost:${port}" exchange-pattern="request-response"/>

        <jersey:resources>
            <component class="com.javarticles.mule.SomeQueryComponent">
                <binding interface="com.javarticles.mule.SomeQueryInterface" method="answerQ1">
                    <vm:outbound-endpoint address="vm://#[string:answerVm]" exchange-pattern="request-response"/>
                </binding>
                <binding interface="com.javarticles.mule.SomeQueryInterface" method="answerQ2">
                    <vm:outbound-endpoint address="vm://#[string:answerVm]" exchange-pattern="request-response"/>
                </binding>                
            </component>
        </jersey:resources>
    </flow>

    <flow name="TransformationFlow">
        <vm:inbound-endpoint path="answerVm" exchange-pattern="request-response"/>
		<component class="com.javarticles.mule.AnswerComponent"/>
    </flow>
</mule>

SomeQueryComponent:

package com.javarticles.mule;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/")
public class SomeQueryComponent {
    private SomeQueryInterface someQueryInterface;

    @GET
    @Path("/query1")
    @Produces("text/plain")
    public String someQuery1()
    {
        return someQueryInterface.answerQ1("Whats your name?");
    }

    @GET
    @Path("/query2")
    @Produces("text/plain")
    public String someQuery2()
    {
        return someQueryInterface.answerQ2("Where do you live?");
    }
    
    public SomeQueryInterface getSomeQueryInterface() {
        return someQueryInterface;
    }

    public void setSomeQueryInterface(SomeQueryInterface someQueryInterface) {
        this.someQueryInterface = someQueryInterface;
    }

}

SomeQueryInterface:

package com.javarticles.mule;

public interface SomeQueryInterface {
    String answerQ1(String question);
    String answerQ2(String question);
}

The outbound vm endpoint answerVm passes the call to the java component AnswerComponent which finally returns the response.

AnswerComponent:

package com.javarticles.mule;

public class AnswerComponent {
    public String answerTo(String question) {
        if ("Whats your name?".equals(question)) {
            return question + " Joe";
        } else if ("Where do you live?".equals(question)) {
            return question + " XYZ";
        } else {
            return question + " Unknown question";
        }
    }
}

MuleDynamicInterfaceExample:

package com.javarticles.mule;

import java.util.HashMap;
import java.util.Map;

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 MuleDynamicInterfaceExample {
    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);
            executeQuery(muleClient, "query1");
            executeQuery(muleClient, "query2");
        } finally {
            ctx.dispose();
        }
    }

    private static void executeQuery(MuleClient muleClient, String query) throws Exception {
        Map<String, Object> headers = new HashMap<>();
        headers.put("http.method", "GET");
        MuleMessage response = muleClient.send("http://localhost:8888/" + query, "", headers);          
        System.out.println("Response: " + response.getPayloadAsString());
    }
}

Output:

Response: Whats your name? Joe
Response: Where do you live? XYZ

Download the source code

This was an example about mule component binding.

You can download the source code: muleComponentBindingExample.zip
Share.

Comments are closed.