【问题标题】:call subflow with in mule3 webservice在 mule3 webservice 中调用子流
【发布时间】:2026-02-07 10:35:01
【问题描述】:

我创建了一个 Mule3 简单前端 Web 服务,它应该将 XML 消息传递给子流并将成功返回给它的调用者。

mule-config.xml


<flow name="webserviceTestFlow">
   <http:inbound-endpoint address="${webservicetest.env.endpoint}" exchange-pattern="request-response" doc:name="HTTP"/>
  <cxf:simple-service serviceClass="com.test.WebserviceTest" doc:name="SOAP"/>
  <component class="com.test.WebserviceTestImpl" />
</flow>

示例网络服务方法


public class WebserviceTestImpl implements WebserviceTest,Serializable {
        @Override
    public String test(String requestMessage) throws Exception{
               // sends XML message to a sub-flow
               return "success";
        }

问题是我在 webservice 方法中找不到用于调用子流程的 mule api。

【问题讨论】:

  • 您希望requestMessage 包含什么内容? SOAP 信封或传递给test 方法的任何参数?我之所以问,是因为您说“将 XML 消息传递给子流”,但不清楚是什么 XML?
  • @DavidDossot 我的意思是,无论传递给测试方法

标签: mule


【解决方案1】:

从代码中调用sub-flow 是一项不小的壮举,但却是可能的。

这是一个测试组件,它调用了一个注入其中的sub-flow

公共类TestComponent实现MuleContextAware,FlowConstructAware { 私人骡上下文骡上下文; 私有 FlowConstruct 流构造; 私有 MessageProcessor 子流; public void initialize() 抛出 MuleException { muleContext.getRegistry().applyProcessorsAndLifecycle(subFlow); } 公共字符串测试(最终字符串请求消息)抛出异常 { final MuleEvent muleEvent = new DefaultMuleEvent(new DefaultMuleMessage(requestMessage, muleContext), MessageExchangePattern.REQUEST_RESPONSE, flowConstruct); 最终 MuleEvent resultEvent = subFlow.process(muleEvent); 返回结果事件.getMessageAsString(); } 公共无效 setMuleContext(最终 MuleContext muleContext) { this.muleContext = muleContext; } 公共无效setFlowConstruct(最终FlowConstruct flowConstruct) { this.flowConstruct = flowConstruct; } 公共无效 setSubFlow(最终 MessageProcessor 子流) { this.subFlow = subFlow; } }

组件是这样配置的:

<spring:beans>
    <spring:bean name="testComponent" class="com.example.TestComponent"
        p:subFlow-ref="testSubFlow" init-method="initialize" />
</spring:beans>

...

    <component>
        <spring-object bean="testComponent" />
    </component>

【讨论】:

【解决方案2】:

我通过使用单例实例将 requestMessage 存储在 WebserviceTestImpl 类的 test() 方法中并在 TestSubflow 组件中检索它来解决了这个问题。我不确定它是否完美。但它有效:)

这就是我的做法..

骡子配置:


<flow name="webserviceTestFlow">
  <http:inbound-endpoint address="${webservicetest.env.endpoint}" exchange-pattern="request-response" doc:name="HTTP"/>
  <cxf:simple-service serviceClass="com.test.WebserviceTest" doc:name="SOAP"/>
  <component class="com.test.WebserviceTestImpl" />
  <flow-ref name="testSubflow"/>
</flow>

<sub-flow name="testSubflow">
    <pooled-component class="com.test.TestSubflow">
        <pooling-profile exhaustedAction="WHEN_EXHAUSTED_FAIL" initialisationPolicy="INITIALISE_ALL" maxActive="1" maxIdle="2" maxWait="3" />
    </pooled-component>
</sub-flow>

WebserviceTestImpl 和 TestSubflow 代码 sn-p


public class WebserviceTestImpl implements WebserviceTest,Serializable {
    private static final long serialVersionUID = 1L;
    private TestMessageProxy testMessageProxy;

    public TriggerTestImpl() {
        this.testMessageProxy = TestMessageProxy.getInstance();
    }
    @Override
    public String test(String requestMessage) throws Exception{
        this.testMessageProxy.setTestMessage(requestMessage);
        return "success";
    }
}

public class TestSubflow  implements Callable{
    private TestMessageProxy testMessageProxy;
    public TestSubflow() {
        this.testMessageProxy = TestMessageProxy.getInstance();
    }

    @Override
    public Object onCall(MuleEventContext context) throws Exception {
        String  testMessage = this.testMessageProxy.getTestMessage();
    }
}

【讨论】: