We are trying to use free jars of mule and host it in a standalone java web application (hosted on an in-house tomcat server). The web-application has some web-pages, apis. In one api call, we would like to call a mule flow with passed values. We are able to host the mule flow as a war, but when we try to package them with the existing web-app, we can not find how to call it. Sample simple flow is like this -
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<flow name="SampleFlow">
<logger message="===============Logging from Mule Flow =================== # [payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>
And we want to call this `SampleFlow` inside a java method so that when `executeFlow()` is called, the `logger` component prints the message with the given `inputValue` as a payload. What we were trying is like this -
import org.mule.api.MuleContext;
import org.mule.api.MuleException;
import org.mule.api.config.ConfigurationException;
import org.mule.api.lifecycle.InitialisationException;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextFactory;
public class ......
{
.....
public executeFlow(String inputValue)
{
String wsStatus = "Received";
....
DefaultMuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
SpringXmlConfigurationBuilder configBuilder;
MuleContext muleContext = null;
try {
configBuilder = new SpringXmlConfigurationBuilder("SampleFlow.xml");
muleContext = muleContextFactory.createMuleContext(configBuilder);
muleContext.start();
[Something would be here to call SampleFlow]
} catch (InitialisationException | ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MuleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
We do put the following lines in the `web.xml`.
<context-param>
<param-name>org.mule.config</param-name>
<param-value>http_test.xml</param-value>
</context-param>
<listener>
<listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>
</listener>
@plymouth_rock You need to get the flow name in your Java class :-
muleContext.getRegistry().lookupFlowConstruct("Your flow name");
Then you can call and process the flow in your java class:-
MuleEvent muleEvent= new DefaultMuleEvent(new DefaultMuleMessage(null,muleContext),MessageExchangePattern.REQUEST_RESPONSE,flowCon);
muleEvent.getMessage().setPayload("your payload");
response = flow.process(muleEvent);