在Jenkins中以给定的时间重复运行Java代码 [英] Running a java code repeatedly for a given duration in Jenkins
问题描述
我想在一定时间内运行Java代码,比如说16个小时!我有一个可以运行大约一个小时的Java代码,我希望它可以重复运行16小时.因此,我有一个用户通过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");
现在,我想在指定时间后退出执行.因此,假设用户选择了16,脚本应运行16个小时,然后退出.
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.
我看到了其他一些问题,但是大多数问题都在处理计时器几秒钟或几分钟.我需要一个有效的解决方案.谢谢:)
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-环境-Jenkins + TestNG + Maven + Java
FYI - Environment - Jenkins+TestNG+Maven+Java
long start = System.currentTimeMillis();
long end = start + durationInHours*60*60*1000;
while (System.currentTimeMillis() < end)
{
//My code here runs for approx. 50 mins!
}
现在假设用户选择值3小时,我希望3小时后退出while循环.但这并没有发生,因为在检查while条件时还没有完成3小时,因此即使进入了第4次时间(因为经过的时间为150分钟且少于180分钟),它也进入了while条件,它在3小时10分钟后结束了.
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.
如何使其在到达180分钟后退出while循环?
How to make it exit the while loop as soon as 180 mins is reached ?
P.S-我可以先做数学运算((迭代= durationFromUser/codeDuration),然后运行for循环,但是我不想这样做,因为我的脚本长度可能会有所不同.
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.
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
.
.
.
.
}
}
}
推荐答案
我尝试了ScheduledThreadPoolExecutor,它起作用了!
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);
此功能将在"durationInHours"之后执行.
This function will be executed after "durationInHours".
感谢@TedBigham:)
Thanks @TedBigham :)
这篇关于在Jenkins中以给定的时间重复运行Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!