Hi Guys, today i am going to discuss how to create a custom connector in mule esb.
MuleSoft Anypoint DevKit:
The
MuleSoft Anypoint DevKit, or simply
DevKit, enables the development of MuleSoft Anypoint Connectors.
To develop the custom connector we have to setup following things.
1. java - java 6 or 7 required to compile and build the Java code.
2. Maven. - for managing connector build.
3. Anypoint DevKit - Plugin in Anypoint DevKit Update Site - http://studio.mulesoft.org/r5/devkit.
Maven setup:
Maven Tools for Mule ESB is a plugin for Maven that allows you to
develop Mule applications using Maven tooling. The kit provided by the
plugin includes archetypes for building regular Mule applications, Mule
domains and Mule domain bundles.
Setup MAVEN_HOME:
Update PATH variable.
Open setting.xml in apache-maven-3.2.5\conf folder and check bellow.
Add pluginGroup in pluginGroups section.
Specifies a further group identifier to use for plugin lookup. Now maven look for maven repository for all dependent jars.
Adding the Maven Dependency for the Plugin.
you need to add some entries in your settings.xml
file, as shown below.
Add the following to the profiles
activeProfiles section:
<profile>
<id>Mule</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>MuleRepository</id>
<name>MuleRepository</name>
<url>https://repository.mulesoft.org/nexus-ee/content/repositories/releases-ee/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>mule-extra-repos</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>mule-public</id>
<url> https://repository.mulesoft.org/nexus/content/repositories/public </url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>mule-public</id>
<url> https://repository.mulesoft.org/nexus/content/repositories/public </url>
</pluginRepository>
</pluginRepositories>
</profile>
DevKit Features:
- Visual design and implementation using Anypoint Studio with
an Eclipse-based interface that simplifies and speeds up development.
- Maven support.
- Connector packaging tools.
- Authentication support for multiple types of authentication, including OAuth and username and password authentication.
- DataSense support to acquire remote metadata.
- Extensive testing capability.
- Examples, training, and support to simplify development startup.
- Batch, Query Pagination, and DataSense Query Language support.
Install DevKit Plugin and check for bellow things.
We can able to see Anypoint connector project and Anypoint connector component.
Connector:
An Anypoint connector is an extension module that eases the interaction
between a Mule application and external resources, such as a databases
or APIs, through REST, SOAP, or the Java SDK.
Connector Architecture:
Connectors operate within Mule applications,
which are built up from Mule Flows, and external resources, which are
the targeted resources.
Connector Project:
Create new project
File --> New ---> Anypoint Connector Project.
Define the attributes as needed:
- Connector Name: Define a unique name for the Anypoint Connector project.
- Run time: Select the compatible Mule runtime for
the connector. This information in the page applies to all Mule versions
from 3.5.0 and later
- Type: Select the way connector can access a resource with the Type field:
REST/GENERIC (Java): For Implementing a REST Connector or Creating a Connector using a Java Client Library
SOAP: For implementing SOAP Connector and specify WSDL location
- Authentication: Choose an authentication of how the connector accesses its resource: custom or OAuth V2 or select None is no security want to provide.
- WSDL location: (Only appears if Type is SOAP)
- From WSDL file or URL: Type in a location or click
the browse (…) button to locate the WSDL file on computer. A URL for the
WSDL file’s location can be typed or even pasted
- From folder: Type in a location or click the browse (…) button to locate a folder containing the WSDL file.
(Note: The first WSDL file in the folder is used)
- DataSense:
Add DataSense methods: Provides methods to display metadata on the resource’s entities
Add DataSense Query Method: The method for DataSense Query Language is added.
Configure Maven and GitHub. (Place default details)
Select Background and Logo information and click finish.
Now Mule download the all required Jar's (Takes long time for first time) and create the project into current workspace.
pom.xml:
pom.xml contain all information regarding mule project dependency's list.
A default connector class will be generated after which the below annotations need to be configured.
- @Connector — For defining the connector details like name schema version
- @Connect — For providing the authentication using username and password
- @Configurable –To define the properties that are required to connect to the Connector
- @ValidateConnection — To check the connection status
- @Disconnect –For closing the connection
- @Processor – Actual operation is being defined here
- @RestCall –In case of connecting to rest implemented services using the attributes uri,method
ConnectorConfig.java contains all information reg. connection and security details.
We have to update following url's from api privided by client.
authorizationUrl = "https://devapi.olacabs.com/oauth2/authorize?response_type=token&client_id=<client_id>&redirect_uri=<partner's redirect_uri>&scope=profile%20booking",
accessTokenUrl = "https://api.myconnector.com/uas/oauth/accessToken" looks like this.
We have to update the @RestCallUrl in connector class as shown bellow.
After all the necessary changes are done, we have to build connector. and it must be success.
Now we have install connector by clicking
Install or Update option.
Once connector is installed successfully we can access the connector from palette .
Create new project and build flow that can access the custom connector.
In this project we have to build two flows.
1. OAuth Access Flow
2. Call Service(bookId by passing id's)
Thanks,
Maheswarareddy.