2
votes

I have been using JSR223 sampler for some java code but now I am implementing Java Request sampler. The JSR223 code cannot directly work in the Java Request sampler. There are some changes needs to be made. For e.g. in JSR223 the code is:

 props.put("p_driver", driver);
 object = props.get("p_driver").getJsonObject(dbser, dbn, wfid);

Here the driver is an object of type Driver (a utility class that we have written.

I want to create a property of type Object and keep the driver object in it. Also I want to get and set different properties through Java. I know we can use:

JMeterContextService.getContext().getCurrentSampler().getProperties("p_driver");

But there is very less clarity on how jmeter variables and jmeter properties can work in a Java Request Sampler.

2

2 Answers

2
votes

Check JavaSamplerContext which is Sampler context, it supports only get (read only) operations:

JavaSamplerContext is used to provide context information to a JavaSamplerClient implementation. This currently consists of the initialization parameters which were specified in the GUI.

Also a Implementation note in code:

All of the methods in this class are currently read-only.

I see you manage to get property, so this was the intention to read property/variable used in GUI, but not to set/update properties/variables .

Especially will not support update properties which effect multi threads:

If update methods are included in the future, they should be defined so that a single instance of JavaSamplerContext can be associated with each thread.

Here's kafka example of reading JMeter variables:

1
votes

What you're looking for is JavaSamplerContext.

Use JMeter 4.0 and you'll have access to:

You can then get Variable or Property through:

javaSamplerContext.getJMeterProperties().get("propName")

javaSamplerContext.getJMeterVariables().get("varName")

You can then set Variable or Property through:

javaSamplerContext.getJMeterProperties().put("propName", Object you want)

javaSamplerContext.getJMeterVariables().putObject("varName", Object you want)