Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

HTTP Callbacks

Many public APIs use callbacks to notify API consumers of specific events. For example, after a phone call Twilio performs an HTTP request to a specified URL to pass information about the status of the call.

DevKit provides a special data type to facilitate invocation of a Mule flow when an HTTP callback is received.

The HTTPCallback Interface

To return a status, DevKit creates an HTTP inbound endpoint for your connector: 


         
      
1
2
3
4
5
6
7
8
9
10
import org.mule.api.annotations.Connector;
import org.mule.api.annotations.Processor;
import org.mule.api.callback.HttpCallback;
@Connector(name = "callback")
public class HttpCallbackTest
{
   @Processor public void doA(HttpCallback onEventX, HttpCallback onEventY)
   {
      String urlEventX = onEventX.getUrl();
      String urlEventY = onEventY.getUrl();

The HTTPCallback interface acquires this inbound endpoint via a single method. When the callback is received, the enpdoint references another flow in the Mule configuration file.  

Note that the flows referenced do not have an inbound endpoint:


         
      
1
2
3
4
5
6
7
8
9
10
11
12
<flow name="doA">
   <callback:do-a on-event-x-flow-ref="flowToExecuteOnEventX"
             do-a on-event-x-flow-ref="flowToExecuteOnEventX"/>
</flow>
 
<flow name="flowToExecuteOnEventX">
   <component class="org.mule.devkit.it.ComponentX"/>
</flow>
 
<flow name="flowToExecuteOnEventY">
   <component class="org.mule.devkit.it.ComponentY"/>
</flow>

Advanced Configuration

The table below lists advanced HTTPCallback attributes that you can use in the Mule configuration file:

Attribute Name Description

localPort

Use to create the HTTP inbound endpoint. Defaults to environment variable http.port

remotePort

The public port to use to construct the public URL to be sent to the external system. Defaults to environment variable http.port

domain

Use to construct the the public URL to send to the external system. Defaults to environment variable fullDomain

async

Possible values are true or false depending on whether the callback flow should be executed asynchronously or not. Defaults to true

The example below illustrates how to use the optional, in-flow HTTPCallback attributes.


         
      
1
2
3
4
<callback:config>
   <callback:http-callback-config domain="www.mydomain.com"
                localPort="8080" remotePort="80" async="false" />
</callback:config>

How It Works

Behind the scenes, DevKit creates a Mule flow containing a message processor that invokes the desired flow. By default, DevKit wraps this processor in an async message processor chain. (You can override this behavior using the async attribute as described in the table above.)

DevKit builds the URL of the HTTP inbound endpoint using localhost as host and the http.port environment variable, or the value of localPort, as port. The path of the URL is a random string.

The URL that is passed to the external system is identical to the URL of the HTTP inbound endpoint, except that the host is replaced by the domain setting (or its default value) and the port is replaced by the remotePort setting (or its default value).