Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Environment Aware Mule Project Template

DZone's Guide to

Environment Aware Mule Project Template

This tutorial contains instructions and a template environment to reuse for multiple projects, to save time without sacrificing project quality.

· Integration Zone
Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements.  What’s below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

Productivity comes from being effective, not repeating yourself. Laziness is also one of three great virtues of a programmer. Once you understand how to deploy Mule apps to multiple environments, there is no point in having to repeat yourself every time you create a new project. But, you also won't want to miss it out just for the sake of convenience as creating an environment aware project is always a best practice.

Then, how could you keep being lazy without compromising quality?

The answer is: a template. MuleSoft promotes reusability with its containerizable app architecture and AnyPoint Exchange. I've created an environment aware Mule project template that you can download and reuse from here.

Batteries Included

This project template includes minimum batteries for you to solid start straight away.

Separation of Concerns

There are three apps under src/main/app folder. businessLogic.xml, config.xml and errorHandling.xml. It is a good idea to separate concerns from the start. This helps you to focus on a particular one at a time and improves manageability.

  • businessLogic.xml: Business logic goes here. It already includes sampleFlow using the environment variable and custom global exception strategy as a reference.
  • config.xml: Global Mule configuration elements go here.
  • errorHandling.xml: Global custom exception strategy goes here.

Project Configuration for Multiple Environments

mule-project configuration

mule-project.xml is located in the root folder of the project.

development is configured to be a default target environment.

Imgur

It auto generates the mule-app.properties file as follows. mule-app.properties file is located under src/main/app folder.

ImgurNote: Some people suggest editing the mule-app.properties file directly, but I don't recommend it, as it is supposed to be auto-generated when you edit mule-project.xml. AnyPoint Studio is smart enough to pick the latest changes from either and copy them to another, but I feel this is hacky. Robustness requires some disciplines.

Resource Files

There are four environment properties files I've included. development, test, QA, and production. You can find this under the src/main/resources folder.

Within the resource files, I have preconfigured sample variables with comments, including naming convention.

# You can comment like this in property file. 

# Naming convention : {Connector}.{PropertyName}={YourOwnValue}   
# e.g. smtp.username=JSmith
# e.g. smtp.password=P@ssword

# Naming convention when value needs to be different per flow : {flowName}.{Connector}.{PropertyName}={YourOwnValue}
# e.g. sampleFlow.http.port=8888
# e.g. sampleFlow.http.port=8081

# 1. Uncomment following and update as per your needs
#{Connector}.{PropertyName}={YourOwnValue}
#{flowName}.{Connector}.{PropertyName}={YourOwnValue}
sampleFlow.http.port=8888

# 2. Update businessLogic.xml from src/main/app to use above when necessary
# For example,
# username="${smtp.username}"

# 3. Update value of mule.env from mule-project.xml to target specific environment. 
# By default within template, you have development, test, qa and production. Development is the initial value.   


Property Placeholder

As covered in Seperation of concerns, global Mule configuration elements are to be specified in config.xml. I have configured the Property Placeholder as follows. This configuration will pick up the ${mule.env} value from mule-project configuration and load the selected environment properties file accordingly.

Imgur

Global Exception Strategy

It is a good idea to create one or more global exception strategies and reuse them from flows. So, I have included a practice one here. With this template, you will start with one global exception strategy and reference to it.

How to Use the Template

Option 1: Create a Project by Importing Deployable Archive

Deployable Archive can be downloaded here. You can import this into AnyPoint Studio and change project name as you like.

Imgur

Option 2: Create a Project Using a Template From Exchange

You can also upload this template into Exchange and create a project from there. As of now, I can't demonstrate this, as Exchange 2.0 BETA doesn't support uploading a template yet. But, it's pretty straightforward once available.

Test

Without doing anything, businessLogic.xml already includes sampleFlow listening to port 8888. Thus, run the project and test a GET request to 127.0.0.1:8888.

Imgur

Summary

This template is intended to help create an environment aware project without having to write any code. I will keep adding/improving templates which I think might help the productivity of Mule developers.

The State of API Integration Report provides data from the Cloud Elements platform and will help all developers navigate the recent explosion of APIs and the implications of API integrations to work more efficiently in 2017 and beyond.

Topics:
mulesoft ,mule ,integration ,anypoint studio

Published at DZone with permission of Aaron Kim. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}