This is my ArrayList -
[
{
AccountNumber=123456,
Amount=257710.06
},
{
AccountNumber=845679,
Amount=3672351.06
}
]
Here, I want to check whether the account number 123456 is in the list or not.
I have used "Choice" component, because I want to create another list with searched account numbers. This is my code-
<choice doc:name="Choice">
<when expression="#[flowVars.investOneList.contains(123456)]">
<logger message="Account number is present" level="INFO" doc:name="Logger"/>
</when>
<otherwise>
<logger message="Account number is not present" level="INFO" doc:name="Logger"/>
</otherwise>
</choice>
Here, my logger prints "Account number is not present". My code is not able to find the account number, although it is there in the list.
Could anyone please help me out?
Thanks!
If you want to use List.contains(...) you will need to map the variable you have like this:
%dw 1.0
%output application/java
---
flowVars.investOneList map $.AccountNumber
Which will output [123456, 845679] as the payload (you can set to something else if you want). Then you can do this in your choice router:
<choice doc:name="Choice">
<when expression="#[payload.contains(123456)]">
<logger message="Account number is present" level="INFO" doc:name="Logger"/>
</when>
<otherwise>
<logger message="Account number is not present" level="INFO" doc:name="Logger"/>
</otherwise>
</choice>
But you could do the whole thing in MEL by using the dw() function like this:
...
<when expression="dw('(flowVars.investOneList map $.AccountNumber) contains 123456')">
...
And that will prevent you from needed an intermediate calculation just to get a list of the account numbers.
Related
We would like to use DB query inside mule cache scope.
Wants to store the output of DB query in cache to save DB query trip.
If the DB query doesn't give any output or payload is empty, we dont want to save in mule cache.
How to invalidate the cache of the empty payload entries ?
thank you.
The answer to this is in mule forum, https://forums.mulesoft.com/questions/84675/mule-cache-scope-how-to-invalidate-mule-cache-for.html
<ee:cache cachingStrategy-ref="Caching_Strategy" filterExpression="#
[payload
!= 'testData']" doc:name="Cache">
<db:select config-ref="DBConfig" fetchSize="100"
doc:name="Database">
<db:dynamic-query><![CDATA[select * from STUDENT where
student_id = 'TEST']]></db:dynamic-query>
</db:select>
<choice doc:name="Choice">
<when expression="#[message.payload.size() == 0]">
<logger message="Payload is empty" level="INFO"
doc:name="Logger"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
payload]]></dw:set-payload>
</dw:transform-message>
</when>
<otherwise>
<object-to-string-transformer doc:name="Object to String"/>
</otherwise>
</choice>
<logger message="After Choice" level="INFO" doc:name="Logger"/>
</ee:cache>
I have two flow one receive the xml payload and another json. I am routing the xml call to json flow using mule flow reference component after converting the xml to json payload. and second flow returns the json payload that i need to convert back to xml and reply to client.
My mule flow reference is throwing TransformerMsgException while receiving the json response from second flow.
Failed to transform from "json" to "java.lang.String" (org.mule.api.transformer.TransformerException). Message payload is of type: String
Here are the code for your reference -
<flow name="post:/chkdb:application/json:chkd-config">
<logger message="========json payload==>>>>==== #[message.payload]" level="INFO" doc:name="Logger"/>
<set-variable variableName="GGG_Number" value="#[json:ggg]" doc:name="Variable"/>
<!-- db call returns the payload-->
<choice doc:name="Choice">
<when expression="#[message.payload.size()>0]">
<set-payload value="{"indicator":"True"}" mimeType="application/json" doc:name="Set Payload"/>
</when>
<otherwise>
<set-payload value="{"indicator":"False"}" mimeType="application/json" doc:name="Set Payload"/>
</otherwise>
</choice>
<logger message="=========after producing json output=======" level="INFO" doc:name="Logger"/>
</flow>
<flow name="post:/chkdb:application/xml:chkdb-config">
<logger message="========= xml payload======== #[message.payload]" level="INFO" doc:name="Logger"/>
<json:xml-to-json-transformer mimeType="application/json" doc:name="XML to JSON"/>
<flow-ref name="post:/chkdb:application/json:chkdb-config" doc:name="post:/chkdb:application/json:chkdbapi-config"/> <!-- Getting exception here -->
<logger message=" after subflow call ==== #[message.payload]" level="INFO" doc:name="Logger"/>
<json:json-to-xml-transformer mimeType="application/xml" doc:name="JSON to XML"/>
<logger message="after json to xml conversion" level="INFO" doc:name="Logger"/>
</flow>
XML Request -
<ggg>DeJmp03bkqALlRFYmgu4+A==</ggg>
How i should retrieve the json response from other flow to current flow.
There is something missing here
First flow name: post:/chkdb:application/json:chkdbapi-config
Second flow name: post:/chkdb:application/xml:chkdbapi-config
Flow ref in second flow: post:/chkdb:application/json:chkdb-config
You are not referencing First flow.
So from your flow posted I can find few issues as follows :-
1. You are using where is the flow post:/chkdb:application/json:chkdb-config doesn't exits and it should be post:/chkdb:application/json:chkdbapi-config instead
2. Right now in the flow you are using <when expression="#[message.payload.size()>0]"> where as the message payload is in String format right now.
So, you can either use <when expression="#[message.payload.length()>0]">, using length() instead of size() or put a <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>before choice if you use #[message.payload.size()>0]
So the full code will be something as follows :-
<flow name="post:/chkdb:application/json:chkdbapi-config">
<logger message="========json payload==>>>>==== #[message.payload]" level="INFO" doc:name="Logger"/>
<!-- <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/> --> <!-- incase you use #[message.payload.size()>0] in your choice -->
<!-- db call returns the payload-->
<set-variable variableName="GGG_Number" value="#[json:ggg]" doc:name="Variable"/>
<choice doc:name="Choice">
<when expression="#[message.payload.length()>0]">
<set-payload value="{"indicator":"True"}" mimeType="application/json" doc:name="Set Payload"/>
</when>
<otherwise>
<set-payload value="{"indicator":"False"}" mimeType="application/json" doc:name="Set Payload"/>
</otherwise>
</choice>
<logger message="=========after producing json output=======" level="INFO" doc:name="Logger"/>
</flow>
<flow name="post:/chkdb:application/xml:chkdbapi-config">
<logger message="========= xml payload======== #[message.payload]" level="INFO" doc:name="Logger"/>
<json:xml-to-json-transformer mimeType="application/json" doc:name="XML to JSON"/>
<flow-ref name="post:/chkdb:application/json:chkdbapi-config" doc:name="post:/chkdb:application/json:chkdbapi-config"/> <!-- Getting exception here -->
<logger message=" after subflow call ==== #[message.payload]" level="INFO" doc:name="Logger"/>
<json:json-to-xml-transformer mimeType="application/xml" doc:name="JSON to XML"/>
<logger message="after json to xml conversion" level="INFO" doc:name="Logger"/>
</flow>
I'm trying to initialize a map with the following in a mule expression-component:
#[message.payload = [ 'changeSet' : #[payload], 'sourceAndConnectionMap' : #[flowVars['sourceAndConnectionMap']]]]
Based on the following page, this looks okay, but does not work.
https://developer.mulesoft.com/docs/display/current/Mule+Expression+Language+Reference
Only the first key is set ('changeSet'), and for some reason the payload, which is a map, becomes an ArrayList containing a map... I can also initialize this with just the second key and valy, so I know there isn't an issue resolving the flowVar.
Is there something basic about how I am initializing this map that is incorrect?
In MEL a map should be created like:
#[{'key':'value', 'key1':'value1'}]
If you do
#[[]]
That's just a list of elements.
The other comments, if you're already inside a:
#[]
You need not to add it again. In your example you have it several times.
Please run this through the Mule debugger (put brakepoints in the logs)and you'll see the difference.
<flow name="test>
<set-payload value="#[{'key':'value', 'key1':'value1'}]" doc:name="Set Payload"/>
<logger level="INFO" doc:name="Logger"/>
<set-payload value="#[{'key':'value', 'key1':payload}]" doc:name="Copy_of_Set Payload"/>
<logger level="INFO" doc:name="Copy_of_Logger"/>
<set-payload value="#[['key','value', 'key1','value1']]" doc:name="Copy_of_Set Payload"/> <logger level="INFO" doc:name="Logger"/>
</flow>
HTH
I have route choice depending on the payload. The payload can be either list, map or string. The payload has to be identified and routed depending on the payload type.
I think that you want something like that
<choice doc:name="Choice">
<when expression="#[payload is List]">
<logger level="INFO" message="i am list" doc:name="Logger"/>
</when>
<when expression="#[payload is Map]">
<logger level="INFO" message="i am map" doc:name="Logger"/>
</when>
<when expression="#[payload is String]">
<logger level="INFO" message="i am string" doc:name="Logger"/>
</when>
<otherwise>
<logger message="class doesnt match with [list, map, string]" level="INFO" doc:name="Logger"/>
</otherwise>
</choice>
I do agree with EddĂș response, except for that I would add an otherwise path. If you don't do that and get something different than a list map or string you would get the rather cryptic error:
org.mule.api.routing.RoutePathNotFoundException: Can't process message because no route has been found matching any filter and no default route is defined. Failed to route event via endpoint: ChoiceRouter [flow-construct=testNull, started=true]. Message payload is of type: NullPayload
I got a Mule flow with 2 queries.
The first simply validates if a username is duplicated.
Does something like this:
SELECT count(*) AS TOTAL FROM user_tests WHERE username = #[json:username] AND test_message_title = #[json:test_message_title]
The second one does an INSERT if TOTAL = 0.
Now, i want to use the variable "TOTAL" in a choice inside the same flow.
How do i do it?
Tried #[TOTAL=0], tried #[variable:TOTAL=0], and many others but couldn't make it work.
EDITED: I made a java component that retrieve the "0" or whatever number from the payload.
How do i make the choice expression to compare it?
Tried using Integer.valueOf(message.payload) = 0 in the choice expression but i get an InvalidExpressionException.
This is my entire flow:
<flow name="ADMIN_INSERT_TEST_FILE" doc:name="ADMIN_INSERT_TEST_FILE">
<ajax:servlet-inbound-endpoint channel="/admin/save_test_message" responseTimeout="10000" doc:name="Ajax"/>
<set-variable variableName="#['saved_payload']" value="#[payload]" doc:name="Save original payload"/>
<jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="validate_test_name" queryTimeout="-1" connector-ref="ExtendedRoutingConnector" doc:name="validate duplicated test name">
<jdbc:query key="validate_test_name" value="SELECT count(*) AS TOTAL FROM user_tests WHERE username = #[json:username] AND test_message_title = #[json:test_message_title]"/>
</jdbc:outbound-endpoint>
<logger message="#[message.payload[0].TOTAL]" level="INFO" category="Total" doc:name="Logger"/>
<choice doc:name="Choice">
<when expression="#[message.payload[0].TOTAL == 0]">
<processor-chain>
<set-payload value="#[variable:saved_payload]" doc:name="Save Payload"/>
<jdbc:outbound-endpoint exchange-pattern="request-response" queryKey="insert_user_test_message" queryTimeout="-1" connector-ref="ExtendedRoutingConnector" doc:name="insert user test message">
<jdbc:query key="insert_user_test_message" value="INSERT INTO user_tests (username, test_message_title, user_test_message) VALUES (#[json:username], #[json:test_message_title], #[json:message])"/>
</jdbc:outbound-endpoint>
</processor-chain>
</when>
<otherwise>
<processor-chain>
<logger message="Name #[json:test_message_title] already exists" level="INFO" doc:name="Logger"/>
</processor-chain>
</otherwise>
</choice>
</flow>
Use MEL:
#[message.payload[0].TOTAL == 0]
This will retrieve the value of the TOTAL column from the first row returned by your SELECT query.