Contact Us 1-800-596-4880

Best Practices to Build Mule Applications

As you build your applications, consider the following best practices for creating a clean, organized, and sustainable project. These practices include guidelines on using Anypoint Studio, avoiding certain app-building issues, and specific settings for using Mule runtime engine (Mule) components and connectors.

Anypoint Studio Best Practices

If you use Anypoint Studio to build Mule apps, consider the following practices:

  • Update to the latest version of Anypoint Studio.

  • Use the recommended Java version (OpenJDK) for the specific Studio version.

  • Include the Mule Maven plugin for automated deployment (executed by CI/CD processes).

Define a Project Structure

To help maintain a clean, structured, and readable project in Anypoint Studio, consider the following recommendations:

  • Create a separate XML file for global elements, for example, configuration elements.

  • Create a separate XML file for each use case or resource implementation.

  • Create separate XML files for common structures or logic.

  • Create different packages for resources such as DataWeave, WSDLs, examples, and so on.

Add Indentation and Formatting

As you build your app project in Anypoint Studio, consider the following points:

  • Add indentation and format to all your XML files (Mule XMLs, pom.xml, log4j2.xml, and so on) before committing to the source code repository.

  • Define a line width in your Anypoint Studio XML editor preferences, for example 140.d.

  • Define a naming convention.

Manage Properties per Environment

To avoid creating a separate set of variables for each environment, Mule 4 contains a built-in mechanism that enables you to set default values, which you manage by using Anypoint Runtime Manager. For details, see Introduction to Mule 4: Configuring Mule Apps and Configuring Properties.

App-Building Best Practice

As you build your Mule app, avoid packaging the configuration files for all the environments inside the application. Instead, use a .yaml file to provide default properties, and use Anypoint Runtime Manager to override each of these properties at deployment time.

Components and Connectors Best Practices

When building your apps using Mule components and connectors, consider the following recommendations.

APIkit

When using APIkit, consider these practices:

  • Use APIkit features to generate flows automatically using your API specification.

  • Use APIkit Exception Strategy to catch connector-related exceptions.

DataWeave

When using DataWeave for building your app, consider these recommendations:

  • Use inline scripts during the development phase.

  • To have a clean XML and reusable scripts, pass the inline scripts to an external file.

Flows

When creating flows for your app, consider the following practices:

  • Have a clear flow diagram showing the steps of the use case.

  • Use a Flow Reference to separate and reuse common logic.

File Connector

If you use Anypoint Connector for File (File Connector) and share part of the path on numerous reads or any other file operations, extract the base path into a file configuration to reuse it:

<file:config name="File_Config" >
   <file:connection workingDir="/Example/path" />
</file:config>

<flow>
   <file:read config-ref="File_Config" path="manual.txt" />
   <file:read config-ref="File_Config" path="readme.txt" />
</flow>

HTTP and HTTPS

For HTTP and HTTPS ports in on-premises deployments, MuleSoft recommends using the names for property placeholders that are reserved for CloudHub deployments. The shared load balancer in CloudHub uses the defaults for these properties.

  • In CloudHub, ${http.port} maps to the setting port=8081, and ${https.port} maps to port=8082.

  • For internal ports to HTTP services in CloudHub deployments, ${http.private.port} maps to the setting port=8091, and ${https.private.port} maps to port=8092.

For details about these settings in CloudHub, see CloudHub Reserved Properties and CloudHub Networking Guide.

Apache Kafka Connector

If you use Anypoint Connector for Apache Kafka (Apache Kafka Connector) perform the following actions:

  • Use the .properties file to add bootstrap server configuration.

  • Add property placeholders, either in a .yaml or a .properties file, and use the file in the configuration:

config.bootstrapServers={your Kafka Server address}
config.consumerPropertiesFile=consumer.properties
config.producerPropertiesFile=producer.properties
# Consumer specific information
consumer.topic=one-replica
consumer.topic.partitions=1
  • Apache Kafka Connector provides various settings for producer and consumer properties, add your own settings in consumer.properties and producer.properties under src/main/resource:

<configuration-properties file="mule-app.properties"/>