How to make parallel outbound calls - java

My use case requires to enrich my input with smne data and send it to an outbound endpoint.
The data for enriching is obtained by making calls to two web-services and then extract the data from the reply.
This extracted data is enriched into my input XML and sent to an outbound endpoint.
The two web-service calls that I need to make needs to be parallel, as they don't have dependency on another. This way I could save my processing time.
Please suggest how I could achieve this parallel processing in a flow in Mule.
Note: I have tried using ALL flow control, but it seems that is calling the web-services (sub-flows) sequentially.
Given below is my abstract flow.
<flow name="mainFlow">
<inbound-endpoint> .....
<some validation>
<setting some flow variables>
<!-- Now make calls to the sub-flows which has some processing of the input and make some web-service calls -->
<all>
<flow-ref name="myFlow1" />
<flow-ref name="myFlow2" />
<flow-ref name="myFlow3" />
</all>
<enrich the input with the data obtained from the output of the above three flows>
<outbound-endpoint>
</flow>
<flow name="myFlow1">
<some transformer to transform the payload provided >
< the tran sformed payload is passed as input to the web-service call>
<http:outbound-endpoint ...>
<transform the reply from the web-service call>
</flow>
<flow name="myFlow2">
<some transformer to transform the payload provided >
< the tran sformed payload is passed as input to the web-service call>
<http:outbound-endpoint ...>
<transform the reply from the web-service call>
</flow>
<flow name="myFlow3">
<some transformer to transform the payload provided to it>
< the tran sformed payload is passed as input to the web-service call>
<http:outbound-endpoint ...>
<transform the reply from the web-service call>
</flow>

Here is a simple configuration that shows one way to make a fork/join with two HTTP outbound endpoints. To add a third endpoint, set MULE_CORRELATION_GROUP_SIZE to 3 and the MULE_CORRELATION_SEQUENCE of the third async flow-ref to 3.
<flow name="fork">
<vm:inbound-endpoint path="fork.in" />
<set-property propertyName="MULE_CORRELATION_GROUP_SIZE"
value="2" />
<all enableCorrelation="IF_NOT_SET">
<async>
<set-property propertyName="MULE_CORRELATION_SEQUENCE"
value="1" />
<flow-ref name="parallel1" />
</async>
<async>
<set-property propertyName="MULE_CORRELATION_SEQUENCE"
value="2" />
<flow-ref name="parallel2" />
</async>
</all>
</flow>
<sub-flow name="parallel1">
<logger level="INFO" message="parallel1: processing started" />
<http:outbound-endpoint address="..."
exchange-pattern="request-response" />
<logger level="INFO" message="parallel1: processing finished" />
<flow-ref name="join" />
</sub-flow>
<sub-flow name="parallel2">
<logger level="INFO" message="parallel2: processing started" />
<http:outbound-endpoint address="..."
exchange-pattern="request-response" />
<logger level="INFO" message="parallel2: processing finished" />
<flow-ref name="join" />
</sub-flow>
<sub-flow name="join">
<collection-aggregator timeout="6000"
failOnTimeout="true" />
<combine-collections-transformer />
<logger level="INFO"
message="Continuing processing of: #[message.payloadAs(java.lang.String)]" />
</sub-flow>
EDIT: In the above config, the aggregator times out after 6 seconds. This is potentially too short for your actual use case: increase as you see fit. Also it is set to fail on time-out, which is maybe not the behaviour you desire in case not all the outbound HTTP endpoint interactions succeeded: it's up to you to decide based on your use case.

Related

Mule Standalone 3.8.1 ClassDefNotFoundError when sending a stream of HTTP requests with big payload at high concurrency

