I have a java class that creates a clean MongoDB database with seeded collections. It automatically identifies if the database is missing and creates it. I would like to run this when I start MuleEsb. This way I don't need to remember to invoke it before I start mule. I was hoping to put it inside a flow and run that flow once, automatically when mule starts up.
Is there a way to do this one-time operation when mule starts?
--- Update ---
As per the conversation below I added the following to my mule config and the flow is automatically triggered.
<quartz:connector name="Quartz" validateConnections="true"/>
<flow name="testService1">
<quartz:inbound-endpoint name="runOnce" repeatCount="0" repeatInterval="1" jobName="job1" connector-ref="Quartz">
<quartz:event-generator-job>
<quartz:payload>foo</quartz:payload>
</quartz:event-generator-job>
</quartz:inbound-endpoint>
<logger message="INBOUND HEADERS = #[headers:inbound:*]" level="WARN"/>
</flow>
To be able to start and stop flows in Mule 4. x you need to acquire the Mule Registry and then get the flow. This KB provides a way to stop and start Mule flow using a Groovy script and Java SDK connector in Mule 4.
Cron Expressions. Cron is a widely used standard for describing time and date information. The Scheduler keeps track of every second and creates a Mule event when the Quartz Cron expression matches your time-date setting. You can trigger the event just once or at regular intervals.
In the Package Explorer, expand the contents of your project's folder, then double-click to open the mule-project. xml file. Studio displays the contents of the file as fields in which you can change the values. Use the drop-down to select the Server Runtime you wish to use to run your Studio project.
I created a JIRA a month ago to request such a feature: http://www.mulesoft.org/jira/browse/MULE-6877
For now, you can use a trick: a Quartz inbound endpoint with an event generator job repeatCount = 0
that will trigger your flow only once at startup.
Alternatively, you can listen to context events and invoke a flow when a specific event is triggered. The following shows a listener that invokes a startup and a shutdown flow:
package com.acme;
import org.mule.DefaultMuleEvent;
import org.mule.DefaultMuleMessage;
import org.mule.MessageExchangePattern;
import org.mule.api.MuleException;
import org.mule.api.MuleRuntimeException;
import org.mule.api.context.notification.MuleContextNotificationListener;
import org.mule.config.i18n.MessageFactory;
import org.mule.construct.Flow;
import org.mule.context.notification.MuleContextNotification;
public class FlowInvokingContextListener implements MuleContextNotificationListener<MuleContextNotification>
{
private Flow startingFlow;
private Flow stoppingFlow;
public void onNotification(final MuleContextNotification notification)
{
if (notification.getAction() == MuleContextNotification.CONTEXT_STARTED)
{
sendNotificationToFlow(notification, startingFlow);
}
else if (notification.getAction() == MuleContextNotification.CONTEXT_STOPPING)
{
sendNotificationToFlow(notification, stoppingFlow);
}
}
private void sendNotificationToFlow(final MuleContextNotification notification, final Flow flow)
{
try
{
final DefaultMuleEvent event = new DefaultMuleEvent(new DefaultMuleMessage(notification,
notification.getMuleContext()), MessageExchangePattern.REQUEST_RESPONSE, startingFlow);
flow.process(event);
}
catch (final MuleException me)
{
throw new MuleRuntimeException(MessageFactory.createStaticMessage("Failed to invoke: "
+ startingFlow), me);
}
}
public void setStartingFlow(final Flow startingFlow)
{
this.startingFlow = startingFlow;
}
public void setStoppingFlow(final Flow stoppingFlow)
{
this.stoppingFlow = stoppingFlow;
}
}
Configured with:
<spring:beans>
<spring:bean name="flowInvokingContextListener"
class="com.acme.FlowInvokingContextListener"
p:startingFlow-ref="startFlow" p:stoppingFlow-ref="stopFlow" />
</spring:beans>
<notifications>
<notification event="CONTEXT" />
<notification-listener ref="flowInvokingContextListener" />
</notifications>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With