gpt4 book ai didi

java - 如何在启动 mule 时自动运行一次流程?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:12:03 28 4
gpt4 key购买 nike

我有一个 java 类,它创建一个带有种子集合的干净的 MongoDB 数据库。它会自动识别数据库是否丢失并创建它。我想在启动 MuleEsb 时运行它。这样我就不需要记住在启动 mule 之前调用它。我希望将它放在流中并在 mule 启动时自动运行该流一次。

有没有办法在mule启动的时候做这个一次性的操作?

---更新---

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

<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

现在,您可以使用一个技巧:a Quartz inbound endpoint with an event generator job 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>

关于java - 如何在启动 mule 时自动运行一次流程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17459888/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com