We are using Java BeanShell interpreter (v1.2 b7) in our application for dynamically executing standard Java syntax.
Sample Code
bsh.Interpreter interpreter = new bsh.Interpreter();
interpreter.set("context", ctx);
interpreter.set("transaction", transaction);
interpreter.set("log", log);
interpreter.eval(script);
We are having a problem under high load where we are seeing thread lock contention when multiple threads execute above code simultaneously. We have multiple threads in the waiting state which is degrading application performance.
Here are the call stack traces of waiting and blocked threads:
Waiting Thread Call Stack
java.util.Vector.addElement():619
bsh.classpath.ClassManagerImpl.addListener():N/A
bsh.BshClassManager.addCMListener():N/A
bsh.NameSpace.<init>():N/A
bsh.BlockNameSpace.<init>():N/A
bsh.BSHBlock.eval():N/A
bsh.BSHBlock.eval():N/A
bsh.BSHWhileStatement.eval():N/A
bsh.Interpreter.eval():N/A
bsh.Interpreter.eval():N/A
bsh.Interpreter.eval():N/A
Blocked Thread Call Stack
java.util.Vector.indexOf():408
java.util.Vector.indexOf():382
java.util.Vector.removeElement():641
bsh.classpath.ClassManagerImpl.addListener():N/A
bsh.BshClassManager.addCMListener():N/A
bsh.NameSpace.<init>():N/A
bsh.BshMethod.invokeDeclaredMethod():N/A
bsh.Name.invokeLocalMethod():N/A
bsh.Name.invokeMethod():N/A
bsh.BSHMethodInvocation.eval():N/A
bsh.BSHPrimaryExpression.eval():N/A
bsh.Interpreter.eval():N/A
bsh.Interpreter.eval():N/A
bsh.Interpreter.eval():N/A
I have checked the source code for the latest version (v2.0 b4) and it also seems to have the same issue.
My questions are:
- Has anyone experienced such problem? If yes, can you please suggest any possible resolution for it?
- Is there any problem with the code that we are using? Is it not recommended to create a separate instance of
bsh.Interpreter
per thread? Please note that script that we are evaluating is different for the different thread. - Is there any alternative to BeanShell interpreter in Java that works well under high load?
As you can see beanshell uses Vector on listeners objects.
Obviously Vector is "over protected" on its internals see vector
BeanShell 1.3.0 was released in August 2003, version 2.0b4 in May 2005.
You can check latest 2.0b5 beanshell but I guess you you will be better off with active projects alternative as groovy