how to run two methods annotated with @Schedule one after another in ejb

拜拜、爱过 提交于 2020-01-06 03:53:07

问题


i have to run two methods one after another for every 10 minutes using @schedule annotation in ejb.

my code is like:

@Schedule(minute="*/10")
public void mth1() { 
    System.out.println("welcome");
}

@Schedule(minute="*/10")    
public void mth2() {
    System.out.println("hello");
} 

how to proceed?

Thanks for ur reply..but,the timer is set for 10 minutes for both mthds separately.2nd mthd starts to execute after 1st mth completes,. if i call 2nd mthd in 1st mthd both are run within 10 min ..i want to run each for 10 minutes


回答1:


If you want to have mth2 executed after mth1, you can just call mth2 at the end of mth1 and remove the Schedule annotation from mth2.




回答2:


You can try the below code for timers to execute one after the other, delayed by specific time interval.

@Schedule(minute="*/10")
public void mth1() { 
    System.out.println("welcome");

    //-- Creating a timer manually for mth2
    timerService.createTimer(duration, info);

}

After the specified interval, the time-out method will be called, marking meth2() with @Timeout annotation. Therefore meth2() will be called after x-unit duration when meth1() exits.

@Timeout
public void mth2(Timer timer){
    System.out.println("hello");
}


来源:https://stackoverflow.com/questions/13657243/how-to-run-two-methods-annotated-with-schedule-one-after-another-in-ejb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!