Running a java code repeatedly for a given duration in Jenkins

I want to run java code for a certain duration ,say 16 hours! I have a java code that runs for approximately an hour.I want this to run repeatedly for 16 hours. So I have a parameter that is passed by the user through Jenkins ! I access this value using

System.getenv("Duration");

Now, I want to exit the execution after the specified time. So suppose the user selected 16, the script should run for 16 hours and then exit.

Accepting input from Jenkins user as shown in the image

I saw some other questions, but most of them were dealing with timers for either few seconds or few minutes. I need an efficient solution. Thanks 🙂

FYI – Environment – Jenkins+TestNG+Maven+Java

EDIT :

long start = System.currentTimeMillis();
long end = start + durationInHours*60*60*1000; 

while (System.currentTimeMillis() < end)
{
    //My code here runs for  approx. 50 mins!
}

Now suppose the user chooses the value 3 hours, I want the while loop to exit after 3 hours. But this does not happen as it has not yet completed 3 hours when checking the while condition.So it enters the while condition even the 4th time(since time elapsed is 150 mins which is less than 180 mins) it ends after 3 hours ten mins.

How to make it exit the while loop as soon as 180 mins is reached ?

P.S – I could do the math first,( iterations =durationFromUser/codeDuration) and then run a for loop, but I don’t want to do this as my script length may vary.

EDIT 2:

boolean alive = true;
Timer timer = new Timer();

@Test() //Annotation from TestNG
public void public void jenkinsEntryPoint()
{
    String duration = System.getenv("Duration");
    int durationInHours=Integer.parseInt(duration);
     long end = System.currentTimeMillis() + durationInHours*60*60*1000;
 TimerTask task = new TimerTask() {
     public void run() {
        alive = false;
     };

 timer.schedule(task, end);
 while (alive) {
     //My code here runs for  approx. 50 mins!
     function1();
 }

}

void function1() {
  function2();
}

private void function2() {
 for(i=0;i<8;i++)
 {
  while(alive)
  {

    //long running code
    sleep(1000);
    //Some more code
    sleep(2000);
    //Some more code
    //Suppose time elapses here, I want it to quit
    //But its continuing to execute
    .
    . 
    .
    .

  }
 }
}

  • what permissions should jenkins have to execute shell-commands without being insecure?
  • Jenkins : Report results of intermediate build steps in email body
  • Access AWS CodeCommit from Jenkins running on EC2 (Ubuntu)
  • Jenkins doesnt retrieve git references using git parameter plugin
  • Bitbucket Build Erring after triggered from Commit
  • Git Plugin parameter for branch and repository urls?
  • not been able to make Jenkins trigger a build when changes are pushed to Github
  • Jenkins plus Git on same server
  • 2 Solutions collect form web for “Running a java code repeatedly for a given duration in Jenkins”

    The while condition will only be evaluated between script invocations (as you’ve seen). You will have to break out of your long running from inside of it.

    I would typically use a Timer to set a “global” boolean that you would check from inside the loops in your long running code.

    Something like this. Notice checks against ‘alive’ would have to be in all you long loops…

    boolean alive = true;
    Timer timer = new Timer();
    
    public void jenkinsEntryPoint()
         long end = System.currentTimeMillis() + durationInHours*60*60*1000;
         TimerTask task = new TimerTask() {
             public void run() {
                alive = false;
             };
    
         timer.schedule(task, end);
         while (alive) {
             //My code here runs for  approx. 50 mins!
             yourLongRunningCode()
         }
    
    public void yourLongRunningCode() {
        while (alive) {
            doStuff();
        }
    }
    

    I tried ScheduledThreadPoolExecutor and it worked!

    ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
    exec.scheduleAtFixedRate(new Runnable() {
               public void run() {
                   System.out.println("Time's Up According To ScheduledThreadPool");
                   alive = false;
               }
           }, durationInHours, 1, TimeUnit.HOURS); 
    

    This function will be executed after “durationInHours”.

    Thanks @TedBigham 🙂

    Git Baby is a git and github fan, let's start git clone.