简体   繁体   中英

How to I make this Timer Service Class handle the “change in scheduled time”?

Sample Code:

@Singleton  
@Startup  public class EBlastScheduler {

    @Resource
    TimerService timerService;
    EBlastScheduler what = new EBlastScheduler();
    @PostConstruct
    public void initialize(){
       if (timerService.getTimers() != null) {
           for (Timer timer : timerService.getTimers()) {               
               if (timer.getInfo().equals("EBlastScheduler")){

                   timer.cancel();
               }
           }
       }


        ScheduleExpression expression = new ScheduleExpression();
        expression.second("*/1").minute("*").hour("*");
        timerService.createCalendarTimer(expression);
    }

    @Timeout
    public void execute(Timer timer){
        System.out.println("----Invoked: " + System.currentTimeMillis());
    } }

I just wanted to make a timer service that can handle the change in schedule of its timeout by canceling the former schedule if the new one is set. In my case, I can't figure out how to do this in EJB 3.1 especially because I am new to this framework. Your help will be mostly appreciated. :D

I want something like this:

EBlastScheduler ebs = new EBlastScheduler(ScheduleExpression sExp); // this line of code must change the current scheduled time of the scheduler to the newly set'ed time.

NOTE:

I wanted to access this class remotely; and by passing new schedule as parameter/s, this class must change its timeout accordingly.

You can have stateless a bean, which can be used as a utility for managing timers in the system. It will be responsible for performing operations on them - create/cancel timers.

The purpose is to decouple the timer operations from the startup class, then you can modify them at particular point of time.

But utility bean must be initialized before EBlastScheduler , which is a startup bean, for that you have to annotate it with the annotation @DependsOn("TimerUtilityBean") .

Below is the sample code, might need some modifications.

EBlastScheduler.java

@Singleton 
@Startup
@DependsOn("TimerUtilityBean") 
public class EBlastScheduler {

@EJB
TimerUtilityBean timerBean;

@PostConstruct
 public void initialize(){
  timerBean.cancelTimer("EBlastScheduler");
  timerBean.createTimer("*/1", "*", "*");
 }
}

TimerUtilityBean.java

    @Stateless
    public class TimerUtilityBean {

    @Resource
    TimerService timerService;

     public void createTimer(String sec, String min, String hour){

        ScheduleExpression expression = new ScheduleExpression();
        expression.second(sec).minute(min).hour(hour);
        timerService.createCalendarTimer(expression);
     }

     public void cancelTimer(String timerInfo){

      if (timerService.getTimers() != null) {

           for (Timer timer : timerService.getTimers()) 
               if (timer.getInfo().equals(timerInfo))
                   timer.cancel();
       }
     }

     @Timeout
     public void execute(Timer timer){
        System.out.println("Invoked: " + System.currentTimeMillis());
     }

     public void reinitializeTimer(String sec, String min, String hour, String timerInfo){
        cancelTimer(timerInfo);
        createTimer(sec, min, hour);
     }
}

Now, you can use TimerUtilityBean to manage timers from anywhere within the application. Can access it remotely & will be able to pass new schedule parameters.

you can do some thing like this (found this code after some online search)

package com.test.scheduler;

import java.io.Serializable;
import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.TimerService;

import org.jboss.seam.annotations.Name;

@Stateless
@Name("pingImpl")
public class PingImpl
                implements PingLocal, PingRemote, Serializable {

        private static final long serialVersionUID = 1105969164226368956L;

        @Resource
        SessionContext sessionContext;
        private static final String name = "Ping";

        public void doAction() {
                Long schedule = new Long(5000); //new timeout...
                TimerService timerService = sessionContext.getTimerService();
                System.out.println("++++ Initialize: AppTimers");
                removeTimer();
                System.out.println("Re-initialize Timers: Setting new timer : " + name);
                timerService.createTimer(new Date(System.currentTimeMillis()+schedule), schedule, name);
                System.out.println("++++ Initialize: Completed");
        }

        public void removeTimer() {
                TimerService timerService = sessionContext.getTimerService();
                for (Object obj : timerService.getTimers()) {
                        javax.ejb.Timer timer = (javax.ejb.Timer) obj;
                        String scheduled = (String) timer.getInfo();
                        System.out.println("-> Timer Found : " + scheduled);
                        if (scheduled.equals(name)) {
                                System.out.println("-> Removing old timer : " + scheduled);
                                timer.cancel();
                        }
                }
        }

        @Timeout
        public void timeout(javax.ejb.Timer timer) {
                System.out.println("Timed event called: " + name);
                doAction();
        }
}

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