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

Access Shared Flow in Mule 4

DZone's Guide to

Access Shared Flow in Mule 4

Look at a Mule tutorial.

· Integration Zone ·
Free Resource

Hello Friends, let's discuss Mule 4.

Scenario:

1: Moving on COE approach, we want to reuse resources like muleFlow which is developed in another project.

2: Project needs to deploy on cloudHub.

Solution:

1: Project needs to be developed as an independent project/module as domain project doesn't work in cloudHub deployment.

2: Instead of associating complete one project to another project, it will increase the size of the required container/runtime.

Let me demonstrate the demo below.

Point 1: Developed two projects: one project [sharedproject] and another one [mainproject].

Image title

Image title

Point 2: Some flows have been developed in [sharedproject] project, which needs to be used in multiple places in the project and even in multiple projects.

Point 3: Now point to how to use external project flows in the required project.

Steps:

  1. Go to the below location: ***\sharedproject\src\main\mule
  2. Execute command to create jar package with requiredfiles.
  3. *command* jar cf sharedproject.jar sharedproject.xml

Now, we can see jar file created at the same location.

Name of jar file: sharedproject.jar

4. Now target to install jar in local maven respository to refer as dependency in the required project and reuse flow.

Execute the below command to install mavenized jar in local maven respository.

mvn install:install-file -Dfile=sharedproject.jar -DgroupId=com.mulesoft.kelltontech -DartifactId=kellton -Dversion=1.0 -Dpackaging=jar

5. After successful execution of the above command, we can see in our local repository that the dependency is installed.

Image title

6. Now configure dependencies in [mainproject] in order to re-use the flow, which we have packages [sharedproject.jar] jar.

<dependency>
<groupId>com.mulesoft.kelltontech</groupId>
<artifactId>kellton</artifactId>
<version>1.0</version>
</dependency>

7. Re-use [sharedproject] projects flow now in [mainproject] flow.

mainproject as below:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/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">
<import doc:name="Import" doc:id="cd59450e-6bf2-4ef0-bd32-49ca35586576" file="sharedproject.xml" />
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="e59ffbcf-61a5-4d3b-8082-e0b2b2bd28b2" >
<http:listener-connection host="0.0.0.0" port="8081" />
</http:listener-config>
<flow name="mainprojecyFlow" doc:id="58dffd8f-e4ec-4d6c-81a1-e26399252977" >
<http:listener doc:name="Listener" doc:id="d41e2a20-a63f-48fe-9843-6f6409d61b00" config-ref="HTTP_Listener_config" path="/mainSource"/>
<flow-ref doc:name="Flow Reference" doc:id="bc8f37e3-ee6f-4cbf-b924-ef4a42c8e6d5" name="shared_Flow_One"/>
<vm:publish queueName="sharedQueue" doc:name="Publish" doc:id="7da4e175-84e4-424e-970c-236f86ee7a16" config-ref="VM_Config"/>
</flow>
</mule>

sharedproject project as below:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd">
<vm:config name="VM_Config" doc:name="VM Config" doc:id="b4ed840e-1246-49bd-8a9b-4927751b4f6d" >
<vm:queues >
<vm:queue queueName="sharedQueue" />
</vm:queues>
</vm:config>
<sub-flow name="shared_Flow_One" doc:id="886571e0-3393-418b-b71c-3073ca4e12a3" >
<logger level="INFO" doc:name="Logger" doc:id="422b5a4e-9004-4cf7-b257-6ad4bc3445b7" message='#["shared_Flow_One"]'/>
</sub-flow>
<flow name="vmSubfloShared" doc:id="9652ccba-46ba-40fa-b5d5-2643a8f22566" >
<vm:listener queueName="sharedQueue" doc:name="Listener" doc:id="b97e542e-ebcf-4167-bd32-dc11d7253215" config-ref="VM_Config"/>
<logger level="INFO" doc:name="Logger" doc:id="20c45f9a-7cbf-4a47-b3f6-3c335816c7dc" message="This is Vm consumer log : #[payload]"/>
</flow>
</mule>

In the above code, we can see subflow {shared_Flow_One} and VM {queueName="sharedQueue"}, reused in main flow.

Let test main project here:

Image title

INFO  2018-12-13 00:29:33,732 [[MuleRuntime].cpuLight.02: [mainproject].mainprojecyFlow.CPU_LITE @64ba1d35] [event: 0-0ea11580-fe40-11e8-970c-36f64bf39774] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: shared_Flow_One
INFO  2018-12-13 00:29:33,990 [[MuleRuntime].cpuLight.04: [mainproject].vmSubfloShared.CPU_LITE @6ce92ff8] [event: 0-0ea11580-fe40-11e8-970c-36f64bf39774] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: This is Vm consumer log : {
"Name" : "Test Validate"
}

It is quite strange, but yes, this is working.

Happy Mule!

Build and deploy API integrations 7x faster. Try the Cloud Elements 100% RESTful platform for 30 days free. Access your trial here.

Topics:
mule 4 ,mulesoft integration ,mulesoft ,tutorial ,integration tutorial ,mule tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}