EJB3.0 TimerService的使用

package com.easyway.tbs;
/**
 * TimerService的使用
 * @author longgangbai
 *
 */
public interface TimerServiceDAO {
 public void scheduleTimer(long milliseconds);
}

 

package com.easyway.tbs;

import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
/**
 * TimerService的使用
 * @author longgangbai
 *
 */
@Stateless
@Remote(TimerServiceDAO.class)
public class TimerServiceBean implements TimerServiceDAO {
 private static int count=0;
 @Resource
 private TimerService timerService; //直接注入定时服务
 public void scheduleTimer(long milliseconds){
   if(count==0){
     count=1;
     timerService.createTimer(new Date((new Date()).getTime()+milliseconds),milliseconds,"大家好,这是我的第一个定时器");
   }
 }
 
 @Timeout
 public void timeroutHandler(Timer timer){
   System.out.println("========第"+count+"次==============");
   System.out.println("定时器事件发生,传进的参数为:"+timer.getInfo());
      if(count>=5)
      {
        timer.cancel();
        count=0;
      }else{
       count++;
      }
  
 }
}

你可能感兴趣的:(ejb)