Creating a simple extension activity

This article provides a a brief introduction to creating an extension activity in Assemble flow.

The Creating a simple Assemble flow tutorial provides the helloworld project in which you can create a flow. This tutorial shows how to make your own extension activity, and how it can be used in Assemble tooling.

This simple extension activity invokes an external Web service to get the latest exchange rate for different currencies, for example, an exchangeRate activity for getting the latest exchange rate for converting a Mexican peso to the American dollar. The following steps are used to create the simple extension activity:

  1. Implementing an extension activity in Groovy
  2. Creating a flow file
  3. Enabling the extension activity in Assemble tooling

Implementing an extension activity in Groovy

You can implement an extension activity using a Groovy method that follows a certain convention defined by Assemble flow. In this example, create it in the helloworld project using the following steps:

  1. Create the helloworld/app/assemble/activities directory if it does not already exist.
  2. Create the exchangeRate.groovy Groovy file in the helloworld/app/assemble/activities directory. You MUST name the file the name of your extension activity, as shown in the following example:

    Creating a Groovy file

  3. Add following code and save it as the exchangeRate.groovy file:
    import zero.core.connection.Connection
    import zero.core.connection.Connection.Response
    		
    def onExchangeRateActivity() {
    	// retrieve the data from the GlobalContext
    	def from = event.attributes["from"]
    	def to = event.attributes["to"]
    	        
    	// executing the logic
    	String url = "http://webservicex.com/CurrencyConvertor.asmx/ConversionRate?fromCurrency=${from}&ToCurrency=${to}"
    	InputStream is = null
    	try {
    		is = Connection.doGET(url).getResponseBodyInputStream()
    		def root = new XmlParser().parse(is)
    		// put the output of the activity to GlobalContext
    		event.result = root.text().toDouble()
    	} finally {
    		if (is != null) {
    			is.close()
    		}
    	}
    }
    // validation information
    metadata = [
    	"attributes" : [["name":"from"],["name":"to"]],
    	"inputOccurrence" : 0
    ]
    

Creating a flow file

Add a flow file in the ready project using the following steps:

  1. Create a new exchangeRate directory under the public folder, as shown in the following example:

    create-directory-exchangeRate

  2. Create a index.flow file under the /public/exchangeRate directory.
  3. Add the following XML fragment in the index.flow file:
    <process name="exchange">
    	<receiveGET name="rcv"/>
    	<exchangeRate name="getRate" from="MXN" to="USD">
    		<control source="rcv"/>
    	</exchangeRate>
    	<replyGET name="rpy">
    	    <input value="${getRate * rcv.currency[0].toDouble()}" content-type="text/plain"/>
    	</replyGET>  	
    </process>
    

After completing the previous steps, run the helloworld project. (For more information about running this project, see the Run and see the result of the flow information.)

You can access the flow using the following URL: http://localhost:8080/exchangeRate?currency=100 and see a result similar to the one in the following example:

result-of-exchangeRate-activity

Enabling the extension activity in Assemble tooling

The extension activity can be used in tooling like the built-in activity. See the information about adding a custom activity to the tooling for details.

Version 1.0.0.3.25591