问题描述
我正在尝试在服务器中实现一个系统,该系统将在数据库的基础上进行一些更新.
I'm trying to implement a system in server that will do some updates on database on a ragular basis.
此处:生成JSF托管Bean中的线程使用计时器进行计划的任务在其他一些类似的问题中,我看到BalusC强烈建议使用Stateless Bean,如果可能的话,请使用SchedulerExecuterService而不是Timer.
Here: Spawning threads in a JSF managed bean for scheduled tasks using a timer and in some other similar questions, I saw that BalusC strongly recommended to use Stateless Beans, if not possible SchedulerExecuterService rather than Timer.
这是我的情况.我需要一个JSF页面,可以在其中配置计划间隔.即,我可以将其规则从 每5分钟运行一次 更改为 每10分钟运行一次
Here is my situation. I need a JSF page in which I can configure the schedule interval. i.e. I can change its rule from run once in every 5 min to run once in 10 min
首先,我尝试使用@Schedule批注,这很棒.但是,我找不到改变间隔的方法.第一个问题,是否可以像我上面所说的那样动态地更改它?
First, I tried to use @Schedule annotation and it was great. However, I couldn't find a way to change interval with that. First question, is it possible to change it dynamically like I told above?
我当前正在使用SchedulerExecutorService,该程序是从无状态Bean的@PostConstruct调用的.第二个问题,强烈建议不使用Timer BalusC是EJB的TimerService吗?
I am currently using SchedulerExecutorService which is called from @PostConstruct of a Stateless Bean.Second question, is the Timer BalusC strongly recommended not to use is the TimerService of EJB?
第三个问题,我喜欢timerService的属性是:使用scheduleExpression 和 timerConfig .ScheduledExecutorService有任何类似的东西吗?
Third Question, I liked the properties of timerService which is:using scheduleExpression and timerConfig. Are there any similar things for ScheduledExecutorService?
其他问题:我走对了吗?我要拉的东西能以更好的方式完成吗?
Additional Question: Am I on right track? Can the thing that I am trying to pull be done in a better way?
推荐答案
我认为@Schedule仅用于固定的cron类计时器,其中EJB容器在EJB启动时部署计时器.显然,您需要与JSF页面关联的更多动态调度.
I think @Schedule is used only for fixed cron-like timers, where the EJB container deploys a timer at EJB startup. You obviously need more dynamic scheduling connected with a JSF page.
如果您在完整的Java EE 6概要文件上运行,为什么不将TimerService与无状态会话EJB一起使用,如下所示:
If you are running on a full Java EE 6 profile, why not use the TimerService with a Stateless Session EJB like this:
@Stateless
public class JobSchedulerBean {
@Resource
private TimerService timerService;
// @PostConstruct
public void initTimer() {
// set initial timer
ScheduleExpression sch = new ScheduleExpression();
// set cron expression into sch
timerService.createCalendarTimer(sch, new TimerConfig("myTimer", false));
}
public void rescheduleTimer(int interval) {
// get timer from timer service
for (Timer timer : timerService.getTimers()) {
if (timer != null && timer.getInfo().equals("myTimer")) {
timer.cancel();
}
}
// schedule new timer, like in initTimer() method
}
@Timeout
public void timeout(Timer timer) {
// do the job
}
}
@ManagedBean(eager=true)
@ApplicationScoped
public class JobRunner {
private ScheduledExecutorService scheduler;
private static final int POOL_SIZE = 1;
ScheduledFuture<?> runHandle;
@PostConstruct
public void init() {
scheduler = Executors.newScheduledThreadPool(POOL_SIZE);
// set initial expiry to 5 minutes after 5 minutes delay
runHandle = scheduler.scheduleAtFixedRate(new MyJob(), 5, 5, TimeUnit.MINUTES);
}
@PreDestroy
public void destroy() {
scheduler.shutdownNow();
}
public void reschedule(int newDelay) {
// cancel old timer, but do not interrupt it
runHandle.cancel(false);
runHandle = scheduler.scheduleAtFixedRate(new MyJob(), newDelay, newDelay, TimeUnit.MINUTES);
}
}
public class MyJob implements Runnable {
public void run() {
// do the job
}
}
这篇关于计划作业上的chedchedExecutorService,timerService和无状态EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!