Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer session variables in Groovy script in mule studio

Tags:

mule

I just started working with Mule. Flow description: I have an HTTP inbound endpoint receive XML message, and I Hvae to update the database (derby) using the XML payload. Ex: I will be receiving Emp Id and Emp name, Exp in the request. I have to update the table with these values.

My Implementation:

After receiving XML input I am using the message Property transformer to save the values in Session scope.

 <message-properties-transformer scope="session" doc:name="Message Properties">    
    <add-message-property key="EmpNum"
                          value="#[xpath:/CreateEmployee/EmpNum]" /> 
 </message-properties-transformer>

like above. And then I have Groovy Script component to update the table. My Query is:

r.update(conn, "INSERT INTO Employee values(#[header:session:EmpNum],#[header:session:EmpName],#[header:session:Experience],#[header:session:Role])");

But it is throwing error:

Lexical error at line 1, column 29.  Encountered: "#" (35), after : "". (org.apache.derby.iapi.error.StandardException)
  org.apache.derby.iapi.error.StandardException:-1 (null)
 Lexical error at line 1, column 29.  Encountered: "#" (35), after : "". Query: INSERT INTO Employee values(#[header:session:EmpNum],#[header:session:EmpName],#[header:session:Experience],#[header:session:Role]) Parameters: [](SQL Code: 30000, SQL State: + 42X02) (java.sql.SQLException)
  org.apache.commons.dbutils.QueryRunner:540 (null)

I have used a logger component to display the values.

#[header:session:EmpNum]

is displaying the proper value.

Please help me how to refer this session values in Groovy script?

like image 721
kaaviraaj Avatar asked Dec 13 '25 05:12

kaaviraaj


1 Answers

The following works for me when using Groovy script in Mule to read flow variables or session variables respectively.

For reading flow variables I'm using

message.getInvocationProperty('yourVarsName').toString()

For reading session variables I'm using

sessionVars['yoursVarsName'] or flowVars['yoursVarsName']

They work very well for me in the Groovy script in Mule 3.5.

like image 65
Jek Avatar answered Dec 15 '25 22:12

Jek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!