I'm pretty new to MuleESB. I'm using Mule Standalone 3.8.1 with a configuration to filter HTTP requests using the xpath3() function. The configuration I have used is as follows.
<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" />
<http:request-config name="HTTP_Request_Configuration" host="example.com" port="8081" basePath="basePath" responseTimeout="120000" />
<custom-transformer name="XML_Transformer" class="org.mule.module.xml.transformer.XmlToOutputHandler"/>
<message-properties-transformer name="Response_Error_Transformer">
<add-message-property key="http.status" value="500"/>
<add-message-property key="Content-Type" value="text/xml" />
</message-properties-transformer>
<message-properties-transformer name="Response_Transformer">
<add-message-property key="Content-Type" value="text/xml" />
</message-properties-transformer>
<set-payload name="Create_Error_Response" value="
<PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20">
<fault>
<code>500</code>
<message>#[flowVars['errorMessage']]</message>
</fault>
</PurchaseOrder>" />
<flow name="Choice_Flow">
<http:listener config-ref="HTTP_Listener_Configuration" path="productFilter" />
<choice>
<when expression="#[xpath3('//Item[1]/ProductName') == 'Laptop']">
<transformer ref="XML_Transformer"/>
<http:request config-ref="HTTP_Request_Configuration" path="EchoService" method="POST" />
<flow-ref name="Response_Transformer" />
</when>
<otherwise>
<flow-ref name="Response_Error_Transformer" />
<set-variable variableName="errorMessage" value="First item must be a Laptop" />
<flow-ref name="Create_Error_Response" />
</otherwise>
</choice>
</flow>
When I'm sending HTTP requests at 10 threads it's working fine. But, then when I tried sending requests at 500 threads, some of the requests got failed. The request is an XML with 2500 Item elements. I don't see any descriptive errors in the logs or anywhere, but the requests got failed. After this happens, some of the requests got failed even for small thread numbers, and I couldn't find any error in files in the logs directory. Only error I see in the mule.log file is as follows. This is written at the first occurrence of this error. Then, simply, nothing.
Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "[default].http.listener(6)"
Can someone please explain me what's going on here? How to do this without any error? Is something wrong with my configuration?

Mule vm:inbound-endpoint not triggered

I have a list which I want to process in paraller, so after calling collection-splitter, I post each record to an outbound-endpoint, but the receiving inbound-endpoint is not been triggered.
Configuration below;
<flow name="ProcessPolledOrders" doc:name="ProcessPolledOrders">
<set-session-variable variableName="fileName" value="#[message.payload.orderBatch.orderSourceName]" />
<set-session-variable variableName="batchId" value="#[message.payload.orderBatch.id]" />
<logger message="************* Item: #[sessionVars.fileName] processing resumed. ***********" level="INFO"/>
<component>
<spring-object bean="PolledOrderSplitter"/>
</component>
<set-session-variable variableName="size" value="#[message.payload.size()"/>
<collection-splitter/>
<set-property propertyName="MULE_CORRELATION_GROUP_SIZE" value="#[sessionVars.size]" />
<set-property propertyName="MULE_CORRELATION_ID" value="#[sessionVars.fileName]" />
<choice>
<when>
<payload-type-filter expectedType="model.Order"/>
<vm:outbound-endpoint exchange-pattern="one-way" path="step2" doc:name="VM"/>
</when>
<otherwise>
<echo-component/>
</otherwise>
</choice>
</flow>
<flow name="PollerOrderProcessingFlow" doc:name="PollerOrderProcessingFlow">
<vm:inbound-endpoint exchange-pattern="one-way" path="step2" doc:name="VM"/>
<logger message="***************** Validating order with id #[message.payload.id] ********************"
level="INFO"/>
<component>
<spring-object bean="PolledOrderValidationComponent"/>
</component>
<collection-aggregator timeout="600000" failOnTimeout="false"
doc:name="Validated Orders Aggregator"/>
<component>
<spring-object bean="BatchOrderUpdateComponent"/>
</component>
<default-exception-strategy>
<flow-ref name="ValidationProcessingExceptionFlow" />
</default-exception-strategy>
</flow>
Are you getting any exception? try to debug and see where the message dropping? I tried similar flow and working fine for me.

Using Mule Anypoint, I want to achieve functionality without flow-ref

