In any mule project, sharing data or configuration among multiple application is a common process. Almost every application needs interaction with external services or other mule applications running.
Mule comes with some connectors that can be used for same purpose.
Sharing common configurations/connectors/properties:
Sometime static data and connectors configurations are common for many applications. We can do it either replicate same into all applications or we can use domain projects for the same. Prior method has some problems, like same configurations are needs to be replicate, and it’s an error prone task, and in future if configurations changed we must have to modify all applications using these configurations. So, the recommended approach in this scenario is using domain project for this task.
We can create a domain project, and applications using same configurations can be created under same domain. All application under same domain can access to configuration defined into domain project.
Example:
Create Domain Project: It’s easy task and can be done as below
· Start your Anypoint Studio
Click on new -> Mule Domain Project.· Provide you domain name, and click Finish button
Your domain is created and now you can define some common configuration into it.
Define Common Configuration into Domain Project:
· Expand your newly created mule domain project, and go to src/main/domain directory
· Open mule-domain-config.xml, you can define common configurations here.
· For example, if you want to define common HTTP and Active MQ connectors, it would look like this
<?xmlversion="1.0" encoding="UTF-8"?>
<domain:mule-domain 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:spring="http://www.springframework.org/schema/beans" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:jms="http://www.mulesoft.org/schema/mule/jms" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.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://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-current.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd">
<jms:activemq-connector name="Active_MQ"
brokerURL="tcp://localhost:61616" validateConnections="true"doc:name="Active MQ" />
<http:listener-config name="HTTP_Listener_Configuration"
host="0.0.0.0" port="8081" basePath="/base"doc:name="HTTP Listener Configuration" />
</domain:mule-domain>
Make sure you have defined proper namespaces and schema locations into your xml.
Above defined Active_MQ connector and HTTP_Listener_Configuration can be used in any application under this domain.
Using Domain Configuration: For using you need to create a mule application and associate it with the domain.
· Click File ->New -> Mule Project to create mule project.
· Provide Mule Project Name and click Finish button
· Expend your newly created mule project
· Open mule-project.xml
· Look for the Domain, select your domain from dropdown and your mule project is now associated with domain project
· Open src/main/app/<your_mule_project_name>.xml
· Search for HTTP connector from palette and drop it on Canvas
· Select HTTP connector on your Canvas and open properties, there must be Connector Configuration drop down, and All HTTP connectors defined in Domain configuration suitable for your component will appear in drop down, you can select one.
· You can do the same for other configurations.
Sharing Static Properties: Like Mule Project, Mule Domain project does not read properties mule-app.properties file. If we want to use configurable properties in domain project we need to define
<context:property-placeholder location="domain-dev.properties" />
File name can be anything and we can pass multiple files list into location. All properties defined in these files, will be accessible in domain project as well as in all project under the same domain.
Sharing Data between two mule applications: For app to app interaction, we may need some mechanism to share data between two applications.
Mule provide us multiple way to achieve that. We can share data among all applications in same domain or between applications that are not attached to same domain.
1. Sharing data between two applications into same domain:
a. VM: It by default uses in-memory queues but can optionally be configured to use persistent queues. More details are available on Here. One application may write some messages using vm outbound endpoint and some other application can read from this queue using inbound endpoint(Source).
Note: VM file persistency does not work on clusters.
b. HTTP: This is standard approach; one application can send to any service and other application can get it from the service.
c. JMS: Two application can also communicate with the help of any messages queue. One application may write some data on JMS queue using outbound endpoint and another can consume messages using inbound endpoint.
2. Sharing Data between two applications into different domains or not attached to any domain:
a. HTTP
b. JMS
3. Sharing Data with external services:
a. HTTP
b. JMS
You can find more ways to achieve the same, but I discussed only above as these are widely used in industry.
No comments:
Post a Comment