Implementing and deploying Domain Project with MuleSoft

  • February 19, 2020

Introduction

Domain Project is used to create shared resources that can be reused across other projects that refer to domain projects.

Mule can define selected connectors as common resources and expose them to all apps deployed under the same domain. These resources are known as shared resources. To host them, you must create a Mule Domain Project and then reference it from each of the projects that use the elements in it. Once defined, any Mule app associated with a particular domain can access resources in this file.

NOTE: Mule apps can be associated with only one domain at a time.

Shared resources allow multiple development teams to work in parallel using the same set of reusable connectors. Defining these connectors as shared resources at the domain level allows the teams to:

  • Expose multiple services within the domain through the same port
  • Share the connection to persistent storage
  • Share services between apps through a well-defined interface
  • Ensure consistency between apps upon any changes because the configuration is set only in one place

To share the metadata, keep the Mule Domain Project open in Anypoint Studio. Otherwise, you must enter the metadata manually on each linked Mule projects.

Use case

Generally, when you deploy the application with HTTP Listener on Mule Standalone Runtime, each application must be deployed on a different port. Otherwise, you’ll get error.

To avoid this problem, you can implement the Domain Project and define the HTTP Listener connection with any port (for example, 8081) and that Domain Project can be referred across all other projects or applications so you can run all the applications on same port (for example, 8081).

Creating a Domain Project

To create a Domain Project using Anypoint Studio, go to File -> New -> Mule Domain Project.

Creating a Domain Project-1

Provide a Project Name, select Mule Runtime and click Finish.

Creating a Domain Project-2

Click Finish, and then define the HTTP Listener with port 8081 in mule-domain.config.xml.

Under Global Configuration Elements, select Create -> Connector Configuration -> HTTP Listener config.

HTTP Listener config-1

Under the HTTP Listener configuration, set the port to 8081 and click OK.

HTTP Listener config-2

mule-domain-config.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<domain:mule-domain

       xmlns:http=”http://www.mulesoft.org/schema/mule/http”

       xmlns=”http://www.mulesoft.org/schema/mule/core”

       xmlns:domain=”http://www.mulesoft.org/schema/mule/ee/domain”

       xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

       xmlns:doc=”http://www.mulesoft.org/schema/mule/documentation” xsi:schemaLocation=”

              http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd

              http://www.mulesoft.org/schema/mule/ee/domain http://www.mulesoft.org/schema/mule/ee/domain/current/mule-domain-ee.xsd

http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd”>

         <http:listener-config name=”HTTP_Listener_config” doc:name=”HTTP Listener config” doc:id=”713eacdb-c363-48a7-88e6-08191fd34541″ >

                   <http:listener-connection host=”0.0.0.0″ port=”8081″ />

         </http:listener-config>

    <!– configure here resource to be shared within the domain –>

</domain:mule-domain>

Building a Domain Project

To build a Domain Project, use the mvn clean install command.

Go to the path where the domain project exists on the command prompt and run the maven command to build it. It will generate a jar file in the target folder within your domain project — that is, common-domain-1.0.0-SNAPSHOT-mule-domain.jar.

Building a Domain Project

Deploying the Domain Project

To deploy a Domain Project, copy the jar file you built at $MULE_HOME/domains. As soon you copy the file to that location, it will automatically unzip the jar file.

Deploying the Domain Project

Before copying the jar file, you need to make sure your Mule Runtime is running.

Referring to the Mule Domain Project in a Mule application

To refer to a Domain Project in a Mule application, you need to make sure the project and the Mule application both exist in Anypoint Studio.

Right Click on Mule Application -> Properties -> Mule Project, select Domain Project, click Apply and click Close.

Referring to the Mule Domain Project in a Mule application

Now you can see the HTTP Listener configuration defined in the Domain Project, which can be used in Mule applications such as the hello-world-application.

hello-world-application

HTTP_Listener_config populates from the Domain Project. This is how you can refer a Domain Project to multiple Mule applications to reuse the HTTP Listener configuration for port 8081.

You need to make sure the Domain Project is deployed on Mule Runtime before deploying the Mule application. Otherwise, the deployment will fail.

Now you can run all your applications with HTTP Listener on port 8081 by referring to the Domain Project HTTP Listener configuration.

Running Mule Applications and Domain Project with Anypoint Studio

Once you start running the Mule application, the Domain Project will automatically start with the Mule application. You don’t need to start a Domain Project explicitly.

Running Mule Applications and Domain Project with Anypoint Studio

Now, you know how to implement and deploy Domain Project With MuleSoft.

— By Jitendra Bafna