Calling a flow using Groovy Script in Mule 4 - mule

I need to make a call to a flow using groovy script in mule 4. Can someone let me know if they have tried it using Mule 4

It is possible, but I would HIGHLY recommend that you do NOT do it.
You will have to dig deep down into the Mule Java APIs and couple yourself to them and also add a lot of your own error handling etc. and be wary of platform updates and the if the Java API changes.
Personally I would restructure you app to only have 'business logic' in scripts and let Mule do the flow lookups elsewhere.
You can even lookup flows in dataweave.
But here is a rough working example(Note you will have to harden this code yourself):
<flow name="test-flow">
<scheduler>
<scheduling-strategy>
<fixed-frequency frequency="100000"></fixed-frequency>
</scheduling-strategy>
</scheduler>
<set-payload value="bla bla" />
<scripting:execute engine="groovy" doc:name="Toggle flow" doc:id="2eb6f071-bdef-4d3d-926d-2565fcd62d33">
<scripting:code>
import org.mule.runtime.api.message.Message;
import org.mule.runtime.core.api.event.CoreEvent;
import org.mule.runtime.core.api.event.EventContextFactory;
flow=registry.lookupByName("another-flow").get();
thisflow=registry.lookupByName("test-flow").get();
msg = Message.builder().value(payload).build();
event =CoreEvent.builder(EventContextFactory.create(thisflow,
org.mule.runtime.dsl.api.component.config.DefaultComponentLocation.fromSingleComponent("add-location"))).message(msg).build();
result =flow.process(event);
</scripting:code>
</scripting:execute>
</flow>
<flow name="another-flow">
<logger level="ERROR" message="Another Flow #[payload]" />
</flow>
You need to fetch the current flow and the flow you want to call.
Using the Mule APIs - construct a Mule Event, Messgae, payload etc. You will also need to add any attributes you need etc.

Is there any way to call mule "static flow" like When I use the above groovy script and it will call the flow ( its starting the called flow and get the values) and get the values.
Requirement :
Like, Mule Flow A is running independently.
Mule flow B wants to access the Flow A variable with out starting the flow A using groovy or python Script.
Note: Can do with flow reference and lookup but would like to implement using script.

Related

How to make a call to multiple rest service in mulesoft

I have a requirement where i need to call multiple rest service.
currently I am invoking only one rest service, as the file is huge we have splitted into parts (like 1 to 10k , 10k to 20k, 20k to 30k...so on)
the flow is designed to read the complete file and now it has been splitted.
Flow goes like this:
HTTP requester(WS call)----businesslogic.
I want to invoke multiple services(1 to 10k , 10k to 20k, 20k to 30k...so on )and use the same common business logic flow.
how to configure?
If you need to call rest api's in sequence for every record the you can do that inside batch steps.
If you need to make rest api call's with out any sequence then you can use scatter gather inside batch.
Number of records to be processed in parallel can be set by configuring batch size.
References: https://docs.mulesoft.com/mule-user-guide/v/3.6/scatter-gather
https://dzone.com/articles/part-1-mule-batch-processing-introduction
Mule allows this by using the scatter-gather component. Be aware of exception handling in this use case. In case one or more routes fails, you need to control this.
<scatter-gather timeout="6000">
<!-- custom-aggregation-strategy class="implement and place your class here if you need a custom strategy"/-->
<http:request config-ref="requestConfig1" path="/path1" method="POST" doc:name="HTTP"/>
<http:request config-ref="requestConfig2" path="/path2" method="POST" doc:name="HTTP"/>
<http:request config-ref="requestConfig3" path="/path3" method="POST" doc:name="HTTP"/>
</scatter-gather>

Is there a way to mock non-global filters in a sub-flow using MUnit?

