Mule Custom Aggregator with Request-Response Exchange Pattern - mule

I am trying to make a request to an inbound http endpoint that uses <recipient-list> and a Custom Aggregator to combine multiple results. How do I get my inbound http endpoint to "wait" for the result of the Custom Aggregator?
Here are my sample flows. The Aggregator works perfectly. But because is asynchronous, the inbound http endpoint returns immediately. What I'd really like to do is return the result from the Custom Aggregator as a response to the http endpoint. I feel like I am missing something simple.
<flow name="AggregationExample">
<http:inbound-endpoint
exchange-pattern="request-response"
host="localhost"
port="8082"
path="test/aggregate"
/>
<recipient-list evaluator="groovy" expression="['vm://source1','vm://source2']"></recipient-list>
<!-- How do I wait for result of custom aggregator? -->
</flow>
<flow name="SourceAggregation">
<vm:inbound-endpoint path="sourceresult" />
<custom-aggregator failOnTimeout="true" class="com.example.MySourceAggregator"/>
<logger message="RESULTS: #[payload]"/>
</flow>
<flow name="Source1">
<vm:inbound-endpoint path="source1" />
<set-payload value="#[groovy:Thread.currentThread().getContextClassLoader().getResourceAsStream('example-source1.json').text]"/>
<vm:outbound-endpoint path="sourceresult" />
</flow>
<flow name="Source2">
<vm:inbound-endpoint path="source2" />
<set-payload value="#[groovy:Thread.currentThread().getContextClassLoader().getResourceAsStream('example-source2.json').text]"/>
<vm:outbound-endpoint path="sourceresult" />
</flow>

Use a request-reply router in the AggregationExample flow instead of a recipient list: dispatch to another flow that does the dispatching to Source1 and Source2.
Generally speaking, I'm unsure about what you're trying to achieve though: did you build this contraption just to read two files in parallel? Or is there more to it? If just for that, are you sure it's really worth it: aren't there any physical limitations to concurrent file reads? If it's all for optimization, caching would potentially be a better path.
Also, if the recipients in recipient-list are static, why using this router and not an all router?
Finally, did you have issues with MEL that you're using Groovy expressions to read the files instead?

Related

Mule: Triggering a process API that calls a System API

I have built a process API (which is a POST) that calls another system API to retrieve some information , question I have is how can I trigger the process API for testing purpose? Also if I were to add a poller , how would the poller invoke this process API?
<flow name="postData" processingStrategy="synchronous">
<http:request config-ref="call-system-api" path="/getInfo" method="GET" doc:name="call backend">
<http:request-builder>
<http:query-param paramName="modifiedAfter" value="#[message.inboundProperties.'http.query.params'.get("modifiedAfter")]"/>
</http:request-builder>
</http:request>
For HTTP Request,
You could postman, to post request.
If you are going to add a poller, you would place the poller as the first component of the flow.
Here is an example.
<poll doc:name="Poll">
<http:request config-ref="" path="" method="" doc:name="HTTP"/>
</poll>

how to replace datamaper in Mule CE

i need to send json to a web service and getting the response in json format.
so firstly i have a http connecter which receive data and then i need a datamaper to map the json that i get to the web service. in a second flow i put another http connecter which listen to the web service and get the response. actually what i need is an element who can replace the datamaper because i'm working with the community version. so if there is any example of code any tutorial, i would be grateful.
First flow:
<flow name="Flow1">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8082" doc:name="HTTP"
contentType="application/x-www-form-urlencoded" path="getDetails" />
<json:json-to-object-transformer
returnClass="java.lang.Object" doc:name="JSON to Object" />
<set-session-variable variableName="tkn"
value="#[message.payload.token]" doc:name="token" />
<set-session-variable variableName="msg"
value="#[message.payload.msg]" doc:name="message" />
<logger message="#[sessionVars['tkn']]" level="INFO" doc:name="Logger" />
</flow>
You can either do your own mapping in a custom component or use a framework like Smooks to do the transformations. If you choose the latter you can check out this blog post. HTH.
Rajeun,
Refer the below link if that helps.
http://www.mulesoft.org/documentation/display/current/JSON+Module+Reference
You need to follow following steps :-
1. Extract the data from your input Json request and store in variables.
2. Now, if your external service is a SOAP, then you can create a SOAP request using XSLT example :- http://bushorn.com/xml-to-xml-transformation-in-mule/
3. If your external web service is a REST, you can create the JSON request for that service using Mule Expression transformer example :- http://bushorn.com/json-to-json-transformation-in-mule/

