Writing 3rd part connectors for wso2 esb

Connsectors and WSO2 ESB


A connector allows you to interact with a third-party product's functionality and data from your ESB message flow, enabling you to connect to and interact with the APIs of services such as Twitter, Salesforce, and JIRA. This means that if you have enabled the Twitter and Google Spreadsheet connectors in your ESB instance, your message flow could receive requests containing a user's Twitter name and password, log into the user's Twitter account, get a list of the user's followers, and write that information to a Google spreadsheet. You can use one or more connectors in order to manage generic use cases related to a business scenario that you may need to address.

You can use the following maven archetype to generate the project template for a 3rd party connectors. Here you can change the artifactID to an artifactID you prefer.


mvn org.apache.maven.plugins:maven-archetype-plugin: 2.4:generate 
-DarchetypeGroupId=org.wso2.carbon.extension.archetype 
-DarchetypeArtifactId=org.wso2.carbon.extension.esb.connector-archetype 
-DarchetypeVersion=2.0.0 -DgroupId=org.wso2.carbon.esb.connector 
-DartifactId=org.wso2.carbon.esb.connector.helloworld 
-Dversion=1.0.0 -DarchetypeRepository=http://maven.wso2.org/nexus/
content/repositories/wso2-public/

After creating the module from maven archetype you can see the following structure in the project.


You can see  the HelloWorldConnector class of the project extends AbstractConnector class from org.wso2.carbon.connector.core package.When you are using the connector you can observe that the MessageContext objects can be retrieved from the connect() method.In the resources folder you have configuration files for the connector.These file helps you to specify the class which contains the connect() method which the MessageContext object first hits the connector.Following you get the content of component.xml ,init.xml and connector.xml.

init.xml

<?xml version="1.0" encoding="UTF-8"?>
<component name="config" type="synapse/template">
    <subComponents>
        <component name="init">
            <file>init.xml</file>
            <description>Configure connector</description>
        </component>
    </subComponents>
</component>

component.xml

<?xml version="1.0" encoding="UTF-8"?>
<template name="init" onError="fault" xmlns="http://ws.apache.org/ns/synapse">
    <sequence>
        <class name="org.wso2.carbon.esb.connector.HelloWorldConnector"/>
    </sequence>
</template>

connector.xml

<?xml version="1.0" encoding="UTF-8"?>
<connector>
    <component name="helloworld" package="org.wso2.carbon.connector">
        <dependency component="config" description="configuration"/>
        <description>wso2 connector</description>
    </component>
    <icon>icon/icon.png</icon>
</connector>

To understand the functionality of the connector we'll retrieve the message body from the MessageContext and log it.


import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseConstants;
import org.wso2.carbon.connector.core.AbstractConnector;
import java.io.IOException;

public class IBMMQProducer extends AbstractConnector {

   @Override  
   public void connect(MessageContext messageContext) throws ConnectException {
SOAPEnvelope soapEnvelope = messageContext.getEnvelope();
        OMElement getElement = soapEnvelope.getBody().getFirstElement();
        String message = getElement.toString();
        log.info(message);
    }
}


Now we need to build the zip file for the connector.Using the following command we can create the zip file.

mvn clean install -Dmaven.test.skip=true

Now we need to upload this zip to the WSO2 ESB. You can do this using the connector section in the management console.

Add connector


To test the connector we need a proxy service.A sample proxy service can be given as follows.

<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="test"    
startOnLoad="true"    
statistics="disable"    
trace="disable"    
transports="http,https">
    <target>
        <inSequence>
            <ibmmq.init/>
        </inSequence>
    </target>
    <description/>
</proxy>

Now you can send a request using postman with a message body and examine the functionality of the connector.

Comments