1) 创建一个工厂来查询你的EJB
第一件事情,我们来创建一个简单的一个工厂类用来创建EJB客户段实例.这个实例用来进行测试.
public class MyServiceFactory {
private static final Log log = LogFactory.getLog(MyServiceFactory.class);
private static MyService service;
private static MyServiceFactory me;
private MyServiceFactory() { }
static {
MyServiceFactory.me = new MyServiceFactory();
}
public static MyServiceFactory getInstance() {
return MyServiceFactory.me;
}
public MyService getService() {
if (MyService.service == null) {
// Get the remote interface of the music search service
try {
log.info("Loading the service...");
// JNDI the old-fashioned way:
Context ctx = new InitialContext();
service = (MyService)ctx.lookup("MyAction/remote");
if (service == null) {
log.error("Didn't get the service!");
}
} catch (NamingException e) {
log.error("Error looking up the remote service", e);
return null;
}
}
return service;
}
}
2) 写测试类.
接下来,我们就写测试的主体class. 这个class因为是一个sampler,所以我们要继承jmeter的 org.apache.jmeter.protocol.java.sampler package. 这个抽象类由一个runtest方法,我们需要重写这个方法即可. 除了这个,我们可能还需要重写getDefaultParameters 方法. 这个方法用来提供一些参数值,用来展现在Jmeter's GUI . 就是我们可以通过gui上的输入框等ui空间,将参数传给该class测试类. (package 包位于 \lib\ext\ApacheJMeter_java.jar 和 \lib\ext\ApacheJMeter_core.jar) 需要被引用.
package us.mikedesjardins.demo.jmeter;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
public class DigitalContentServiceEJBTestSampler extends AbstractJavaSamplerClient {
public SampleResult runTest(JavaSamplerContext context) {
SampleResult results = new SampleResult();
MyService service = MyServiceFactory.getInstance().getService();
results.sampleStart();
Long param1 = context.getLongParameter("PARAM_1");
String param2 = context.getStringParameter("PARAM_2");
MyResult result = service.myMethod(param1, param2);
if (result != null) {
results.setSuccessful(true);
results.setResponseCodeOK();
results.setResponseMessage("'myResult:" + myResult);
} else {
results.setSuccessful(false);
}
results.sampleEnd();
return results;
}
@Override
public Arguments getDefaultParameters() {
Arguments args = new Arguments();
args.addArgument("PARAM_1", "4815162342");
args.addArgument("PARAM_2", "Iculus");
return args;
}
}
2) 设置jmeter
${JMETER_INSTALL_LIB}/lib/ext 是jmeter额外需要的库的文件夹. 里面放的都是需要的第三方的额外库.因为我们进行测试ejb,用刀了ejb client 依赖一些ejb client库,所以我们需要将jbossall-client.jar 放入此文件夹.
将我们自己写的sampler 声称jar包,也放入该目录.
然后重启Jmeter.
3) 启动后, 添加threadGroup, 然后添加java Request Sampler, 然后选择你要进行测试的类名称.
4) 那么你就可以开始测试你的EJBs了.