Sivakami
Sivakami

Reputation: 11

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

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

Upvotes: 1

Views: 721

Answers (2)

Nayan Wadekar
Nayan Wadekar

Reputation: 11622

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");
}

Upvotes: 0

dunni
dunni

Reputation: 44545

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.

Upvotes: 3

Related Questions