Passing a java object to the HTTP Connector

I have a requirement to pass an object to an HTTP connector (Not sure if I can use any other connector). The next step is convert an object to a CXML and make an outbound end point call to another API, get the response and convert it another java object. I am very new to Mule and need some inputs on this. Any pointers would be very helpful.
Can someone give me some good points where we have good links for mule implementation examples (apart from Mule in Action)
EDIT: code from OP's comment below
<flow name="object_serialization.mflowFlow1" doc:name="object_serialization.mflowFlow1">
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
<component class="SerializeObject" doc:name="Java"/>
<serializable-to-byte-array-transformer doc:name="Serializable to Byte Array"/>
<http:outbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" method="POST" doc:name="HTTP"/>
</flow>
You can achieve your goal with Mule:
On the client side:
Serialize the Java object with serializable-to-byte-array-transformer
HTTP POST it with an http:outbound-endpoint
On the server side:
Receive the HTTP POST with an http:inbound-endpoint
Deserialize the Java object with byte-array-to-serializable-transformer
This assumes the Java object implements java.io.Serializable, which should be the case since you stated you want to use Java serialization. This also assumes that the necessary Java classes are available on the classpath of both client and server Mules.

Mule - Exception Strategy propagation across synch VM endpoints

I am experimenting with exception strategies and came across the following config.
If I invoke one flow from another using synchronous vm endpoints and do not catch exceptions in the callee flow. The caller flow exception strategy does not get invoked, but instead the callee flow adds an exceptionPayload.
I would assume if there is an exceptionPayload that the caller flow exceptions strategy would get invoked. But it doesn't. Is this a feature or a bug?
<flow name="main" doc:name="main">
<poll frequency="60000">
<set-payload value="main"></set-payload>
</poll>
<vm:outbound-endpoint address="vm://private" exchange-pattern="request-response" />
<logger level="ERROR" message="After private #[exception]" />
<catch-exception-strategy>
<logger level="ERROR" message="Exception caught in parent." />
</catch-exception-strategy>
</flow>
<flow name="private">
<vm:inbound-endpoint address="vm://private" exchange-pattern="request-response" />
<logger level="ERROR" message="private" />
<null-component></null-component>
</flow>
Also I can access the exceptionPayload via MEL using #[exception] but not via #[message.exceptionPayload]. Is there a reason why you cannot access it this way in Mule? I can see it on the DefaultMuleMessage.
Mule is a message oriented platform therefore, by design, exceptions are contained within flows (not sub-flows) and get propagated as a specific message payload.
MEL works on context objects: here is the MessageContext object API where, as you see, there is no exceptionPayload field.

How to implement periodical requests in Mule ESB?

I need my Mule application to process periodical requests (for example, my application will send requests to a service every 5 seconds and process responses from this service).
There is a Quartz trigger and transport built into Mule ESB. It does exactly what you want, initializing flows at a given interval or CRON expression.
Very comprehensive documentation can be found here.
An alternate solution would be using <poll/> and configuring frequency attribute in it
Reference :-https://developer.mulesoft.com/docs/display/current/Poll+Reference
example:-
<flow name="test1" doc:name="test1" processingStrategy="synchronous">
<poll frequency="1000" doc:name="Poll">
<set-payload value="Polling started at particular interval !!!" doc:name="Set Payload"/>
</poll>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>