如何在启动mule时自动运行一次流程? [英] How to run a flow once, automatically when starting mule?

查看:531
本文介绍了如何在启动mule时自动运行一次流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java类,它使用种子集合创建一个干净的MongoDB数据库。它会自动识别数据库是否缺失并创建它。我想在启动MuleEsb时运行它。这样我就不需要记得在我开始骡子之前调用它。我希望把它放在一个流程中,然后在骡子启动时自动运行一次流程。

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.

有没有办法在mule启动时进行一次性操作?

Is there a way to do this one-time operation when mule starts?

---更新---

根据下面的对话,我将以下内容添加到我的骡子配置并自动触发流程。

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>


推荐答案

我在一个月前创建了一个JIRA来请求这样的功能: http://www.mulesoft.org/jira/browse/MULE-6877

I created a JIRA a month ago to request such a feature: http://www.mulesoft.org/jira/browse/MULE-6877

现在,你可以使用一个技巧:具有事件生成器作业的Quartz入站端点 repeatCount = 0 将仅触发一次流启动。

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;
    }
}

配置:

<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>

这篇关于如何在启动mule时自动运行一次流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