-
1. Re: How can we set a delay time to do the action after the triggering
adinn Mar 12, 2014 5:09 AM (in response to flyaway_921)Hi Wei Cao,
I would like to kill the jvm 10ms later after line 7 was called,how can I do this?
Is there any build in function that can help with this? or do I need to start a new thread to done this, if so how can I start a new thread in the rule?
I need some idea for this.
No, I don' t think there is a built-in method which does exactly what you want. The nearest thing you can do with built-in methods is this
RULE delay 10 ms then kill jvm
CLASS myTest
METHOD myFun
AT LINE 7
IF TRUE
DO delay(10);
killJVM();
ENDRULE
This will delay the thread you are running in for 10 ms and then kill the JVM. However, I think what you want is to allow the thread which triggers the rule to continue executing myTest.myFun() and have the killJVM call happen at some later point.
So, yes, the best way to do this is probably to start a new Thread. You will not be able to do that directly in the Rule code as it will require you to define your own Thread class with a specialised run() method. Byteman rules are only allowed to contain executable code. They cannot include class definitions of classes and methods.
The way to do this is to use a Helper class to implement the functionality you want. Here is what you need to do
1) Define a class call MyHelper which inherits its behaviour from the Byteman class Helper.
import org.jboss.byteman.rule.helper.Helper;
public class MyHelper extends Helper
{
public void killJVMAfter(int msecsDelay)
{
final const Helper h = this;
final const int delay = msecsDelay;
Thread t = new Thread() {
public void run() {
h.delay(delay);
h.killJVM();
}
};
}
}
Put the following rule into file myRule.btm
RULE delay 10 ms then kill jvm
CLASS myTest
METHOD myFun
HELPER MyHelper
AT LINE 7
IF TRUE
DO traceln("callign killJVMAfter!");
killJVMAfter(10)
ENDRULE
The HELPER line says use class MyHelper to identify built-in methods. So, when Byteman sees a call to traceln it looks for a public method of class MyHelper to impement the call. Since MyHelper inherits from Helper the standard traceln method gets called. If you look at class Helper you will see that it has a public method implementing every one of the standard Byteman built=in operations. So, when Byteman sees the call to killJVMAfter it uses the method on your class MyHelper to implement it.
In order to make sure your helper code is available you need to put the helper jar into the classpath. First you need to put the helper class in a jar.
javac -classpath ${BYTEMAN_HOME}/lib/byteman.jar MyHelper.java
jar cf myhelper.jar MyHelper.java
The you can ask Byteman to add the jar to the classpath when you start your program
java -javaagent:${BYTEMAN_HOME}/lib/byteman.jar=sys:myhelper.jar,script:myRule.btm myTest
The sys:xxx option says place the jar in the system classpath. That means Byteman can find your class when it tries to execute the injected rule code. You probably ought to change your main method so that it does something after calling myFun. Otherwise you will not really notice that the JVM has been killed.
-
2. Re: How can we set a delay time to do the action after the triggering
flyaway_921 Mar 12, 2014 11:22 PM (in response to adinn)Hi Andrew,
First of all, thank you very much , your answer really helps me.
Before I read your answer I create a java class just like the helper to make asynchronously sleep , then I inject it into the code and it works.
However, I think your solution is better, I don't have to create a useless class in my project and the helper can reuse for other situations,that's very nice.
BTW,can I use more than one helper for one rule
e g:
RULE xx
CLASS xx
METHOD xx
HELPER a
HELPER b
AT LINE x
...
-
3. Re: Re: How can we set a delay time to do the action after the triggering
adinn Mar 13, 2014 4:33 AM (in response to flyaway_921)Hi Wei cao,
BTW,can I use more than one helper for one rule
No, I am afraid that each rule must have only one helper class. If you look at the section on helper classes in the Programmer's Guide (available on the project documentation page) you will learn why.
-
4. Re: How can we set a delay time to do the action after the triggering
flyaway_921 Mar 14, 2014 4:33 AM (in response to adinn)Thanks very much for your help Andrew!