简体   繁体   中英

Stop/Start all method annotated with @Schedule in EJB 3.1

I need to stop and start all methods annotated with @Schedule in my EJBs classes.

For example I would like to stop and after few minutes restart doIt() method in that EJB class :

@Stateless
public class MySchedule {

  @Schedule(second = "*/15", minute = "*", hour = "*", persistent = false)
  public void doIt(){
    // do anything ...
  }

}

I tried to use EJB Interceptors , like this code :

@Stateless
public class MySchedule {

  @Schedule(second = "*/15", minute = "*", hour = "*", persistent = false)
  @Interceptors(MyInterceptor.class)
  public void doIt(){
    // do anything ...
  }
}

public class MyInterceptor{
         @AroundInvoke
         public Object intercept(){
            System.out.println(" Schedule Intercepted");  
            Object result = context.proceed();
         }
}

But intercept method never fired by @Schedule

What you can do is to have a "Container" class with all of the classes annotated with @Scheduled . However, the problem with breaking/exit a method would rely on a condition/variable that keeps the code running or emits a signal of stop.

I'd do something like:

@Stateless
public class MySchedule implements Schedulable{
    boolean shouldRun = true;

    //This method should be present in the Schedulable interface
    @Override
    public synchronized boolean shouldBeRunning(boolean shouldRun){
      this.shouldRun = shouldRun;
    }

    //This method should also be in the Schedulable interface, so
    //you can invoke it wherever you need it.
    @Override
    @Schedule(second = "*/15", minute = "*", hour = "*", persistent = false)
    public void doIt(){
       /* If it runs a loop, you can break it like this: */
       while(shouldRun){
           //do anything
       }

       /* Otherwise you can break functionality of doIt and verify in each step: */
       if(shouldRun){
           //do anything step 1
       }

       if(shouldRun){
           //do anything step 2
       }

       if(shouldRun){
           //do anything step 3
       }
    }
}

The "Container" class:

@Named
public class SchedulableContainer{
    @EJB
    MySchedule mySchedule;

    @EJB
    MyOtherSchedule myOtherSchedule;

    private Schedulable[] schedulables;

    @PostConstruct
    void initSchedulables(){
       schedulables = new Schedulable[]{ sched, sched2 };
    }

    void toggleSchedulables(boolean shouldRun){
        for(Schedulable sched: schedulables){
            sched.shouldBeRunning(shouldRun);
        }
    }

    public void stopSchedulables(){
        this.toggleSchedulables(false);
    }

    public void restartSchedulables(){
        this.toggleSchedulables(true);

        //Here you could read a property that tells you
        //how much should you delay to trigger MySchedule#doIt
        //again.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM