Mulesoft Connector Devkit : Coding & Deployment

In my previous blog I explained configuration and setup for Mulesoft connector Devkit. In this blog I am going to explain how to write and deploy your connector. As I mentioned in my previous blog Devkit is a platform to develop Mulesoft connector. Devkit is very powerful tool to develop extreme complex connector or simple connector.

Here are few steps to develop Mulesoft connector.

1) Create project from anypoint studio

connector-project-create

2) Select SDK Based connector. This selection supports standalone java as well as REST based API. Once you select this selection below window will come. Name your connector project, select working directory and  then click next

connector-project-create-box

3)  Now next step you need to select maven Group Id and Artifact Id and click next.

4) Next step you need to select icon and logo for your connector then click finish.
connector-project-icon-select

After clicking finish connector project will generate.

Two java files are generated in your connector project. Here my project name is Vanrish, so it generated VanrishConnector.java and ConnectorConfig.java.

Generated VanrishConnector.java 


@Connector(name="vanrish", friendlyName="Vanrish")

public class VanrishConnector {
 
@Config
ConnectorConfig config;
 

In this code snippet annotation defines your connector name and display name. In above annotation “name” is for connector name and “friendlyName” will display connector name once you install this connector in Anypoint studio. This annotated class is main class for creating connector

In 2nd line we are initiating config class to add all configuration related with this connector.

If you are adding any method to execute this connector you need to define your method with  @Processor annotated method.


@Processor
public String getVehicleInfo(String deviceId) throws Exception {
return "Hello World"+deviceId;
}

Here is Full code snippet for this class


package org.mule.modules.vanrish;

import org.mule.api.annotations.Config;
import org.mule.api.annotations.Connector;
import org.mule.api.annotations.Processor;
import org.mule.api.annotations.lifecycle.Start;
import org.mule.api.annotations.oauth.OAuthProtected;
import org.mule.modules.vanrish.config.ConnectorConfig;

@Connector(name = "vanrish", friendlyName = "Vanrish")
public class VanrishConnector {

 @Config
 ConnectorConfig config;

 @Start
 public void init() {
 }

 public ConnectorConfig getConfig() {
  return config;
 }

 public void setConfig(ConnectorConfig config) {
  this.config = config;
 }

 @Processor
 public String getVehicleInfo(String deviceId) throws Exception {
  return "Hello World" + deviceId;
 }
}

Now in 2nd class we define connector configuration. This class is annotated with @Configuration

In this class I defined couple of methods to access external REST api for this connector.

I define apiURL and their version to use inside my annotated method


@Configurable
@Optional
@Default("https://platform.vanrish.com/api")
private String apiUrl;

@Configurable
@Optional
@Default("v1")
private String apiVersion;

Here are annotation definition for connector
@Configurable — Allow to configure this field
@Optional —This field is not mandatory
@Default —This is providing default value for field

Here is full code snippet


package org.mule.modules.vanrish.config;

import org.mule.api.annotations.components.Configuration;
import org.mule.api.annotations.Configurable;
import org.mule.api.annotations.param.Default;
import org.mule.api.annotations.param.Optional;

@Configuration(friendlyName = "Configuration")
public class ConnectorConfig {

/**

* Vanrish API Url

*/

 @Configurable
 @Optional
 @Default("https://platform.vanrish.com/api")
 private String apiUrl;

 @Configurable
 @Optional
 @Default("v1")
 private String apiVersion;

 public String getApiUrl() {
  return apiUrl;
 }

 public void setApiUrl(String apiUrl) {
  this.apiUrl = apiUrl;
 }

 public String getApiVersion() {
  return apiVersion;
 }

 public void setApiVersion(String apiVersion) {
  this.apiVersion = apiVersion;
 }
}

In advance connector writing you can create client java class and use above apiURL and version to access api method and execute to get result.

Now to build this project in Anypoint studio, you need to select project and right click. This action will pop up option window. Here in this window you need to select Anypoint Connector then click Build connector.

Steps —  Right Click on project –>Anypoint Connector –> Build Connector
Here it is shown in the picture below
connector-project-build

This action will build your connector.

Follow the same steps to install your connector into Anypoint studio.
Steps — Right Click on project –> Anypoint Connector –> Install or Update
This action will install your connector into Anypoint studio.

After installing your connector,you can search your connector name into Anypoint studio.

vanrish-connector

Connector Testing
you can create small flow in Anypoint studio and test your connecotor.

Here is example to test my connector
vanrish-connector-flow

Mulesoft Connector Devkit Setup

Mulesoft connector is the backbone of mulesoft flow. It receives or sends messages between Mule and one or more external sources, such as files, databases, or Web services. It also acts as message sources by working as inbound endpoints, they can act as a message processor that performs an operation in middle of a flow, or they can fall at the end of a flow and act as the recipient of the final payload data.

Mulesoft connectors are either endpoint-based or operation-based. Endpoint-based follow one-way or request-response exchange patterns. Operation-based connectors are based on information exchange pattern.

There are number of connector available in anypoint studio but some time the connector available is not able to satisfy company specific requirement. Own connector is a good option to explore whether we need a connector with a specific functionality or want to connect to a system without a pre-built connector.

If you are building your own connector you need to setup your development environment inside your mulesoft anypoint studio.

Make sure you enable maven configuration inside your anypoint studio and install Devkit for Mulesoft connector
1. Installation and Configuration of Maven 
Steps to enable Maven setting inside Anypoint studio.

a) Studio –>Help –> Install New Software

newsoftware

b)  In Available Software window

Work with –>Anypoint Addons Update Site

anypoint_workwith

c) Now you need to select Maven Tools for Mule

anypoint_maven

d)   Click Next button and Then Finish.

This will install maven plugin into your Anypoint studio

Validate your maven plugin installation

Got to Window –> Preferences
you can find maven link under Anypoint Studio tab

configure maven in your Anypoint studio

set maven installation directory and enter your maven command

anypoint_maven_prefrences

Click Test Maven configuration. You will get green check. Now you all set for maven configuration

2.   Installation of Anypoint Devkit Plugin
Steps to get Anypoint  DevKit Plugin

a)  Studio –>Help –>Install New Software

b)  In Available Software window Please select Anypoint DevKit Update site

anypoint_devkit_site

c) Now you need to select Anypoint Devkit Plugin

anypoint_devkit_site_select

d)  Click Next button and Then Finsh.

This will install Anypoint Devkit Plugin into your Anypoint studio

Validate your Devkit plugin installation

Go to  File — New in Anypoint studio

You will find two new options for Anypoint Connector

a) Anypoint Connector Project

b)Anypoint Connector Component

anypoint_devkit_install

Now you all set to develop your own connector.

In my next blog I will write how to build and deploy your own connector.