1 22 package org.jboss.test.timer.ejb; 23 24 import java.util.Date ; 25 import javax.ejb.EJBException ; 26 import javax.ejb.EntityBean ; 27 import javax.ejb.EntityContext ; 28 import javax.ejb.TimedObject ; 29 import javax.ejb.Timer ; 30 import javax.ejb.TimerHandle ; 31 import javax.ejb.TimerService ; 32 33 import org.jboss.logging.Logger; 34 35 52 public class TimerEntityBean 53 implements EntityBean , TimedObject 54 { 55 56 60 private EntityContext mContext; 61 private Timer sTimer; 62 private int sCounter; 63 64 private Logger mLog = Logger.getLogger(this.getClass().getName()); 65 66 70 77 public void startSingleTimer(long pPeriod) 78 { 79 mLog.info("TimerEntityBean.startSingleTimer(), try to get a Timer Service from the Entity Context"); 80 TimerService lService = mContext.getTimerService(); 81 mLog.info("TimerEntityBean.startSingleTimer(), create a timer if not already done"); 82 if (sTimer == null) 83 { 84 sTimer = lService.createTimer(new Date (new Date ().getTime() + pPeriod), "TimerEntityBean"); 85 sCounter = 0; 86 } 87 else 88 { 89 throw new EJBException ("Timer is already set"); 90 } 91 } 92 93 101 public void startTimer(long pPeriod) 102 { 103 mLog.info("TimerEntityBean.startTimer(), try to get a Timer Service from the Entity Context"); 104 TimerService lService = mContext.getTimerService(); 105 mLog.info("TimerEntityBean.startTimer(), create a timer if not already done"); 106 if (sTimer == null) 107 { 108 sTimer = lService.createTimer(new Date (new Date ().getTime() + pPeriod), pPeriod, "TimerEntityBean"); 109 sCounter = 0; 110 } 111 else 112 { 113 throw new EJBException ("Timer is already set"); 114 } 115 } 116 117 120 public void stopTimer() 121 { 122 try 123 { 124 if (sTimer != null) 125 { 126 sTimer.cancel(); 127 } 128 else 129 { 130 throw new EJBException ("Timer is not available"); 131 } 132 } 133 finally 134 { 135 sTimer = null; 136 } 137 } 138 139 142 public int getTimeoutCount() 143 { 144 mLog.info("TimerEntityBean.getTimeoutCount(): " + sCounter); 145 return sCounter; 146 } 147 148 153 public Date getNextTimeout() 154 { 155 if (sTimer != null) 156 { 157 return sTimer.getNextTimeout(); 158 } 159 else 160 { 161 return null; 162 } 163 } 164 165 170 public long getTimeRemaining() 171 { 172 if (sTimer != null) 173 { 174 return sTimer.getTimeRemaining(); 175 } 176 else 177 { 178 return -1L; 179 } 180 } 181 182 187 public Object getInfo() 188 { 189 if (sTimer != null) 190 { 191 return (Object ) sTimer.getInfo(); 192 } 193 else 194 { 195 return null; 196 } 197 } 198 199 204 public TimerHandle getTimerHandle() 205 { 206 if (sTimer != null) 207 { 208 return sTimer.getHandle(); 209 } 210 else 211 { 212 return null; 213 } 214 } 215 216 public void ejbTimeout(Timer pTimer) 217 { 218 mLog.debug("ejbTimeout(), timer: " + pTimer); 219 sCounter++; 220 } 221 222 227 public String toString() 228 { 229 return "TimerEntityBean [ " + " ]"; 230 } 231 232 236 239 public Integer ejbCreate(Integer pk) 240 { 241 mLog.info("ejbCreate(" + pk + ")"); 242 return pk; 243 } 244 245 public void ejbPostCreate(Integer pk) 246 { 247 mLog.info("ejbPostCreate(" + pk + ")"); 248 } 249 250 253 public Integer ejbFindByPrimaryKey(Integer pk) 254 { 255 mLog.info("ejbFindByPrimaryKey(" + pk + ")"); 256 return pk; 257 } 258 259 public void ejbActivate() 260 { 261 mLog.info("ejbActivate"); 262 } 263 264 public void ejbLoad() 265 { 266 mLog.info("ejbLoad"); 267 } 268 269 public void ejbPassivate() 270 { 271 mLog.info("ejbPassivate"); 272 } 273 274 public void ejbRemove() 275 { 276 mLog.info("ejbRemove"); 277 } 278 279 public void ejbStore() 280 { 281 mLog.info("ejbStore"); 282 } 283 284 public void setEntityContext(EntityContext ctx) 285 { 286 mLog.info("setEntityContext"); 287 this.mContext = ctx; 288 } 289 290 public void unsetEntityContext() 291 { 292 mLog.info("unsetEntityContext"); 293 } 294 } 295 | Popular Tags |