一个月前我创建了一个 JIRA 来请求这样的功能: http: //www.mulesoft.org/jira/browse/MULE-6877
现在,您可以使用一个技巧:一个带有事件生成器作业的 Quartz 入站端点, repeatCount = 0
它只会在启动时触发您的流程一次。
或者,您可以侦听上下文事件并在触发特定事件时调用流。下面显示了一个调用启动和关闭流的侦听器:
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;
}
}
配置:
<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>