Loading
Salesforce now sends email only from verified domains. Read More

How to run a flow once in when an application starts in Mule4

Publish Date: Aug 4, 2025
Task

GOAL

There is a need to run a flow just once when an application starts.
Steps

To run a flow automatically on a predetermined schedule, it is necessary to use the Scheduler component. You can read about this component in the documentation; https://docs.mulesoft.com/mule-runtime/4.1/scheduler-concept.

While this component has no concept of a "single run", it is possible to emulate this behaviour with the following options:
  • By using a very long frequency for the scheduling strategy. For example, setting the frequency to 10,000 days will effectively give a single run of the flow at application startup. - Please note this Schedule will not run again on an application restart. The application will need to be re-deployed for the single time run to execute again.
  • By using a Groovy script component at the end of the flow which stops the flow after completion.
Below is an example using the two options above combined:
<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
    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/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
    <flow name="singleRunAtStartupFlow" doc:id="3e1a1585-7a33-4c16-ab4c-1415bfc3515c" >
        <scheduler doc:name="Scheduler" doc:id="907d3d0b-ecc6-4d87-a364-3e6ef3829849" >
            <scheduling-strategy >
                <fixed-frequency frequency="10000" timeUnit="DAYS"/>
            </scheduling-strategy>
        </scheduler>
        <logger level="INFO" doc:name="Logger" doc:id="541e1a54-a4ef-4bee-b89a-cd35db033473" message="Perform some actions"/>
        <scripting:execute engine="groovy" doc:name="Stop flow after completion" doc:id="1ea52b1b-f728-4e1a-8619-78180c63cb29" >
            <scripting:code >flow = registry.lookupByName(&quot;singleRunAtStartupFlow&quot;).get();
if (flow.isStarted())
  flow.stop()</scripting:code>
        </scripting:execute>
    </flow>
</mule>
Knowledge Article Number

001114475

 
Loading
Salesforce Help | Article