call subflow with in mule3 webservice

前端 未结 2 567
傲寒
傲寒 2020-12-18 16:15

I created a Mule3 Simple Front-end Web Service which is supposed to pass the XML message to a sub-flow and returns success to its caller.

mule-config.xml


相关标签:
2条回答
  • 2020-12-18 16:27

    Invoking a sub-flow from code is no small feat but possible.

    Here is a test component that does invoke a sub-flow injected in it:

    public class TestComponent implements MuleContextAware, FlowConstructAware
    {
        private MuleContext muleContext;
        private FlowConstruct flowConstruct;
        private MessageProcessor subFlow;
    
        public void initialize() throws MuleException
        {
            muleContext.getRegistry().applyProcessorsAndLifecycle(subFlow);
        }
    
        public String test(final String requestMessage) throws Exception
        {
    
            final MuleEvent muleEvent = new DefaultMuleEvent(new DefaultMuleMessage(requestMessage, muleContext),
                MessageExchangePattern.REQUEST_RESPONSE, flowConstruct);
            final MuleEvent resultEvent = subFlow.process(muleEvent);
    
            return resultEvent.getMessageAsString();
        }
    
        public void setMuleContext(final MuleContext muleContext)
        {
            this.muleContext = muleContext;
        }
    
        public void setFlowConstruct(final FlowConstruct flowConstruct)
        {
            this.flowConstruct = flowConstruct;
        }
    
        public void setSubFlow(final MessageProcessor subFlow)
        {
            this.subFlow = subFlow;
        }
    }
    

    The component is configured this way:

    <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>
    
    0 讨论(0)
  • 2020-12-18 16:28

    I solved this problem by using singleton instance to store requestMessage with in test() method of WebserviceTestImpl class and retrieve it in TestSubflow component. I am not sure if it is perfect. but it works:)

    Here is how I did it..

    mule-config:


    <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 and TestSubflow code snippet


    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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题