How To Use Custom Jars in JMeter and Loadium?
How To Use Custom Jars in JMeter and Loadium?
![](https://cdn-images-1.medium.com/max/2560/1*8zwFMUSQ48WHRWnHGi5z3g.jpeg)
Why do you need to use custom jars in Jmeter?
In some cases, we need to share or pass variables between threads and thread groups in JMeter. There are a few ways to do this and using custom jars in Jmeter is one of them. At Jmeter documentation:
![](https://cdn-images-1.medium.com/max/800/0*v_-XNRg4--xo-l3R.png)
But sometimes we want to ensure synchronization between threads and thread groups. In this case, one of the best ways is to “write your own Java classes.”
Creating a Custom Jar
- Create a new java class in your favorite IDE. Our java package structure is as follows:
![](https://cdn-images-1.medium.com/max/800/0*vyhjGQkAUVO3tO-G.png)
Create the methods that you need in your test. In this example, we created methods about BlockingQueue operations.
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class Loadium {
private static LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>();
public static void put(Object obj) throws InterruptedException {
queue.put(obj);
}
public static Object take() throws Exception {
return queue.take();
}
public static Object get(long timeout) throws Exception {
return queue.poll(timeout, TimeUnit.MILLISECONDS);
}
public static void clear() throws Exception {
queue.clear();
}
public static int size() throws Exception {
return queue.size();
}
}
- Create a JAR file by following this instruction “Click Build->Build Artifacts.” Then a jar will be created in your workspace.
![](https://cdn-images-1.medium.com/max/800/0*pmibZKuFnU9Gkm7Z.png)
Copy and paste the jar file in <your JMeter path>/lib folder.
- Restart JMeter to load this JAR file.
Using the custom jar in your JMeter script
In this section, we will learn how to use the custom class in JMeter script.
- Create a Test Plan in your JMeter GUI.
- Add a Thread Group element.
- Add a BeanShell Sampler to your test plan.
Now we can call our custom method as follows in BeanShell Sampler element.
![](https://cdn-images-1.medium.com/max/800/0*Oa4Jgq-CucgTWv3S.png)
Run your test plan, and the result is below.
![](https://cdn-images-1.medium.com/max/800/0*04oy_9AfpByJuzVc.png)
How To Use Custom Jars On Loadium?
- Upload the JMX file.
- Add your JAR file.
- Set your JMeter configuration as thread count, engine count, etc.
- Save and run your test on Loadium.
![](https://cdn-images-1.medium.com/max/800/0*GkrjTxTj1YR7AMvm.png)
For reporting purposes, go to Logs tab in your test report.
![](https://cdn-images-1.medium.com/max/800/0*vZuYpL4pUDEnRppK.png)
The result is below:
![](https://cdn-images-1.medium.com/max/800/0*FprhTKun_GQnej7m.png)
Please feel free to ask questions if you have any.
Happ load testing!