在Jboss中使用Quartz

     Jboss EJB默认使用的定时服务是TimerService,TimerService的使用过程较为繁琐,需要使用一个无状态的serviceBean去实现scheduleTimer, timeoutHandler方法,并且要通过外部调用去启动和控制定时器的频率。在使用过程中觉得TimerService定时器非常不好用。因此希望能使用简单好用Quartz来做为服务器的定时器。

     如果使用Quartz定时器,在启动定时服务前,需要初始化定时的内容,但在Jboss Ejb中,我实在是找不到能让Quartz初始化的办法,没有web.xml文件,也无法配置init方法。最初想到的办法是让TimerService去调用一次Quartz定时器的初始化方法,来启动Quartz定时器。但这实在不是一个好的方法,。后来在一次无意中使用google搜索ejb quartz时(使用baidu,真找不到这文章), 看到了曙光http://docs.jboss.org/ejb3/docs/tutorial/1.0.7/html/Quartz_scheduler_integration.html
原来在jboss的ejb3文档中,已经清楚的说明整合Quartz的办法。
下面简单的解释一下,EJB3中使用Quartz的方法
 
 1 import javax.ejb.ActivationConfigProperty;
 2 import javax.ejb.MessageDriven;
 3 import javax.inject.Inject;
 4 
 5 import org.apache.log4j.Logger;
 6 import org.jboss.annotation.ejb.ResourceAdapter;
 7 import org.quartz.Job;
 8 import org.quartz.JobExecutionContext;
 9 import org.quartz.JobExecutionException;
10 //@MessageDriven注释指明这是一个消息驱动Bean,并使用@ActivationConfigProperty注释配置消息的各种属性
11 //@ResourceAdapter告诉EJB容器使用哪种ResourceAdapter
12 @MessageDriven(activationConfig =
13 {@ActivationConfigProperty(propertyName = "cronTrigger", propertyValue = "0/2 * * * * ?")})
14 @ResourceAdapter("quartz-ra.rar")
15 public class AnnotatedQuartzMDBBean implements Job
16 {
17      @Override
18     public void execute(JobExecutionContext arg0) throws JobExecutionException {
19       //定时器执行内容
20      }
21 }

posted on 2014-10-14 13:23  飞凡_gz  阅读(974)  评论(0编辑  收藏  举报

导航