对于J2EE中的调度任务, 在ejb2.1推出前, 似乎只能采用一些非常途径:比如在servlet中new一个Timer.
我们急切的需要EJB的标准实现.
最近我们就遇到了这样的需求. 还好websphere升级到了6.0, 支持ejb2.1规范,在ejb2.1标准中, 提供了一个Timer Service的标准实现.
要实现Timer Service, 通常的做法就是让一个无状态sessionbean实现TimedObject接口.
一个最简单的Timer Service如下(由一个无状态sessionbean实现, home和local接口省略)
publicclassDemoTimerServiceBeanimplementsjavax.ejb.SessionBean,TimedObject
{
privatejavax.ejb.SessionContextmySessionCtx;
/**
*getSessionContext
*/
publicjavax.ejb.SessionContextgetSessionContext(){
returnmySessionCtx;
}
/**
*setSessionContext
*/
publicvoidsetSessionContext(javax.ejb.SessionContextctx){
mySessionCtx=ctx;
}
/**
*ejbCreate
*/
publicvoidejbCreate()throwsjavax.ejb.CreateException
{
}
/**
*ejbActivate
*/
publicvoidejbActivate(){
}
/**
*ejbPassivate
*/
publicvoidejbPassivate(){
}
/**
*ejbRemove
*/
publicvoidejbRemove()
{
}
publicvoidstartTimer()
{
TimerServicets=this.getSessionContext().getTimerService();
Timertimer=ts.createTimer(newDate(),1000,"timerInfo");
}
/*(non-Javadoc)
*@seejavax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
*/
publicvoidejbTimeout(Timerarg0)
{
//implementsyourbusinesslogic.
System.out.println("processyourbusinesslogic");
TaskHandler.process();
}
}
其中startTimer()用来启动一个调度任务,当调度任务的timeout时间到来时, ejb容器回调ejbTimeout()方法.以上示例子每隔一秒执行一次ejbTimeout()方法.
需要注意的是, timer service的启动需要客户调用startTimer()方法; 并且timer service具有持久化特性, 也就是说: 如果一个timer service已经启动, 如果
服务器重新启动了, 这个timer service会继续执行(无须再次调用startTimer()方法).
其实我们可以把startTimer()方法放在ejbCreate(), 当ejb实例化的时候就启动调度. 但这需要修改一下启动调度的策略. 因为容器会根据需要调用sessionbean的
create()方法, 如果每实例化一个sessionbean,就产生一个新调度, 这显然不是我们需要的.
所以,修改的策略为:
如果已经有一个同名的timer service了, 就放弃, 否则产生一个新timer service.
publicvoidstartTimer()
{
TimerServicets=this.getSessionContext().getTimerService();
Iteratortimers=ts.getTimers().iterator();
while(timers.hasNext())
{
Timertimer=(Timer)timers.next();
Stringinfo=(String)timer.getInfo();
System.out.println("timerinfo="+info);
if(info.equals("timerInfo"))
{
System.out.println("thereisalreadyatimer=timerInfo,soreturn");
return;
}
}
ts.createTimer(newDate(),1000,"timerInfo");
}
贴一个完整的timer service实现供有需要的人参考, 包括启动, 停止timer service(由一个无状态sessionbean实现, home和local接口省略)
publicclassDemoTimerServiceBeanimplementsjavax.ejb.SessionBean,TimedObject
{
privatestaticfinalStringCONGIG_BEAN_NAME="configBean";
privatejavax.ejb.SessionContextmySessionCtx;
/**
*getSessionContext
*/
publicjavax.ejb.SessionContextgetSessionContext(){
returnmySessionCtx;
}
/**
*setSessionContext
*/
publicvoidsetSessionContext(javax.ejb.SessionContextctx){
mySessionCtx=ctx;
}
/**
*ejbCreate
*/
publicvoidejbCreate()throwsjavax.ejb.CreateException
{
}
/**
*ejbActivate
*/
publicvoidejbActivate(){
}
/**
*ejbPassivate
*/
publicvoidejbPassivate(){
}
/**
*ejbRemove
*/
publicvoidejbRemove()
{
}
publicvoidstopAllTimer()
{
TimerServicets=this.getSessionContext().getTimerService();
Iteratortimers=ts.getTimers().iterator();
while(timers.hasNext())
{
Timertimer=(Timer)timers.next();
timer.cancel();
}
}
publicvoidstopTimer(StringtimerName)
{
TimerServicets=this.getSessionContext().getTimerService();
Iteratortimers=ts.getTimers().iterator();
while(timers.hasNext())
{
Timertimer=(Timer)timers.next();
Stringinfo=(String)timer.getInfo();
System.out.println("timerinfo="+info);
if(info.equals(timerName))
{
System.out.println("thereisalreadyatimer="+timerName+",sowecancelit!");
timer.cancel();
}
}
}
publicArrayListgetAllTimerInfo()
{
ArrayListtimerList=newArrayList();
TimerServicets=this.getSessionContext().getTimerService();
Iteratortimers=ts.getTimers().iterator();
while(timers.hasNext())
{
Timertimer=(Timer)timers.next();
Stringinfo=(String)timer.getInfo();
timerList.add(info);
}
returntimerList;
}
publicvoidstartTimer()
{
Objectconfigobj=MyBeanFactory.instance().getBean(CONGIG_BEAN_NAME);
if(configobj==null)
{
System.out.println("Can'tgetconfigBeanfromspringconfigfile.config==null");
return;
}
Configconfig=(Config)configobj;
longinterval=config.getInterval();
StringtimerInfo=config.getTimerInfo();
this.stopTimer(timerInfo);
System.out.println("startatimerandinfo="+timerInfo+",interval="+interval);
TimerServicets=this.getSessionContext().getTimerService();
Timertimer=ts.createTimer(newDate(),interval,timerInfo);
}
/*(non-Javadoc)
*@seejavax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
*/
publicvoidejbTimeout(Timerarg0)
{
//TODOAuto-generatedmethodstub
System.out.println("inejbTimeoutnow.");
TaskHandler.process();
}
}
分享到:
相关推荐
EJB2.1/EJ2EE 规范教程,个人觉得很不错的
从EJB 2.1迁移到EJB 3.0 的實例
EJB 2.1 入门操作实践,自己录制的,方便同学们学习。
EJB 2.1 技术规范pdf文档
EJB3.0规范 EJB3.0 SPECIFICATION 官方规范英文版
EJB 2.1中实现Web Service.doc
EJB3.0规范和EJB3.0经典入门(含源码) 本材料适合Java程序员、项目经理和系统架构师。学习本材料,您不需要EJB2.x知识,如果具备一些Web和jdbc知识,学习效率会更快些。学习本材料,初级Java程序员应在1个月左右,...
weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0weblogic+ejb3.0
ejb3.0开发规范,开发ejb3.0过程中的参考文档
EJB开发文档 EJB开发文档
ejb 3.0 jar 包 很全ejb 3.0 jar 包 很全ejb 3.0 jar 包 很全
ejb2.0 ejb3.0的区别,比较详细讲解两者的区别
EJB3.0中文文档,实例教程.EJB3.0中文文档,实例教程.EJB3.0中文文档,实例教程.EJB3.0中文文档,实例教程.
EJB 3.0 开发指南之定时服务非常经典
压缩包中包含:EJB3.0实例教程 JPA教程 实战EJB三本电子书,都是不错的学习资料。
ejb3.pptejb3.pptejb3.pptejb3.ppt
ejb3-persistence-3.0.jar 持久化的时候使用
EJb3.0.doc EJb3.0.doc EJb3.0.doc EJb3.0.doc EJb3.0.doc EJb3.0.doc EJb3.0.doc EJb3.0.doc
《EJB3.0入门经典》是关于EJB 3.0的专业技术教程,从实用的角度出发,理论联系实际,用9章的篇幅详细讲解了EJB 3.0开发的方法和技巧。《EJB3.0入门经典》内容丰富,讲解由浅入深,全面系统,在讲解EJB 3.0最新开发...
ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结ejb3.0总结