14
Accessing Variables in Groovy By Anirban Sen Chowdhary

Accessing Mule variables in groovy

Embed Size (px)

Citation preview

Page 1: Accessing Mule variables in groovy

Accessing Variables in Groovy

By Anirban Sen Chowdhary

Page 2: Accessing Mule variables in groovy

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.

Page 3: Accessing Mule variables in groovy

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

Page 4: Accessing Mule variables in groovy

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

Page 5: Accessing Mule variables in groovy

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

Page 6: Accessing Mule 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

Page 7: Accessing Mule 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

Page 8: Accessing Mule variables in groovy

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

Page 9: Accessing Mule variables in groovy

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');

Page 10: Accessing Mule variables in groovy

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');

Page 11: Accessing Mule variables in groovy

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

Page 12: Accessing Mule variables in groovy

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

Page 13: Accessing Mule variables in groovy

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

Page 14: Accessing Mule variables in groovy

Thank You