I want to write a java file to control the business flow of mule message,
I do not want to use flow-ref module.
How can I achieve it? Below is my mule xml segment.
<flow name="gtrfsettleFlow">
<wmq:inbound-endpoint queue="PINOSAY" doc:name="WMQ"/>
<component class="com.hsbc.gtrf.settle.esb.Rout" doc:name="Java"/>
<flow-ref name="flow2" metadata:id="0ec4db90-8b8c-476b-8186-cfc6b015435c" doc:name="re"/>
<logger level="INFO" doc:name="Logger"/>
</flow>
<sub-flow name="flow1">
<wmq:outbound-endpoint queue="PINOSAY" doc:name="WMQ" connector-ref="WMQ"/>
<logger message="this is flow1" level="INFO" doc:name="Logger"/>
</sub-flow>
<sub-flow name="flow2">
<logger message="now is flow 2" level="INFO" doc:name="Copy_of_Logger"/>
</sub-flow>
You can develop java class implementing Callable interface. Where you can access MuleContext (Event, MuleMessage, Payload and properties). Please refer following link for more details
https://docs.mulesoft.com/mule-user-guide/v/3.8/developing-components#customizing-the-message-flow-behavior

HTTP SOAP Post Request

I need to send HTTP Request to publish some data in a WS
E.g:
http://localhost:8081/hello/publishAMANSequence/filter/sequenceGenerationTime=1696-09-01T00:00:00Z&AMANId=B1&landingSequenceEntry=11234567890EST
I take this fault from server:
Parameter should be ordered in the following sequence: [sequenceGenerationTime, AMANId, landingSequenceEntry]
I'm doing something wrong in the order?
mule flow:
<jms:activemq-connector name="Active_MQ1" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<flow name="jmsFlow1" doc:name="jmsFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="hello" doc:name="HTTP"/>
<cxf:jaxws-service doc:name="SOAP" serviceClass="aero.itec.amansequenceservice.AMANSequenceInfo"/>
<component doc:name="Java" class="implementations.AMANSequenceImpl"/>
<mulexml:object-to-xml-transformer doc:name="Object to XML"/>
<jms:outbound-endpoint queue="StudioIN" connector-ref="Active_MQ1" doc:name="JMS"/>
<logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
</flow>
The url which you have provided is wrong. There should be a questionmark(?) after "filter". Then only it will consider it as parameters
http://localhost:8081/hello/publishAMANSequence/filter?sequenceGenerationTime=1696-09-01T00:00:00Z&AMANId=B1&landingSequenceEntry=11234567890EST
Moreover if you are trying to access a webservice you can't do that with a HTTP GET. You need to send it as a SOAP request. You can use APIs like CXF, AXIS etc.
Following this tutorial ~
I create a flow for publish the WS , that have an inbound-endpoint where I do the request
publish.flow:
<jms:activemq-connector name="Active_MQ1" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/>
<flow name="jmsFlow1" doc:name="jmsFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" path="hello" doc:name="HTTP"/>
<cxf:jaxws-service doc:name="SOAP" serviceClass="aero.itec.amansequenceservice.AMANSequenceInfo" >
<cxf:jaxb-databinding/>
<cxf:inInterceptors>
<spring:bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</cxf:inInterceptors>
<cxf:outInterceptors>
<spring:bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</cxf:outInterceptors>
</cxf:jaxws-service>
<component doc:name="Java" class="implementations.AMANSequenceImpl"/>
<object-to-string-transformer doc:name="Object to String"/>
<jms:outbound-endpoint queue="StudioIN" connector-ref="Active_MQ1" doc:name="JMS"/>
<logger message="#[message.payload]" level="INFO" doc:name="Logger"/>
</flow>
Then I create a client.class where I set the variable values:
client.java
public class AMANwsClient extends AbstractTransformer{
#Override
protected Object doTransform(Object src, String enc)
throws TransformerException {
AMANSequence sequence = new AMANSequence();
XMLGregorianCalendar fec;
sequence.setSequenceGenerationTime(fec);
sequence.setAMANId("AA");
System.out.println(sequence);
return sequence;
}
This class is used like a transformer, we don't need to pass parameters in the url, only need to connect to the endpoint URL
Finally, create a client.flow
<custom-transformer class="implementations.AMANwsClient" name="AMANwsClient" />
<flow name="csvPublisher">
<transformer ref="AMANwsClient" />
<object-to-string-transformer doc:name="Object to String"/>
<outbound-endpoint address="http://localhost:63081/hello" exchange-pattern="request-response">
<cxf:jaxws-client clientClass="aero.itec.amansequenceservice.AMANSequenceInfo_Service" port="AMANSequenceInfoService" operation="publishAMANSequence">
<cxf:inInterceptors>
<spring:bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</cxf:inInterceptors>
<cxf:outInterceptors>
<spring:bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</cxf:outInterceptors>
</cxf:jaxws-client>
</outbound-endpoint>
</flow>
Now I can save the payload into a JMS Queue and reproduce the payload from Browser, console and JMS.
If you have some suggestion to improve program performance, I'm open to listen to it.
Put http://localhost:8081/hello?wsdl in SOAPUI ... It will create the request and response there ... then you can pass the values in the request and invoke the webservice ...
Please check the following for your reference :-
http://developers-blog.org/blog/default/Webservice-testing-with-soapUI and http://quicksoftwaretesting.com/soapui-web-service-testing-tool/

How to filter a single URI from a group of URI's in a Mule inbound endpoint?

I have the following code to configure a Jersey service at "http://localhost:8080/alpha":
*** my mule config ***
<flow name="flow1">
<inbound-endpoint address="http://localhost:8080/" exchange-pattern="request-response" />
<jersey:resources>
<component>
<singleton-object class="com.address.Flow1Resource"/>
</component>
</jersey:resources>
</flow>
*** Flow1Resource.java ***
#Path("/alpha")
public class Flow1Resource {...}
I want to add a new inbound-endpoint that handles all the addresses under "http://localhost:8080" except "http://localhost:8080/alpha" (e.g. "http://localhost:8080/beta"). These new addresses need a single jersey resource. For example:
*** my mule config ***
<flow name="flow1">
<inbound-endpoint address="http://localhost:8080/" exchange-pattern="request-response" />
<jersey:resources>
<component>
<singleton-object class="com.address.Flow1Resource"/>
</component>
</jersey:resources>
</flow>
<flow name="flow2">
<inbound-endpoint address="http://localhost:8080/*" exchange-pattern="request-response" />
<jersey:resources>
<component>
<singleton-object class="com.address.Flow2Resource"/>
</component>
</jersey:resources>
</flow>
*** Flow1Resource.java ***
#Path("/alpha")
public class Flow1Resource {...}
*** Flow2Resource.java ***
#Path("/")
public class Flow2Resource {
#Path("beta")
public void beta() {...}
#Path("gamma")
public void gamma() {...}
...
}
How do I set up the mule inbound-endpoint to capture all the addresses (i.e. beta & gamma), except for a specific url (i.e. alpha).
I know that I can hardcode the paths in the mule config, but this would result in duplication because each address (i.e. beta & gamma) would need its own flow and resource code, which are similar.
Please note that I used "http://localhost:8080/*" in the code above as an conceptual example. It does not work.
--- Update ---
I forgot to mention that the beta and gamma uri's also have security associated with them using:
<http:inbound-endpoint ...>
<spring-security:http-security-filter realm="mule-realm"/>
</http:inbound-endpoint>
I tried adding a 'choice' element to the endpoint, but it complained that spring-security was invalid inside the choice decision structure.
A solution would also need to accommodate this feature.
An easy way to achieve your goal is to combine your flows into one and use the choice router. In this configuration your flow will look like the following:
<flow name="stackoverflowFlow1" doc:name="stackoverflowFlow1">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8081" doc:name="HTTP" />
<logger level="ERROR" />
<choice doc:name="Choice">
<when expression="#[message.inboundProperties['http.request'] == '/']">
<processor-chain>
<logger message="/ invoked " level="ERROR" />
<jersey:resources doc:name="REST">
<component class="Resource" />
</jersey:resources>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="otherwise invoked " level="ERROR" />
<jersey:resources doc:name="REST">
<component class="ApplicationsResource" />
</jersey:resources>
</processor-chain>
</otherwise>
</choice>
</flow>
As you can imagine you can take decision on the path or on top of any other http header.
You can find this router documentation here and a list of the most common http properties you can use to make your choices here

Categories