jmeter 实现java请求1方法有两种:
第一种: 继承抽象类 MyJavaClass extends AbstractJavaSamplerClient{ }
第二种:实现 JavaSamplerClient 接口 SecretClass implements JavaSamplerClient{ }
这里我用第二种:
JavaSamplerClient主要有4个方法:
public void setupTest(JavaSamplerContext ctx) {} //可以为空
public SampleResult runTest(JavaSamplerContext ctx) {} //这个是编写自定义java请求的核心代码区
public void teardownTest(JavaSamplerContext ctx) { } // 可为空
public Arguments getDefaultParameters() {} // 参数入口区
代码实现:
package com.javareq; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SecretClass implements JavaSamplerClient { private static final Logger logger = LoggerFactory.getLogger(SecretClass.class); @Override public void setupTest(JavaSamplerContext ctx) { } @Override public SampleResult runTest(JavaSamplerContext ctx) { SampleResult result= new SampleResult(); String secret=ctx.getParameter("secret"); String message=ctx.getParameter("message"); String algorithm=ctx.getParameter("algorithm"); result.sampleStart(); String response=javaSecretUtil.EncryptHmacSha1(secret,message,algorithm); result.setResponseData(response,"utf-8"); result.setDataType(SampleResult.TEXT); result.setSuccessful(true); result.setResponseMessage("方法执行成功!"); result.setResponseCode("0"); result.sampleEnd(); logger.info("result finally get as String is : "+ result.getResponseDataAsString()); return result; } @Override public void teardownTest(JavaSamplerContext ctx) { } @Override public Arguments getDefaultParameters() { Arguments arguments =new Arguments(); arguments.addArgument("secret",""); arguments.addArgument("message",""); arguments.addArgument("algorithm",""); return arguments; } // public static void main(String[] args) { // SecretClass demo = new SecretClass(); // Arguments arguments = new Arguments(); // arguments.addArgument("secret","hello"); // arguments.addArgument("message","key"); // arguments.addArgument("algorithm","HmacSHA1"); // // String response=codecHmacSha1.hmacSha1("hello","key","HmacSHA1"); // // JavaSamplerContext ctx=new JavaSamplerContext(arguments); // demo.setupTest(ctx); // demo.runTest(ctx); // demo.teardownTest(ctx); // // // } }
main方法为测试使用,使用完打包前注释掉
加密类:
package com.javareq; import org.apache.commons.codec.digest.DigestUtils; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; /** * dependency : org.apache.commons.codec==1.1.4 and org.apache.commons.lang3==3.9 */ public class javaSecretUtil { static String utf8="utf-8"; static String EncryptHmacSha1(String encryptText,String key,String ALGORITHM){ String res=null; try { SecretKey secretKey = new SecretKeySpec(key.getBytes(),ALGORITHM); Mac mac = Mac.getInstance(ALGORITHM); mac.init(secretKey); // deal text byte[] text = encryptText.getBytes(javaSecretUtil.utf8); byte[] bytes = mac.doFinal(text); res = Base64.getEncoder().encodeToString(bytes); }catch (Exception e){ e.printStackTrace(); } return res ; } }
依赖包:
日志配置:
<?xml version="1.0" encoding="UTF-8"?> <Configuration package="log4j.test" > <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Logger name="log4j.test.Log4jTest"> <AppenderRef ref="Console"/> </Logger> <Root level="info"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>
打包完成放入lib/ext测试请求:
监听结果树:
当然也可以本地先调试好再打包:
使用打包前要注释的main方法调试,用完记得注释掉在打包前: