Seam集成的定时器应用_基于Seam框架的后台定时执行服务
程序员文章站
2022-03-10 09:27:13
...
转自:http://blog.csdn.net/beenin/article/details/4363075
在企业应用中,需要编写后台定时服务的需求经常会遇到,如要对某个数据表的字段进行定时刷新等。
在基于Seam框架中要编写一个后台定时执行服务主要要考虑以下两个方面:
- 服务的初始化;
- 服务的定时执行;
本文以Seam集成的QuartZ来实现后台定时执行服务。
一、服务的初始化
服务的初始化Seam组件:
- @Name("scheduleController")
- @AutoCreate
- public class ScheduleController implements Serializable {
- private static final long serialVersionUID = -6332836501640042340L;
- @In
- EntityManager entityManager;
- @In
- EntOnlineProcessor entOnlineProcessor;
- @Logger
- Log log;
- public void scheduleTimer() {
- //启动客户端代理服务
- Long now = System.currentTimeMillis();
- Date when = new Date(now);
- String cron = "0 0/1 * * * ?";//每1分钟
- Date stoptime = new Date(now + 360000000000L);
- entOnlineProcessor.scheduleOnlineTask(when, cron, stoptime);
- log.info("客户端代理服务服务已成功启动");
- }
- }
@Name("scheduleController") @AutoCreate public class ScheduleController implements Serializable { private static final long serialVersionUID = -6332836501640042340L; @In EntityManager entityManager; @In EntOnlineProcessor entOnlineProcessor; @Logger Log log; public void scheduleTimer() { //启动客户端代理服务 Long now = System.currentTimeMillis(); Date when = new Date(now); String cron = "0 0/1 * * * ?";//每1分钟 Date stoptime = new Date(now + 360000000000L); entOnlineProcessor.scheduleOnlineTask(when, cron, stoptime); log.info("客户端代理服务服务已成功启动"); } }
在Seam框架中如何在Seam环境初始化后来自动执行该初始化方法呢,可以components.xml的配置来实现:
- <event type="org.jboss.seam.postInitialization">
- <action execute="#{scheduleController.scheduleTimer}"/>
- </event>
- <!-- Install the QuartzDispatcher -->
- <async:quartz-dispatcher/>
- sp;
<event type="org.jboss.seam.postInitialization"> <action execute="#{scheduleController.scheduleTimer}"/> </event> <!-- Install the QuartzDispatcher --> <async:quartz-dispatcher/>
二、服务的定时执行
服务的定时执行关键是服务的实现,参考实现如下代码:
- @Name("entOnlineProcessor")
- @AutoCreate
- public class EntOnlineProcessor {
- @In
- EntityManager entityManager;
- @Logger Log log;
- @Asynchronous
- @Transactional
- public QuartzTriggerHandle scheduleOnlineTask(@Expiration Date when,
- @IntervalCron String cron,
- @FinalExpiration Date stoptime)
- {
- ......
- return null;
- }
- }
@Name("entOnlineProcessor") @AutoCreate public class EntOnlineProcessor { @In EntityManager entityManager; @Logger Log log; @Asynchronous @Transactional public QuartzTriggerHandle scheduleOnlineTask(@Expiration Date when, @IntervalCron String cron, @FinalExpiration Date stoptime) { ...... return null; } }
关于如何触发该异步方法,并且控制定时机制在上一个代码中已包含了:
- //启动客户端代理服务
- cron = "0 0/1 * * * ?";//每1分钟
- entOnlineProcessor.scheduleOnlineTask(when, cron, stoptime);
- log.info("客户端代理服务服务已成功启动");
上一篇: web前端开发需要学习什么?新人必看
下一篇: maven3 创建本地库