In this post I dive into the use of a Component Binding in Mule. The context is given here. I will show how I use a component binding to combine the original message and the response message in one message. In the next post I show how I use this combined message to determine (and perform) the next step.
Here is the relevant piece of the Mule config file for the component binding:

XML:
  1. <service name="OrderPostService">
  2.   <inbound>
  3.     <vm:inbound-endpoint path="order-post-service" synchronous="true"/>
  4.   </inbound>
  5.   <component class="net.pascalalma.mule.OrderAppInvoker">
  6.     <binding interface="net.pascalalma.mule.InboundOrderBinding" method="process">
  7.       <http:outbound-endpoint address="http://app-server:8090/order-in" synchronous="true">
  8.         <response-transformers>
  9.           <byte-array-to-string-transformer />
  10.         </response-transformers>
  11.       </http:outbound-endpoint>
  12.     </binding>
  13.   </component>
  14.   <default-service-exception-strategy>
  15.     <vm:outbound-endpoint path="handle-error" />
  16.   </default-service-exception-strategy>
  17. </service>

What this service does is it receives the message that has to be send to the order application on a VM channel 'order-post-service'. To send the message to the order application it is posted on an outbound http channel 'http://app-server:8090/order-in'. But like I said this outbound endpoint is bound to the component 'OrderAppInvoker' by the interface 'InboundOrderBinding'.
The interface 'InboundOrderBinding' looks like this:

JAVA:
  1. package net.pascalalma.mule;
  2.  
  3. public interface InboundOrderBinding {
  4.  
  5.     String process(String o);
  6. }

What you see here is that the binding has a method 'process', which takes a String as input and will return a String as output. The name of the method ('process') is actually irrelevant here since I bind it to a HTTP endpoint. What is important is that a String is supplied and a String is returned.

The class 'OrderAppInvoker' looks like:

JAVA:
  1. package net.pascalalma.mule;
  2.  
  3. import org.mule.api.lifecycle.Callable;
  4. import ...;
  5.  
  6. public class OrderAppInvoker implements Callable {
  7.  
  8.     private InboundOrderBinding binding;
  9.  
  10.     public InboundOrderBinding getBinding() {
  11.         return binding;
  12.     }
  13.     public void setBinding(InboundOrderBinding binding) {
  14.         this.binding = binding;
  15.     }
  16.  
  17.     public Object onCall(MuleEventContext ctx) throws Exception
  18.     {
  19.         MuleMessage msg = ctx.getMessage();
  20.  
  21.         //Get the original XML message
  22.         String orgMessageString = (String) msg.getPayload();
  23.  
  24.         // Call the bound method and get the response message
  25.         String responseString = binding.process(orgMessageString);
  26.  
  27.         //Deserialize the String messages to JAXB Objects so it's easier to combine the
  28.         // two to my new message type
  29.         InboundOrderType it = unmarshal(InboundOrderType.class, orgMessageString);
  30.         OrderResponseType or = unmarshal(OrderResponseType.class, responseString);
  31.  
  32.         // Now construct the new response containing both the original and the orderResponse
  33.         // message
  34.         ObjectFactory of = new ObjectFactory();
  35.         MyResponseType responseType = of.createMyResponseType();
  36.         responseType.setOrderResponse(or);
  37.         responseType.setOriginalMsg(it);
  38.         JAXBElement<myResponseType> response = of.createMyResponse(responseType);
  39.  
  40.         // Put the newly constructed as payload on the message and return that message
  41.         msg.setPayload( marshal(response));
  42.         return msg;
  43.     }
  44.  
  45.     private <t> T unmarshal(Class<t> docClass, String input)
  46.             throws JAXBException {
  47.        ...
  48.     }
  49.  
  50.     public String marshal(JAXBElement el) throws JAXBException {
  51.         ...
  52.     }
  53. }

What I do here is I created a getter and setter for the interface that is used for the bound component and I implemented the 'onCall' method. What I actually do in the 'onCall' method is that I store the original message in a local variable, I then call the 'http-endpoint' via the binding and get the response message. This response message is combined with the original message (by using JAXB) in a new message I created and this new message is put on the payload. So when this component is finished I have both the original message and the response message in one message. This aggregated message is transferred to the next service 'process-response' by making use in of a 'chaining-router' as described here.
In the next post we will see how I use this combined message and process it further.