Is it possible to define a spring bean backed by a dynamic language in Mule ESB? - mule

I'm attempting to define a spring bean using as below:
<lang:groovy id="mmmm" name="GroovyRssGeneralTestServer" script-source="${app.home}/groovy/RssGeneralTestServerImpl.groovy">
<lang:property name="xmlFile" value="${app.home}/rss/GeneralTestServer.xml" />
</lang:groovy>
I've tried several different locations for the script-source, but have had no luck in getting mule to find the source for the groovy script.
Second, I'm curious whether I can even wire up a groovy component to use this bean even if I do get it configured correctly above?

Why are you trying Spring bean when there is a Groovy component in Mule that can execute external Groovy script ? Please refer :- http://www.mulesoft.org/documentation/display/current/Groovy+Component+Reference
and this :-
http://groovy.codehaus.org/Dynamic+language+beans+in+Spring
Hope this help

Related

Facing problem while migrating API from mule 3 to mule 4

In my case, I'm doing a migration from Mule 3 to Mule 4.
I have some questions regarding the manual migration from Mule 3 to Mule 4.
How to use the expression component in Mule 4?
Are there any other ways to use session variables?
In Mule 3, I used an expression component to return Java code to my logic. I'd like to reuse that Java code in Mule 4.
The message enricher component is not supported in Mule 4.
Mule 4 does not support transformers such as DOM to XML, XML to String, and so on.
Please assist me.
Several of the questions asked are already explained in the Migration Guide.
How to use the expression component in Mule 4?
In Mule 4 the expression language is DataWeave 2. You should learn about DataWeave and read the migration guide from MEL to DataWeave.
Are there any other ways to use session variables?
No. This is mentioned in the migration guide Migrating Core Components: "Session variables have been removed. Users must explicitly pass headers across transport boundaries."
In Mule 3, I used an expression component to return Java code to my logic. I'd like to reuse that Java code in Mule 4.
You can call Java static methods directly from DataWeave. Also you can execute any Java method with the Java Module and store the output in a variable to be used in a DataWeave script.
Additionally you can create custom functions to add to DataWeave using the Mule SDK.
Note that in Mule 4 you should not use any Mule APIs in your Java code.
Generally speaking try to see if you can use pure DataWeave features before trying to call Java code.
The message enricher component is not supported in Mule 4.
There is no need for the enricher. Mule 4 can use target variables in any operation. This is mentioned in the migration guide.
Mule 4 does not support transformers such as DOM to XML, XML to String, and so on.
That's right. You should use DataWeave Transformations instead of transformers. Yet another topic mentioned in the migration guide.

what is the easy way to manually parse the RAML file in mule?

In mule application I am trying to parse RAML file. I knew that APIKit is doing same as it creates flows after parsing the RAML file. But still, what if I want to parse it in middle of the flow manually?
I have seen the raml parsers available but not finding the proper usage of those javascript libraries or java libraries on how to use them in mule application..
Yes you can parse your RAML in your java application using java class or groovy component implementing java.
There are java parser available like RamlModelBuilder which you can use to parse your application RAML like validation of your RAML file, getting APIs name, getting all resources name, method name, scopes, security schema and their names, query parameters, headers and many more...
Just check the example how it is used here. You can simply create a java class and get your RAML parsed
https://github.com/anirban37/Anirban-Custom-Oauth-Policy/blob/master/Anirban-RAML-Oauth-V3/OauthPolicies.xml#L594.
ramlModelResult = new RamlModelBuilder().buildApi(ac.getRaml())
will give you the current RAML file access of the application in the java class
Theres nothing in Mule to work with the RAML file at runtime.
But you can create any Java component that uses RAML Java libraries and invoke that from Mule in your flows.
The Mule4 SDK is one way of extending mule through Java.
For more information on Mule SDK can be found here https://mule4-docs.mulesoft.com/mule-sdk/v/1.1/
You can also invoke Java classes but they need to be decoupled from the Mule API and you need to extract any variables, properties or payload and explicitly pass the values to your class. For example passing a static String and a flow var as arguments to a Java constructor:
<java:new class="com.foo.AppleEater" constructor="MyClass(String, Apple)">
<java:args>#[{name: 'some string arg', apple: vars.apple}]</java:args>
</java:new>
In your class you could use the RAML Java libraries, and pass the file or path to RAML file to load from the classpath.
More on Java integration with Mule 4 here: https://docs.mulesoft.com/mule-runtime/4.1/intro-java-integration

Best way to load properties file during runtime in mule

I would like to know what would be the best way to load the properties file in mule for the following scenario.
I have a properties file where in I set all the email related fields set in it like templates, to and from etc.
All these need to be set to a specific object along with other changes to that object so I'm planning to use a Java transformer and now I need to load all those values from that properties file and send to transformer. So what would be a best approach to work in above scenario.
Load properties in Java transformer using core java load properties
Load properties using spring context and send it to transformer and access using inbound properties
Kindly let me know if there is any other better approach other than above
First, you should be able to load the property file using spring context as shown below:
<context:property-placeholder location="somename.properties" />
Then, you can set specific property value as flow variable as shown below:
<set-variable variableName="fromAddress" value="${xyz.fromAddress}" />
Finally, you can access this flow variable in your processor class as shown below:
String fromAddress = muleEvent.getFlowVariable("fromAddress");
I will suggest to use Java load properties if these properties are only used in one Transformer. There is one more benefit out of Java load properties is that after modifying your properties file you do not need to restart the application.

Mule spring bean schedule run

We have defined spring beans in Mule-config.xml. Certain public methods in this bean class needs to be periodically executed. We attempted to used spring quartz and spring task scheduler (adding beans in mule-config.xml)- but method is not executing in a schedule way - it is not triggered. Even using annotation (scheduled) does not work. Any work around for this? Any issue with spring scheduler with mule? Kindly help.
Thanks
If you want to use the Schedule annotation, take a look at this recent answer on the subject for a workaround.
Otherwise, Spring Quartz should work fine too. What have you tried? Share your config and specify the Mule version you're using. I'll review my answer accordingly.

How to create and package a custom component for Mule Studio?

I am new to Mule and hoping to avoid going in the completely wrong direction. I would like to create a component, which other devs can add to their Components lists in Mule Studio (to be dropped into their flows).
I see that I can create a Java class that implements callable, which can be added to a Flow by using a Java component and setting its "Class Name" to the one I created. Is there a good way that I can use this to package as a component that can be plugged in to Mule Studio? Or is Mule Devkit the best option for this?
Mule DevKit is the very best option for this. Other alternatives imply messing with Eclipse-related code. You don't want that, go DevKit: http://www.mulesoft.org/documentation/display/current/Mule+DevKit