EJB2.1 & EJB3.0: Timer Service三部曲

本文介绍如何使用 EJB 2.1 的 TimerService 实现定时任务,通过无状态 Session Bean 和 TimedObject 接口管理调度任务。文章详细展示了定时任务的创建、启动及停止的方法,并提供了完整的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对于J2EE中的调度任务, 在ejb2.1推出前, 似乎只能采用一些非常途径:比如在servlet中new一个Timer.
我们急切的需要EJB的标准实现.
最近我们就遇到了这样的需求. 还好websphere升级到了6.0, 支持ejb2.1规范,在ejb2.1标准中, 提供了一个Timer Service的标准实现.
要实现Timer Service, 通常的做法就是让一个无状态sessionbean实现TimedObject接口.

  1. public interface TimedObject{   
  2.   public void ejbTimeout(Timer timer);   
  3. }  

一个最简单的Timer Service如下(由一个无状态sessionbean实现, home和local接口省略)

java 代码
  1. public class DemoTimerServiceBean implements javax.ejb.SessionBean, TimedObject    
  2. {   
  3.   
  4.  private javax.ejb.SessionContext mySessionCtx;   
  5.  /**  
  6.   * getSessionContext  
  7.   */  
  8.  public javax.ejb.SessionContext getSessionContext() {   
  9.   return mySessionCtx;   
  10.  }   
  11.  /**  
  12.   * setSessionContext  
  13.   */  
  14.  public void setSessionContext(javax.ejb.SessionContext ctx) {   
  15.   mySessionCtx = ctx;   
  16.  }   
  17.  /**  
  18.   * ejbCreate  
  19.   */  
  20.  public void ejbCreate() throws javax.ejb.CreateException    
  21.  {   
  22.  }   
  23.  /**  
  24.   * ejbActivate  
  25.   */  
  26.  public void ejbActivate() {   
  27.  }   
  28.  /**  
  29.   * ejbPassivate  
  30.   */  
  31.  public void ejbPassivate() {   
  32.  }   
  33.  /**  
  34.   * ejbRemove  
  35.   */  
  36.  public void ejbRemove()    
  37.  {   
  38.  }   
  39.   
  40.     
  41.  public void startTimer()   
  42.  {   
  43.   TimerService ts=this.getSessionContext().getTimerService();   
  44.   Timer timer = ts.createTimer(new Date(), 1000"timerInfo");    
  45.  }   
  46.  /* (non-Javadoc)  
  47.   * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)  
  48.   */  
  49.  public void ejbTimeout(Timer arg0)    
  50.  {   
  51.   // implements your business logic.   
  52.   System.out.println("process your business logic");   
  53.   TaskHandler.process();   
  54.      
  55.  }   
  56. }   
  57.   

其中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.

java 代码
  1. public void startTimer()   
  2. {   
  3.  TimerService ts=this.getSessionContext().getTimerService();   
  4.  Iterator timers = ts.getTimers().iterator();   
  5.  while(timers.hasNext())   
  6.  {   
  7.   Timer timer = (Timer)timers.next();   
  8.   String info = (String)timer.getInfo();   
  9.   System.out.println("timer info = " + info);   
  10.   if (info.equals("timerInfo"))   
  11.   {   
  12.    System.out.println("there is already a timer = timerInfo, so return");   
  13.    return;   
  14.   }   
  15.  }   
  16.   ts.createTimer(new Date(), 1000"timerInfo");   
  17. }   

贴一个完整的timer service实现供有需要的人参考, 包括启动, 停止timer service(由一个无状态sessionbean实现, home和local接口省略)

java 代码

 

  1. public class DemoTimerServiceBean implements javax.ejb.SessionBean, TimedObject    
  2. {   
  3.  private static final String CONGIG_BEAN_NAME = "configBean";    
  4.   
  5.  private javax.ejb.SessionContext mySessionCtx;   
  6.  /**  
  7.   * getSessionContext  
  8.   */  
  9.  public javax.ejb.SessionContext getSessionContext() {   
  10.   return mySessionCtx;   
  11.  }   
  12.  /**  
  13.   * setSessionContext  
  14.   */  
  15.  public void setSessionContext(javax.ejb.SessionContext ctx) {   
  16.   mySessionCtx = ctx;   
  17.  }   
  18.  /**  
  19.   * ejbCreate  
  20.   */  
  21.  public void ejbCreate() throws javax.ejb.CreateException    
  22.  {   
  23.  }   
  24.  /**  
  25.   * ejbActivate  
  26.   */  
  27.  public void ejbActivate() {   
  28.  }   
  29.  /**  
  30.   * ejbPassivate  
  31.   */  
  32.  public void ejbPassivate() {   
  33.  }   
  34.  /**  
  35.   * ejbRemove  
  36.   */  
  37.  public void ejbRemove()    
  38.  {   
  39.  }   
  40.   
  41.     
  42.  public void stopAllTimer()   
  43.  {   
  44.   TimerService ts=this.getSessionContext().getTimerService();   
  45.   Iterator timers = ts.getTimers().iterator();   
  46.   while(timers.hasNext())   
  47.   {   
  48.    Timer timer = (Timer)timers.next();   
  49.    timer.cancel();   
  50.   }   
  51.  }   
  52.     
  53.  public void stopTimer(String timerName)   
  54.  {   
  55.   TimerService ts=this.getSessionContext().getTimerService();   
  56.   Iterator timers = ts.getTimers().iterator();   
  57.   while(timers.hasNext())   
  58.   {   
  59.    Timer timer = (Timer)timers.next();   
  60.    String info = (String)timer.getInfo();   
  61.    System.out.println("timer info = " + info);   
  62.    if (info.equals(timerName))   
  63.    {   
  64.     System.out.println("there is already a timer = " + timerName + ", so we cancel it!");   
  65.     timer.cancel();   
  66.    }   
  67.   }   
  68.  }   
  69.     
  70.  public ArrayList getAllTimerInfo()   
  71.  {   
  72.   ArrayList timerList = new ArrayList();   
  73.   TimerService ts=this.getSessionContext().getTimerService();   
  74.   Iterator timers = ts.getTimers().iterator();   
  75.   while(timers.hasNext())   
  76.   {   
  77.    Timer timer = (Timer)timers.next();   
  78.    String info = (String)timer.getInfo();   
  79.    timerList.add(info);      
  80.   }   
  81.      
  82.   return timerList;   
  83.  }   
  84.     
  85.  public void startTimer()   
  86.  {   
  87.   Object configobj = MyBeanFactory.instance().getBean(CONGIG_BEAN_NAME);   
  88.   if (configobj == null)   
  89.   {   
  90.    System.out.println("Can't get configBean from spring config file. config == null");   
  91.    return;   
  92.   }   
  93.   Config config = (Config)configobj;   
  94.   long interval = config.getInterval();   
  95.   String timerInfo = config.getTimerInfo();   
  96.   this.stopTimer(timerInfo);   
  97.   System.out.println("start a timer and info = " + timerInfo + ", interval = " + interval);   
  98.   TimerService ts=this.getSessionContext().getTimerService();   
  99.   Timer timer = ts.createTimer(new Date(), interval, timerInfo);    
  100.  }   
  101.  /* (non-Javadoc)  
  102.   * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)  
  103.   */  
  104.  public void ejbTimeout(Timer arg0)    
  105.  {   
  106.   // TODO Auto-generated method stub   
  107.   System.out.println("in ejbTimeout now.");   
  108.   TaskHandler.process();   
  109.      
  110.  }   
  111. }   
  112.   

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值