Accelerate Development on Anypoint Code Builder using Code Snippet

Anandasankar Joardar
Another Integration Blog
6 min readApr 19, 2023

Configure connector and message event processor using Mule-XML JSON file

Introduction:

Anypoint Code Builder is the next generation IDE released by MuleSoft for developers to design, develop and deploy APIs, Integrations and automations from a single environment. The IDE is powered by Visual Studio Code and (at the time of writing this blog) available as Web IDE. Desktop version of the IDE will be available down the line. Few advantages of using Anypoint Code Builder over Anypoint Studio is as follow —

  1. Using Anypoint Code Builder — developers and designers can design API specification (RAML and OAS both are supported), Mock test API specification, Implement API, Create Integration, Locally test application, debug integration and finally Deploy to CloudHub (at the time of writing this blog Anypoint Code Builder only support CloudHub 1.0 and MuleSoft Runtime 4.4.0) — from a single IDE.
  2. Anypoint Code Builder IDE (MuleSoft extensions plugged into Visual Studio Code) is managed and maintained by MuleSoft
  3. The Web based IDE eliminates the overhead of desktop installation and need for locally managing the IDE.

However there is one more feature introduced in Anypoint Code Builder that is clearly going to make ACB as the preferred IDE for large MuleSoft implementation and maintenance engagements. This is the Code Snippet feature available in MuleSoft Anypoint Code Builder and can be used to maintain common standards for configuring connectors and event processors across the development team. This will also eliminate the need for writing connection and event processing components from scratch for every development need. This will expedite the development effort and help the development team to deliver the implementation in less time.

Configure Centrally and Implement locally:

Code Snippet in Anypoint Code Builder — ensure common configuration standards across development team

As the above diagram depicts how using common code snippets can ensure the same configuration standard to be maintained across all MuleSoft development as well as expedite the development effort using Anypoint Code Builder as the IDE.

  1. MuleSoft Architect or C4E (Center for Enablement) team can create the code snippet that contains all configuration standards for connector and for message event processor for MuleSoft implementation. Keep the code snippet file will be published on a shared location (can be published as a Custom asset on Exchange) that is accessible by all developers.
  2. Developers from their Anypoint Code Builder will navigate to File> Preferences> Configure User Snippets
Open Configuration User Snippet from Anypoint Code Builder

3. Then the Developer will select the mule-xml.json file and open the file.

Open mule-xml.json file

4. For the first time the mule-xml.json file will look like below.

mule-xml.json file content by default

5. Developer will replace their local mule-xml.json file content with the global or common mule-xml.json file content (delivered by C4E team)

Sample content from mule-xml.json file

6. While developing the code, instead of defining the message event processor (or Connectors) from scratch , developers select the configuration for that component from the mule-xml.json file. For example in below screenshot Logger component is being selected to be configure from mule-xml.json file

Define logger component in the MuleSoft flow from mule-xml.json file

7. Selected message event processor or connector (or flow) definition will be auto populated from the mule-xml.json file. Developers only need to update the configuration further as per the specific need.

Logger component got auto-defined in the flow

8. So developers need not to write the common configurations (which is the same across all development) for the event processor, connector or flow again and again. This will expedite the overall development activity.

A Closer Look to the Code Snippet Structure:

Let us now take a closer look at the structure of the code snippet. Let’s consider the HTTP Listener Configuration code snippet as an example for the rest of the explanation.

"HTTP Listener Config": {
"prefix": "muledx:http-listener-config",
"body": [
"<http:listener-config name=\"${1:httpListenerConfig}\" doc:name=\"${2:Listener Config}\" doc:id=\"$RANDOM_HEX-$RANDOM_HEX\">",
" <http:listener-connection host=\"${3:Host}\" port=\"${4:Port}\"/>",
"</http:listener-config>"
]
}

The “HTTP Listener Config” is the part that is used to trigger the code snippet to be activated in the MuleSoft code during implementation. If the developer type “HTTP Listener Config” on the code then on the pop-up list the value defined for “prefix” will be available as an option for the developer to select. So in this scenario developer will see a pop-up with an option to select “muledx:http-listener-config”.

On selection of that option “HTTP Listener Config” will be replaced with the value defined for the key “body”. The place holders defined inside the code snippet definition like “${4:Port}” needs to be replaced with the actual value relevant for that specific application by the developer. For example “${4:Port}” needs to be replaced with the actual port value on which the application is supposed to listen (e.g. — 8081) the incoming traffics. Below is an example of how the HTTP Listener Configuration will be replaced with the defined code snippet. On the other hand $RANDOM_HEX will be replaced with a hexadecimal value inside the actual MuleSoft application code to generate a unique Id value for the event processor or connector (for this example HTTP Listener configuration).

<http:listener-config name="httpListenerConfig" doc:name="Listener Config" doc:id="1811cb-796144">
<http:listener-connection host="Host" port="Port"/>
</http:listener-config>

So all such configuration parameters intended to be defined by the developer locally on the code itself need to be defined on the code snippet with following conventions.

  • enclosed with double quote
  • each quote is escaped with backslash (\)
  • Start with $ and enclosed with curly braces ({})
  • Put a sequential Number (for example — in this code snippet Port is the 4th placeholder inside the “body” JSON key element value of “HTTP Listener Config” snippet, so it started with 4).
  • Put the placeholder name that will be visible to the developer inside the code s/he is implementing —
  • So the portion in bold is the convention for the placeholder for the Port parameter for HTTP listener port=\”${4:Port}\”

Advantages:

The benefits of code snippets feature are of many folds. Some of the key advantages are as follow —

  1. No need for the developers to re-write the same code and configuration from scratch again and again — this will expedite the development and reduce the implementation cycle
  2. Code quality and standards across multiple developers can be controlled centrally
  3. Configuration strategy for connectors and event processors will be globally uniform for all development across the board. For example C4E can define a retry strategy for all HTTP connectors and keep it the same across all implementations. They can define the strategy for the HTTP Connection configuration in the global code snippet. All developers are going to copy the same Global code snippet locally from their individual Anypoint Code Builder. Because all local code snippets will be inherited from the same global code snippet so very easily the same reconnection strategy will be enforced in every development.
  4. Ease of enforcing standard will in turn reduce the review effort

Conclusion:

At the time of writing this blog Anypoint Code Builder is in beta version and the blog is written based on the current capacity of the code snippet feature. However in the General Availability (GA) phase of Anypoint Code Builder this code snippet feature (like many other features) will be more enhanced and matured. The sole intention of this blog is to highlight this feature of Anypoint Code Builder and how this feature can be utilized to create a strategy to enforce coding standards easily and how can the development effort expedite to reduce the overall go to market timeline.

If you are interested to view the demo of Code Snippet feature in Anypoint Code Builder then watch the YouTube Video

For demo of Code Snippet feature for Snowflake Connector in Anypoint Code Builder then watch the YouTube Video —

--

--

Anandasankar Joardar
Another Integration Blog

MuleSoft Ambassador and Delivery Champion, YouTuber, Blogger and Speaker, An Integration Architect