1)。我知道如何从我的问题Use java class fields in beanshell访问beanshell中的java字段和对象。但是,实现起来并不是那么干净,因为我需要先在beanshell中设置java变量,然后才能使用它。然而,在Jmeter中,它提供了非常干净的方式在beanshell中使用地图,就像在java中一样,但是JMeter开发了它的知识库(类),它有助于访问地图的get / put方法。我想以类似的方式在beanshell中访问Map。
我已经检查了JMeter以获取更多信息,我想知道,我已经创建了用户定义变量temp和赋值错误,现在在BSF过程中我只写了一行vars.put('Name','temp Value')并且它已更新临时变量的值。所以,问题是我还没有创建JMeterVariables对象变量,但仍然beanhell允许更新map中的值而不设置你的答案中提到的任何值。我想知道这是如何工作的,需要更深入的信息。
2)。我已经在java和beanshell中创建了我自己的类我正在导入这个类,但它给出的Command not found: BSClass()
是整个代码
Java类
package test;
public class BSClass {
public void BSCMethod(){
System.out.println("I am from BSClass method BSCMethod");
}
}
sample.bsh
import test.BSClass;
c=BSClass();
c.BSCMethod();
print("I am from BeanShell Script");
调用sample.bsh文件java类
package test;
import java.io.FileNotFoundException;
import java.io.IOException;
import bsh.*;
public class DynamicVariable {
public static void main(String[] args) throws FileNotFoundException, IOException, EvalError {
new bsh.Interpreter().source("\\src\\test\\sample.bsh");
}
}
注意:
我很感激你的意见
在Beanshell中,您可以添加任何所需的对象,包括地图
在JMeter中,JMeterVariables
是Map的特殊实现,在评估之前添加到Beanshell Interpreter中,并且还添加了特殊的Object作为JMeterContext
,其中甚至包括JMeterVariables
。码:
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
bshInterpreter.set("Label", getName()); //$NON-NLS-1$
bshInterpreter.set("prev", jmctx.getPreviousResult());//$NON-NLS-1$
bshInterpreter.set("props", JMeterUtils.getJMeterProperties());
bshInterpreter.set("vars", vars);//$NON-NLS-1$
在您使用地图的情况下,您可以按照您在评论中描述的方式执
bshInterpreter.set("myMap", javaMyMapObject);"
然后在Beanshell中从地图中获取特定键:
myMap.get("aField");
要创建类,您应该使用new
关键字,调用:
c= new BSClass();
而不是c=BSClass();
如果你创建自己的类,Class应该在相关包中的jar里面。
jar应位于lib文件夹而不是bin文件夹中,请参阅JMeter's getting started:
这样的目录中的任何jar文件都将自动包含在user.classpath中,子目录中的jar文件将被忽略。给定的值是lib目录中找到的任何jar的补充。所有条目都将添加到系统类加载器的类路径中,也将添加到JMeter内部加载器的路径中。