可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
<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>
sample webservice method
public class WebserviceTestImpl implements WebserviceTest,Serializable { @Override public String test(String requestMessage) throws Exception{ // sends XML message to a sub-flow return "success"; }
The issue is I do not find an mule api to call a sub-flow with in webservice method.
回答1:
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>
回答2:
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(); } }