4 Replies Latest reply on Mar 14, 2014 4:33 AM by Wei Cao

    How can we set a delay time to do the action after the triggering

    Wei Cao Newbie
      public class myTest {
          public static void main(String[] args) {
              myFun();
          }
          
          public static void myFun(){
              System.out.println("this is my function");
          }
      
      }
      

       

      RULE kill jvm after 10ms

      CLASS myTest

      METHOD myFun

      AT LINE 7

      IF TRUE

      DO killJVM();

      ENDRULE

       

      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.adinn

        • 1. Re: How can we set a delay time to do the action after the triggering
          Andrew Dinn Master

          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
            Wei Cao Newbie

            Hi

            • 3. Re: Re: How can we set a delay time to do the action after the triggering
              Andrew Dinn Master

              Hi Wei cao,

              • 4. Re: How can we set a delay time to do the action after the triggering
                Wei Cao Newbie

                Thanks very much for your help Andrew!