如何使这个定时服务类处理“预定时间的更改”? [英] How to I make this Timer Service Class handle the "change in scheduled time"?

查看:182
本文介绍了如何使这个定时服务类处理“预定时间的更改”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例代码

@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());
    } }

我只想制作一个可以处理时间表变更的计时器服务如果设置了新的时间,则通过取消以前的时间表来超时。在我的情况下,我不知道如何在EJB 3.1中这样做,特别是因为我是这个框架的新手。您的帮助将大受赞赏。 :D

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

我想要这样的东西:


EBlastScheduler ebs = new EBlastScheduler(ScheduleExpression sExp); //
这行代码必须将调度程序的当前预定时间更改为新设置的
时间。

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.

注意:


我想远程访问这个类;并且通过传递新的
调度作为参数/ s,该类必须相应地更改其超时

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


推荐答案

您可以使用无状态的bean,它可以用作管理系统中的计时器的实用程序。它将负责对它们执行操作 - 创建/取消定时器。

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.

但实用程序bean必须在 EBlastScheduler 之前初始化,这是一个启动bean必须使用注释 @DependsOn(TimerUtilityBean)进行注释。

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 / p>

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

现在,您可以使用 TimerUtilityBean 可以在应用程序中的任何位置管理定时器。可远程访问将能够传递新的计划参数。

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.

这篇关于如何使这个定时服务类处理“预定时间的更改”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