简体   繁体   中英

JMeter setTestLogicalAction from JMeterContext gives “Cannot reach instance method”

JMeter 5.1. The following link gives a working code Restarting a user thread conditionally in JMeter where setTestLogicalAction is called from SampleResult . the method is indeed listed in methods in https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html . However, it's also listed in https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html but using org.apache.jmeter.threads.JMeterContext.setTestLogicalAction gives

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``    import org.apache.jmeter.samplers.SampleResult;     import org.apache.jmeter . . . '' : Cannot reach instance method: setTestLogicalAction( org.apache.jmeter.threads.JMeterContext$TestLogicalAction ) from static context: org.apache.jmeter.threads.JMeterContext

Why can't I use setTestLogicalAction from JMeterContext ? What do I miss about classes in java?

Also, the code from above mentioned post works in beanshell, but neither that nor modified as above code works in JSR223+Groovy. After taking notice that methods are not static, code is as below (both paths were tried, finally I list them together) in groovy, no errors, but thread continues, not restarted...

jmco = new org.apache.jmeter.threads.JMeterContext();
jmsr = new org.apache.jmeter.samplers.SampleResult();

jmco.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);                                 
jmsr.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD); 

Can I use setTestLogicalAction in groovy script?

PS interesting how beanshell can call non-static method w/o instantiation ...

ADDED 2019/04/01:

def mycontext = org.apache.jmeter.threads.JMeterContextService.getContext()
mycontext.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);

the code was changed to the above after suggestion in Dmitri answer, but the code does not restart the thread.

prev.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_CURRENT_LOOP)

为我工作(来自基于Groovy的后处理器)。

  1. If you're not that good at programming go for Flow Control Action sampler instead of trying to replicate its functionality in code, in any case it will be faster.
  2. There is no need to instantiate neither JMeterContext nor SampleResult as they're pre-defined in JSR223 Test Elements , moreover you're doing this wrong, ie correct way of accessing JMeterContext is calling JMeterContextService.getContext() function

    在此处输入图片说明

  3. Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so I would recommend forgetting about Beanshell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM