使用EJB定时服务 [英] Using EJB Timer Service
问题描述
我有一小段代码,我正试图用定时器服务执行。
I have a small bit of code that I'm trying to execute with the timer service.
我无法在线找到一个很好的例子或教程。甲骨文的教程太快了,让我掌握了我需要的基本工具。我只是希望它在程序启动时立即执行,然后在每个小时之后执行。
I'm having trouble finding a good example or tutorial online. Oracle's tutorial covered a bit too much too quickly for me to grasp the basic utility that I need. I just want it to execute immediately when the program starts and then every hour after that.
什么样的计时器看起来像?
What would a sample timer look like?
推荐答案
使用 @Singleton
@Schedule
和另外一个 @PostConstruct
来调用方法直接在施工后:
That's the simplest to achieve with a @Singleton
@Schedule
and an additional @PostConstruct
to invoke the method directly after construction:
package com.example;
import javax.annotation.PostConstruct;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
@Singleton
public class SomeBackgroundJob {
@PostConstruct
@Schedule(hour="*/1", minute="0", second="0", persistent=false)
public void run() {
// Do your job here.
}
}
唯一的区别是它没有在启动后每个小时都会运行,但只能在启动后的每一个小时内运行。这不应该真的很重要,我想?
The only difference is that it doesn't run every hour after startup, but only on every whole hour after startup. That shouldn't really matter, I think?
这篇关于使用EJB定时服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!