Mule variable declaration using set-payload vs expression component - mule

From this SO question Mule expression variable scope, I understood that:
variables declared using set-payload are like instance variables.
variables defined in expression component are like method local variables.
Is my understanding correct? How do we access variables inside expression component?

There is something wrong in your question..
You cannot declare a variable using set-payload... set-payload are used to override existing message payload ..
You can set variable using <set-variable>
Now back to your question ..
Yes there are 2 types of variable in Mule :-
Flow Variables
Session Variables
If you use Flow variable, it is an instance variable.. the scope of the variable is INVOCATION .. that is limited to a particular Flow (and sub-flow)
where as if you use Session Variables .. It's scope is Global and can access from any flow in the configuration
So now .. as I said you can declare a variable using <set-variable> .. here during declaring the variable you need to declare it's scope ... That is either INVOCATION or SESSION
Similarly if you declare a variable using expression component you need to declare the variable either as INVOCATION or SESSION
for example .. see the following :-
<set-session-variable variableName="sessionId" value="#[message.id+’#’+mule.nodeId]" />
<expression-component>sessionVars.sessionId = message.id+'#'+mule.nodeId;</expression-component>
Here you are setting variable using both <set-variable> and Expression Component
and you are setting here as a Global or you can say as a Session Variable
Please find the following for your reference :-
http://blogs.mulesoft.org/mule-school-the-mulemessage-property-scopes-and-variables/
UPDATED ANSWER:-
Yes .You can set the variable either in set-variable or expression and you can access the variable anywhere and it doesn't matter where you have declared ..
For example if you want access the session variables in Expression component that you have declared in set-session-variable using the following :- <set-session-variable variableName="sessionId" value="#[message.id+’#’+mule.nodeId]" /> .....
you can do the following :-
set the session variable into payload <expression-component>payload=sessionVars.sessionId;</expression-component>
and then you can print payload using logger :-
<logger message="payload with sessionVars:-#[message.payload]" level="INFO" doc:name="Logger"/>
to check the value

Related

how to read a property's value of value in Mule4?

I have a property defined in properties file.Note client1 prefix, which is calculated dynamically when request hits.
client1_appilicationid=OBCDSEFT
i have 2 variables defined in my flow;
<set-variable value="#[attributes.headers.'x-client-id']" doc:name="ClientId" doc:id="1e33f179" variableName="clientid"/>
Here attributes.headers.'x-client-id' =client1
<set-variable value="'#[vars.clientid]'++'_'++'applicationid'" doc:name="applicationId" doc:id="9df0420e" variableName="applicationid"/>
In the above if i access #[vars.applicationd] it will print client1_appilicationid as value. but i want 'OBCDSEFT' as value. How I can define applicationId variable to get that?
You are just concatenating the client id header value with the suffix, however that will not read a property value. You should use the p() function to read properties values.
The expression should be something like this: #[p(vars.clientid++'_'++'applicationid')]

Use JMeter Variable in Groovy Code

I have an Config Element in JMeter, Especially User Defined Variables.
I have the variable "user" with the value "Justin", How can I use this variable in the groovy code (of an JSR223 Assertion)?
There are several of getting it:
Given you pass the variable to the JSR223 Assertion script via "Parameters" section you can access it as Parameters which contains the full string passed in the "Parameters" section
Given you pass the variable to the JSR223 Assertion script via "Parameters" section you can access it as args[0] (if you pass more than one variable separated by spaces you will be able to refer 2nd variable as args[1], 3rd as args[2], etc.
You can access it as vars.get('user') where vars stands for JMeterVariables class instance
You can access it as vars['user'] - basically the same as point 3 but uses some Groovy Syntax Sugar
You can access it as ctx.getVariables().get('user') - where ctx stands for JMeterContextService class instance just in case (in some test elements vars shorthand is not available)
Demo:
Any JSR223 element including Assertion have few variables it can use out of the box.
One of the variable is vars which is basically a map of JMeter stored variables.
User Defined Variables row is creating a JMeter variable, so you can get your value Justin in JSR223 using vars.get("user")

variable hides a variable in an enclosing block error

The code
Using m_objSqlConnection = New SqlCeConnection(m_strConnectionString)
End Using
produces the following error:
Variable 'm_objSqlConnection' hides a variable in an enclosing block
The "m_" prefix suggests that you have a member variable with that name. When you use a Using statement you are declaring a variable that exists only in the scope of that block. If you already have a member variable with that name then why would you want a local variable with the same name? You need to decide whether a local variable or a member variable is more appropriate and stick with the one option. If you're disposing the connection at the end of the Using block then you're apparently not reusing it so I'd think that getting rid of the member variable is the way to go.

Deleting unnecessary symbols from variable in Jmeter

I have variable as string in this view:
["564546","56454654","3123123","868987"]
I need the script that deletes unnecessary symbols [ ] " and put it to another variable . (something like trim method)
I assume it should be made in BeanShell pre-processor.
It can be done via Beanshell PreProcessor as follows:
Add Beanshell PreProcessor as a child of the request which needs "another variable"
Put the following code into the PreProcessor's "Script" area:
String yourvar = vars.get("yourvar");
String anothervar = yourvar.replace("[","").replace("]","").replaceAll("\\\"","");
vars.put("anothervar",anothervar);
Change "yourvar" and "anothervar" according to your variables reference names.
yourvar - source variable name
anothervar - result variable name
vars is a shorthand to JMeterVariables class instance which provides access to JMeter variables in current context. See JavaDoc for the class for all available methods and How to use BeanShell: JMeter's favorite built-in component guide for advanced information on Beanshell scripting in JMeter.

the variable scope in tcl

I have a question about variable scope in TCL, I have the following code:
namespace eval ::hello {
variable player
variable name
namespace export *
}
proc ::hello::setPlay {value} {
set ::hello::player
}
proc ::hello::getPlay {} {
return $::hello::player
}
proc ::hello::setName {value} {
set ::hello::name
}
proc ::hello::getName {} {
return $::hello::name
}
proc GET_NAME {} {
#here I have a variable called name as well
set name "RON"
return $name
}
in the GET_NAME,if I set the variable name as "RON", does it will set the variable name declaired in namespace? can I have the same variable name in GET_NAME and namespace? And if I add this line variable name in GET_NAME, does this mean, it will set the variable declaired in name space?
Variables in procedures (and lambda expressions) default to being local to that procedure. If explicitly declared to be something else (e.g., with global, upvar or variable) then that actually couples a local variable to another variable elsewhere so that acting on one (e.g., reading or writing) is exactly like acting on the other. If you use a variable name with :: in it, that is a reference to a namespaced variable (the global namespace is just ::) which might be resolved either relative to the current namespace or the global namespace, depending on whether the name starts with :: or not. (Think of it a bit like a slash in a filename.)
When executing code inside namespace eval, always use variable to declare variables before using them. This avoids problems with global variable shadowing that are really horrible but necessary to keep due to some existing code depending on them. (Technically, you don't need to do this in namespace eval :: as global variables can't shadow themselves, but it doesn't hurt.)
Dealing with specifics:
in the GET_NAME,if I set the variable name as "RON", does it will set the variable name declaired in namespace?
No, not with the code you wrote.
can I have the same variable name in GET_NAME and namespace?
You can use the same name of variable in the two places. This can be confusing, but it limits surprises overall.
And if I add this line variable name in GET_NAME, does this mean, it will set the variable declaired in name space?
Yes. Exactly that. (It couples the two together precisely.)