问题
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