http://computer.mblogger.cn/sammi_tea
http://www.weste.net/html/200310/20031029QBI124658.html
使用Timer Service 可以在某一指定时间到了,或周期性Schedule开始时,通知EJB执行指定操作。为了使用Timer Service,EJB需要实现javax.ejb.TimedObject接口。
public interface TimedObject {
public void ejbTimeout(Timer timer) ;
}
例如:当时间到,EntityBean执行数据更新操作
public abstract class ShipBean implements javax.ejb.EntityBean, javax.ejb.TimedOut {
javax.ejb.EntityContext ejbContext;
public void setEntityContext(javax.ejb.EntityContext ctxt){
ejbContext = ctxt;
}
public void ejbTimedout(javax.ejb.Timer timer) {
// business logic for timer goes here
}
。。。。
从上面的使用例子看ejbTimedout是一个回调方法,执行具体的商业逻辑,那么怎样设置什么时间触发这个方法呢,我们利用javax.ejb.TimerSevice。该对象我们可以从EJBContext中获得该对象实例。为此我们在上面的例子中添加一个ejbHome方法scheduleMaintenance。
public inteface ShipBeanHome implements javax.ejb.EJBHome {
ShipBeanRemote create() throws CreateException, RemoteException;
void scheduleMaintenance(String desc, Date scheduleDate) throws RemoteException;
……
}
public abstract class ShipBean implements javax.ejb.EntityBean, javax.ejb.TimedOut {
javax.ejb.EntityContext ejbContext;
public void setEntityContext(javax.ejb.EntityContext ctxt){
ejbContext = ctxt;
}
public void ejbScheduleMaintenance(String desc, Date scheduleDate) {
TimerService service = ejbContext.getTimerService();
Service.createTime(scheduleDate, desc);
}
public void ejbTimedout(javax.ejb.Timer timer) {
// business logic for timer goes here
}
在TimerService中提供了设置了Timer的方法,包括设置一个时间点(ejbTimedOut方法运行一次)和时间间隔(ejbTimedOut方法运行多次)。另外,它还提供了getTimer()的方法,该方法返回一个Collection,里面包含了已经注册了的所有Timer对象。
Timer接口
public interface Timer {
// Cause the timer and all its associated expiration notifications to be canceled
public void cancel()
throws IllegalStateException,NoSuchObjectLocalException,EJBException;
// Get the information associated with the timer at the time of creation.
public java.io.Serializable getInfo()
throws IllegalStateException,NoSuchObjectLocalException,EJBException;
// Get the point in time at which the next timer expiration is scheduled to occur.
public java.util.Date getNextTimeout()
throws IllegalStateException,NoSuchObjectLocalException,EJBException;
// Get the number of milliseconds that will elapse before the next scheduled timer expiration
public long getTimeRemaining()
throws IllegalStateException,NoSuchObjectLocalException,EJBException;
//Get a serializable handle to the timer.
public TimerHandle getHandle()
throws IllegalStateException,NoSuchObjectLocalException,EJBException;
}
Cancel方法用来取消一个已经无效的Timer(时间点已经过去)。
GetInfo方法获得一个Timer的相关信息,这些信息在createTimer时作为参数和Timer联系在一起。在前面的例子中,为了防止客户胡乱调用ejbHome方法scheduleMaintenance,我们可以为每一个设定的时间添加一个唯一编号。修改后的代码如下:
public class MaintenanceItem implements java.io.Serializable {
private long maintenanceItemNumber;
private String description;
public MaintenanceItem(long min, String desc){
maintenanceItemNumber = min;
description = desc;
}
public long getMIN(){
return maintenanceItemNumber;
}
public String getDescription(){
return description;
}
public void scheduleMaintenance(MaintenanceItem maintenanceItem, Date dateOfTest){
TimerService timerService = ejbContext.getTimerService();
java.util.Iterator timers = timerService.getTimers().iterator();
while( timers.hasNext() ){
javax.ejb.Timer timer = (javax.ejb.Timer) timers.next();
String timerMainItem = (MaintenanceItem) timer.getInfo();
if( maintenanceItem.getMIN() == timerMainItem.getMIN() )
timer.cancel();
}
}
timerService.createTimer( dateOfText, maintenanceItem);
}
TimerService的Transaction
TimerService可以参与Transaction,当createTimer如果在Transaction中,如果该transaction rollback则创建的Timer也会取消。