Jmeter BeanShell调用第三方Jar包方法失败

BeanShell脚本:

import dataProducer.mongodb.*;
import org.apache.commons.lang.StringUtils;

log.info("调用第三方Jar包,MongoDB创建colletion>>>");
String url="172.16.8.193:27017";
String database="dtstack";

log.info("close MongoDB connection>>>");
MongoDBDataProducer.close();//关闭MongoDB资源
MongoDBDataProducer.connectNoAuth(url,database);

错误日志:

2020-08-13 10:38:00,414 INFO o.a.j.t.JMeterThread: Thread started: 数栈-StreamApp接口 1-1
2020-08-13 10:38:00,416 INFO o.a.j.u.BeanShellTestElement: 调用第三方Jar包,MongoDB创建colletion>>>
2020-08-13 10:38:00,417 INFO o.a.j.u.BeanShellTestElement: close MongoDB connection>>>
2020-08-13 10:38:00,420 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval    Sourced file: inline evaluation of: ``import dataProducer.mongodb.*; import org.apache.commons.lang.StringUtils;  log. . . . '' : Method Invocation MongoDBDataProducer.connectNoAuth
2020-08-13 10:38:00,420 WARN o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval    Sourced file: inline evaluation of: ``import dataProducer.mongodb.*; import org.apache.commons.lang.StringUtils;  log. . . . '' : Method Invocation MongoDBDataProducer.connectNoAuth
2020-08-13 10:38:00,420 INFO o.a.j.s.FileServer: Stored: ./login.csv

排查问题步骤:

  1. 排除语法错误问题
  2. 日志中可以看出connectNoAuth方法调用失败,排查是否调到connectNoAuth这个方法:将MongoDBDataProducer.connectNoAuth(url,database);改成
    MongoDBDataProducer.connectNoAuth(url,1,database);再运行,报错如下:
2020-08-13 10:47:39,948 INFO o.a.j.t.JMeterThread: Thread started: 数栈-StreamApp接口 1-1
2020-08-13 10:47:39,949 INFO o.a.j.u.BeanShellTestElement: 调用第三方Jar包,MongoDB创建colletion>>>
2020-08-13 10:47:39,950 INFO o.a.j.u.BeanShellTestElement: close MongoDB connection>>>
2020-08-13 10:47:39,955 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval	Sourced file: inline evaluation of: ``import dataProducer.mongodb.*; import org.apache.commons.lang.StringUtils;  log. . . . '' : Error in method invocation: Static method connectNoAuth( java.lang.String, int, java.lang.String ) not found in class'dataProducer.mongodb.MongoDBDataProducer'
2020-08-13 10:47:39,955 WARN o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval	Sourced file: inline evaluation of: ``import dataProducer.mongodb.*; import org.apache.commons.lang.StringUtils;  log. . . . '' : Error in method invocation: Static method connectNoAuth( java.lang.String, int, java.lang.String ) not found in class'dataProducer.mongodb.MongoDBDataProducer'
2020-08-13 10:47:39,955 INFO o.a.j.s.FileServer: Stored: ./login.csv

可反向推断原先MongoDBDataProducer.connectNoAuth(url,database);是调到的
3. 排查是否是connectNoAuth方法报错,只是jmeter未捕获到异常信息;使用try catch来捕获异常:

try {
    MongoDBDataProducer.connectNoAuth(url,database);
}
catch (Throwable ex) {
    log.error("Failed to do this or that", ex);
}

错误日志:

2020-08-13 10:51:08,385 INFO o.a.j.t.JMeterThread: Thread started: 数栈-StreamApp接口 1-1
2020-08-13 10:51:08,386 INFO o.a.j.u.BeanShellTestElement: 调用第三方Jar包,MongoDB创建colletion>>>
2020-08-13 10:51:08,387 INFO o.a.j.u.BeanShellTestElement: close MongoDB connection>>>
2020-08-13 10:51:08,390 ERROR o.a.j.u.BeanShellTestElement: Failed to do this or that
java.lang.NoSuchMethodError: com.mongodb.MongoClient.getDatabase(Ljava/lang/String;)Lcom/mongodb/client/MongoDatabase;
	at dataProducer.mongodb.MongoDBDataProducer.connect(MongoDBDataProducer.java:84) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
	at dataProducer.mongodb.MongoDBDataProducer.connect(MongoDBDataProducer.java:97) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
	at dataProducer.mongodb.MongoDBDataProducer.connectNoAuth(MongoDBDataProducer.java:106) ~[dataPrepare-1.0-SNAPSHOT.jar:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_201]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_201]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
	at bsh.Reflect.invokeMethod(Reflect.java:131) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.Reflect.invokeStaticMethod(Reflect.java:95) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.Name.invokeMethod(Name.java:868) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:69) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:96) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:41) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.BSHBlock.evalBlock(BSHBlock.java:125) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.BSHBlock.eval(BSHBlock.java:75) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.BSHBlock.eval(BSHBlock.java:41) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.BSHTryStatement.eval(BSHTryStatement.java:80) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.Interpreter.eval(Interpreter.java:659) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.Interpreter.eval(Interpreter.java:750) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.Interpreter.eval(Interpreter.java:739) [bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at sun.reflect.GeneratedMethodAccessor28.invoke(Unknown Source) ~[?:?]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_201]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_201]
	at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:166) [ApacheJMeter_core.jar:4.0 r1823414]
	at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:189) [ApacheJMeter_core.jar:4.0 r1823414]
	at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:151) [ApacheJMeter_core.jar:4.0 r1823414]
	at org.apache.jmeter.protocol.java.sampler.BeanShellSampler.sample(BeanShellSampler.java:124) [ApacheJMeter_java.jar:4.0 r1823414]
	at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
	at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
	at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_201]
2020-08-13 10:51:08,391 INFO o.a.j.s.FileServer: Stored: ./login.csv

java.lang.NoSuchMethodError问题排除参考
https://blog.csdn.net/Care_sQueendom/article/details/107989703

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值