I wanted to know if there was a way to mock a custom-filter that is within a sub-flow using MUnit.
I'm using Mule 3.4.0 and MUnit 3.4.0.M5.
The sample flow looks like the following.
<sub-flow name="a">
<choice>
<when expression="something...">
<custom-filter doc:name="filter a">...</custom-filter>
</when>
<otherwise>
...
</otherwise>
</choice>
</sub-flow>
I've had to create a wrapper flow around the subflow because I get a NullPointerException whenever I try to hit the sub flow directly using the runFlow syntax. However, in doing so, I am unable to mock the custom-filter using the whenMessageProcessor syntax. Please see my attempt below.
whenMessageProcessor("custom-filter"
.withAttributes(attribute("name").ofNamespace("doc").withValue("filter a"))
.thenReturn(muleMessageWithPayload("some response");
This results in the message not being mocked.
There are to issues here mixed up.
You having to wrap you sub-flow is a MUnit/Mule issue as commented here:
How to mock a Java component within Mule Flow using MUnit
The second issue is filter mocking. Short answer is you can not, please check:
https://github.com/mulesoft/munit/issues/108
Conceptually a filter is a shorthanded way of doing a choice (or an if in a normal language). One usually does not mock a choice/if it rather changes a value in a variable or in our case in the mule message payload. That's why the filter MP can not be mocked.
HTH

How can sync two jira instances using either Mule or Mule Studio?

I want to sync two jira instances using using either Mule ESB or Mule Studio (custom connector). Here Mule will act as middleware for two jira instances. If some activity is performed in JIRA-1 ex., issue is created it should be replicated in JIRA-2.
Is this possible using Mule ESB or Mule Studio ?
I am not getting starting point for this. Currently I am looking for Mule Orchestration for this.
Can someone guide me in right direction ?
<jira:config name="Jira_A" doc:name="Jira_for_A" >
</jira:config>
<jira:config name="Jira_B" doc:name="Jira_for_B">
</jira:config>
<flow name="consuming-from-jiraA" doc:name="consuming-from-jiraA">
<poll doc:name="Poll" frequency="1000">
<jira:get-issues-from-jql-search connectionAddress="http://localhost:8080/rpc/soap/jirasoapservice-v2"
connectionPassword="admin" connectionUser="admin"
config-ref="Jira_A" jqlSearch="project = "MULE ESB" AND key = ME-1"
maxNumResults="100" doc:name="Jira_for_A" />
</poll>
<foreach doc:name="For Each">
<jira:create-issue config-ref="Jira_B" doc:name="Jira_for_B"
project="WT" summary="Mule ESB :Jira sync test issue 1 " type="2" connectionAddress="http://192.xxx.x.xx:8091/rpc/soap/jirasoapservice-v2" connectionPassword="admin" connectionUser="admin">
</jira:create-issue>
</foreach>
</flow>
Have you tried the jira-connector with the JQL query syntax?
I have not tried before, but I assume you could use the jira connector within a poll, and query one of your jiras instances to retrieve information you want (ticket) and then hit the other jira instance to reflect it if it doesn't exists.
Try if it works.

Mule - Flow executed successfully, first time only, but not after that. Why/solution?

I am new to Mule.
I have written java code(CreateObject)for creation of some object on Server end.
I pass few parameters to Mule from client end HTML form like Object Name, Object Size, Object Host name etc.
Mule further call java code(CreateObject) and creates Object on Server end and return response that Object created successfully.
In Mule, the flow is running fine, for the first time, but is not executing successfully during second time.
Following is my Mule flow:
<flow name="Object-Create" doc:name="Object-Create">
<http:inbound-endpoint exchange-pattern="request-response" address="http://localhost:8146/Create" contentType="text/html" doc:name="Service-Request">
<http:body-to-parameter-map-transformer doc:name="FormParam-To-Mule"/>
<not-filter>
<wildcard-filter pattern="/favicon.ico"/>
</not-filter>
</http:inbound-endpoint>
<logger level="INFO" doc:name="Form-Display" message="#[payload]" />
<custom-transformer class="Objectclient.CreateObject" doc:name="Call-Server"/>
<logger level="INFO" doc:name="Output-Display" message="#[payload]"/>
<echo-component doc:name="Echo"/>
</flow>
When i restarted the service, then flow executed successfully(Object is created on server end) for the first time but second/Third/Fourth time, it doesn't. There is no error given second time by flow.
When i run PSVM java code independently twice then Object creates successfully on server end, both the time/everytime.
Could anyone suggest what could be the reason in Mule which is not letting me to accomplish other requests than first and solution for the same ?
Though this is probably not your issue, I suggest you:
move the http:body-to-parameter-map-transformer after the inbound endpoint as there's no need to run it for the favicon requests you're filtering out,
drop the useless and obsolete echo-component.

Mule modularising/invoking private and sub flows

I am modularising my mule configuration into multiple private and sub flows for common tasks such as a common notification/logging flow for example.
These flows require multiple different pieces of information in order to be invoked from various different flows.
What is the best practice to pass this information to the private/sub modularised flow?
Is it flow variables? session variables? create a map of these values as the payload?
I am currently using flow variables, but this makes the configuration very verbose.
Taking my logging flow as an example, I have multiple set-variable processors followed a flow-ref multiple times:
<set-variable variableName="loggingEndpoint" value="xx" />
<set-variable variableName="loggingPriority" value="INFO" />
<set-variable variableName="loggingSubject"
value="xxx" />
<set-variable variableName="loggingBody"
value="xxxxx" />
<set-variable variableName="loggingCC"
value="xx" />
<flow-ref name="LoggingService" doc:name="Logging service" />
I do this multiple times so theres a lot of XML. But if flowVars is the best way to pass info to a private flow then I don't mind. Just want to know best practice.
For logging purposes I would use a logging interceptor, as explained here:
http://www.mulesoft.org/documentation/display/current/Using+Interceptors
http://www.mulesoft.org/documentation/display/current/Java+Component+Reference
If you have something more general, like sending an email or persisting info in a database you can put a and send information in session variables. This way those time consuming tasks do not block your main flow.
Regards.