MuleSoft — Making of Custom Connector in Mule 4

Amlan Mahapatra
The Mule Blog
Published in
6 min readJun 14, 2020

Often in our projects we use any particular business logic or a rest or soap call in many mule applications ,so you have to configure those component each time. But you can’t reuse the same configurations or components. In this article, we will learn how to build a reusable components as a rest based connector very easily without a single line of java code.

In this tutorial, we will create two connectors. One of these will be used to call a third party api and another will be used to call other mule application’s endpoint.

If you have a basic idea of RAML, you can make it easily.

OK, lets start with creating our first connector which will call a third-party api.

Our third-party api is “https://cat-fact.herokuapp.com/facts/random” and we will pass two query parameters, one is animal_type and second is amount. For the api both are optional, but we will make animal_type query parameter as mandatory and amount as optional.

To create the specification of the third-party api, first login to Anypoint Platform, then go to Design Center. Then create a new specification and give a name, for me the name is CatDetails.

OK, the specification for third-party api is ready. Now just punish it in Exchange.

OK, now our first connector is ready.

Now, lets start with creating our second connector which will call an endpoint of cloudhub deployed mule application.

So first we will create a simple rest based mule application which will accept three mandatory parameters, one is operation and rest are two number.

We will use a http listener with default configuration and path will be /calculate. Using a switch we will decide the operation and performed the operation with two number.

We can see we are using three parameter — attributes.queryParams.”operator” for getting the operation type and attributes.queryParams.”a” and attributes.queryParams.”b" for getting two number.

After creating this first you can test with the postman rest client.

After testing successfully , we will deploy it to cloudhub.

After deploying the application only, we can find the App Url.

Now we can test the application using the App Url also

Now we will create the raml specification in Desing Center for hitting this /calculate api named calculate.

Now we will publish it to exchange.

Now we have only our two assets in the exchange.

OK, now our connectors are ready to use. Now the question is how to use them in our mule flow like other connector.

Let’s create an mule application which will use these connector.

First just login in your Anypoint account in Anypoint Studio.

Window ==> Preferences ==> Search for Authentication ==> Click on Authentication

Click on Add ==> Put your Anypoint platform username and password and login ==> Apply and Close.

Now create a mule application. We will use a scheduler which will trigger the flow and then we will use our custom connector.

To fetch the custom connector, we have to search it in the exchange by clicking “Search in Exchange”.

Then a window will open. First we will search for catDetails then select it from list of left side and click on add then we will search for calculate and you have to select and add it. Then click on finish.

Now we can see these connector are in the connector palette.

Now click on catDetails.

Look there is a child flow under the main connector, now go to catDetails.raml. You can see a endpoint /random and its display is random.

Now drag it into these canvas one by one.

Click on the “random” connector to configure it.

Now click on “+” to fill the configuration details. After clicking, you can see the configurations are already filled with required values. So don’t change it.

Now we will configure the outer settings.

We will put “dog” as Animal type and “2” as Amount. But Amount is optional, if we wish , we can remove it also.

Now we will go to the second custom connector calculate to provide its configuration. So click on “+” , to provide its configuration. Again we can see it comes with configuration data. So keep the default.

Now we will configure the outer configuration means the query parameters.

First we will select the Operator, it comes with drop down list because in the raml we used enum. And we will put 10 in A and 5 in B.

OK, our flow is ready to run. Now Just Run It.

We can see the two json objects are coming from the connector as a response which include two dog facts because we specify 2 as amount.

We can see the response as expected because we specify the “Division” as Operator and use “10” as A, “5” as B.

That’s it.

Suggestion :-

If you are thinking about to use these type of connector in your project, please keep some points in mind-

  1. First decide the functionality of the reusable connector.
  2. These type of connectors are synchronized.
  3. You can put the connector’s host in the calling application’s properties file. It will help you during the mulesoft environment change like — my second custom connector’s host is “simple-calculator.us-e2.cloudhub.io”, but when I move the underlying application to QA or PROD, then the host will change to “simple-calculator-qa.us-e2.cloudhub.io” or “simple-calculator-prod.us-e2.cloudhub.io”. So its better to pick from properties file. And most importantly it will help you to create a centralized configurable project like micro services.

Hope you enjoyed this article and this article helps you understand about custom connector and how to build and use it in our daily applications, will come back later with more interesting stuffs.

--

--

Amlan Mahapatra
The Mule Blog

Java| Spring-Hibernate | Certified Mulesoft Developer | Foodie | Nocturnal