Upload
anirban-sen-chowdhary
View
544
Download
0
Embed Size (px)
Citation preview
Accessing Variables in Groovy
By Anirban Sen Chowdhary
We have often seen that people asking how the variables can be accessed in Groovy Script or Java..
Well, accessing variables in Groovy Script or Java are almost in same way and we will see a simple demo of doing it.
But before we start, we will be seeing what are the variables we have in Mule…Mule basically have 2 types of variables:-• Flow variables :- Flow variables are the variables in Mule whose scope is limited to
a flow or a sub flow and whose values doesn’t cross outside a flow level.• Session variables :- Session variables are basically global variable in that
application and it’s scope is all over the flow declared or designed in the application. Session variables can be declared in any flow and can be accessed from any other flow or sub flow
Now, let’s see how can we access a Session variable and a Flow variable from a Groovy script..
Let’s consider we have defined a Session variable and a Flow variable in our flow as follow :-
<set-variable variableName="myflowVariable" value="TestFlowVariable" doc:name="Variable"/>
<set-session-variable variableName="mysessionVariable" value="TestSessionVariable" doc:name="Session Variable"/>
Now, we will see how to retrieve these values in Groovy script
Now, we can use the following to retrieve a Flow variables in a Groovy script :-
message.getInvocationProperty('myflowVariable')
Or
message.getProperty("myflowVariable", org.mule.api.transport.PropertyScope.INVOCATION)
Or simply
flowVars['myflowVariable']
Any of the above code will retrieve the value of Flow variables in Groovy
Similarly we can use the following to retrieve a Session variable in a Groovy script :-
message.getSessionProperty('mysessionVariable')
Or
message.getProperty(" 'mysessionVariable ", org.mule.api.transport.PropertyScope.SESSION)
Or simply
sessionVars['mysessionVariable ']
Any of the above code will retrieve the value of Session variables in Groovy
So, let’s test our application… our Mule flow will be as follows :-
You can see we are setting the variables in the flow and we will be retrieving the values in the Groovy scripts and print it in logger
So, if we test the application we will get the Session variable and Flow variables values in console as follows :-
So we are getting the values of the Session variable and Flow variables with all the method we have used in GroovyWe can use the same methods in our Java class to retrieve the values of variables
So, now we will look to set variables from Groovy script and access it in flow :-
You can set the Flow variables in Groovy using the following syntax :-
message.setProperty('key', 'value', org.mule.api.transport.PropertyScope.INVOCATION);
or
message.setInvocationProperty('key','value');
And you can set the Session variables in Groovy using the following syntax :-
message.setProperty('key', 'value', org.mule.api.transport.PropertyScope.SESSION);
or
message.setSessionProperty('key','value');
Now, lets design our flow again and this time we will be setting the variables in Groovy and access it in flow using loggers. You can easily see we are setting the variables (both Session variable and Flow variable) in 2 different ways for each variable. You can use any one method out of 2
In logger we can see the values of the variables
So, this is the way you can read as well as set variables (both Session variable and Flow variable) in Groovy or Java
In my next slide I will bring some other techniques in Mule implementation .Hope you have enjoyed this simpler version. Keep sharing your knowledge and let our Mule community grow
Thank You