8

我是 J2EE6 中 @Schedule 注释的新手

我想使用 EJB 3.x 和 Glassfish 3.1 运行作业。

javax.ejb.Schedule 对我们来说似乎是一个不错的选择,因此我们可以将自定义时间想象为:

@Singleton
public class CustomTimer {
    @EJB
    SettingsFacade settingsFacade;

    @Schedule(second="someSecondParameter", minute="someMinuteParameter",hour="someHourParameter", persistent=false)
    public void executeTimer(){
        //Code executing something against database using the settingsFacade
    }
}

在这里,我们希望从数据库中获取参数,因此每个月都会更改它们。有什么干净的解决方案吗?

4

4 回答 4

7
@Singleton
@启动
公共类 ScheduleTimerService {

    @Resource private TimerService timerService;

    公共无效 setTimerService(TimerService timerService) {this.timerService = timerService; }

    @PostConstruct
    私人无效postConstruct(){
        timerService.createCalendarTimer(createSchedule());
    }

    @暂停
    公共无效计时器超时(计时器计时器){
           在此处添加您的代码,以便在达到调度时调用...
           在此示例中:每天 01h:30m ;-)
    }

    私人 ScheduleExpression createSchedule(){

        ScheduleExpression 表达式 = new ScheduleExpression();
        expression.dayOfWeek("Sun,Mon,Tue,Wed,Thu,Fri,Sat");    
        表达式.小时(“01”);
        表达式.分钟(“30”);

        返回表达式;
    }
}
于 2016-11-21T20:17:00.710 回答
4

不,没有解决方案@Schedule,因为注释属性通常应该是编译时常量。

当需要更大的灵活性时,可以使用编程计时器。此外,还必须实施轮询数据库以更改配置以及删除现有计时器和创建新计时器。

于 2013-04-12T18:18:02.073 回答
0

那么您需要创建两个调度程序一个调度程序将运行以更新数据库中的数据,基于您可以创建其他调度程序。

但是对于这个需要做一些程序化的东西。您还可以查看 EJB 计时器,在这种情况下对您有帮助。这也是基于注释的。

于 2013-04-16T09:59:38.273 回答
-1

有一种简单的方法可以做到这一点。我想要一个每天都称为流程的东西,但是工作本身应该在同一天随机完成。我设法通过在调用 EJB 计时器服务后添加一个简单的线程工作者来运行它。然后我会让它在那天随机休眠一段时间。

以下代码是每 1 分钟唤醒一次并等待线程完成的服务示例。

@Schedule(minute = "*/1", hour = "*", persistent = false)
public void runEveryMinute() throws InterruptedException {
    log.log(Level.INFO, "Scheduling for every minute .. now it's: " + new Date().toString());

    // Delay, in milliseconds before we interrupt adding a follower thread
    //we can therefore garantee that it runs every day
    long patience = 1000 * 5;

    threadMessage("Starting forever alone no more thread");
    long startTime = System.currentTimeMillis();
    Thread t = new Thread(new MessageLoop());
    t.start();

    threadMessage("Waiting for new thread to finish");
    // loop until MessageLoop thread exits
    while (t.isAlive()) {
        threadMessage("Still waiting...");
        // Wait maximum of 1 second for MessageLoop thread to finish.
        t.join(1000);
        if (((System.currentTimeMillis() - startTime) > patience)
                && t.isAlive()) {
            threadMessage("Tired of waiting! Adding new followers now!");
            t.interrupt();
            // Shouldn't be long now -- wait indefinitely
            t.join();
        }
    }
    threadMessage("Finally! You are not alone anymore!");

}

// Display a message, preceded by
// the name of the current thread
static void threadMessage(String message) {
    String threadName = Thread.currentThread().getName();
    System.out.format("%s: %s%n", threadName, message);
}

private static class MessageLoop implements Runnable {

    public void run() {
        String importantInfo[] = {
            "A kid will eat ivy too"
        };
        try {
            for (int i = 0;
                    i < importantInfo.length;
                    i++) {
                // Pause for 4 seconds
                int max = 10;
                int min = 2;
                int randomTimer = 0 + (int) (Math.random() * ((max - min) + 1));
                Thread.sleep(randomTimer * 1000);
                // Print a message
                threadMessage(importantInfo[i]);
            }
        } catch (InterruptedException e) {
            threadMessage("Patience is not a virtue! Thread stopping for now!");
        }
    }
}
于 2014-06-11T15:03:31.433 回答