1 21 22 package com.rift.coad.daemon.timer; 23 24 import java.io.Serializable ; 26 import java.rmi.RemoteException ; 27 import java.util.ArrayList ; 28 import java.util.Calendar ; 29 import java.util.Date ; 30 import java.util.List ; 31 import javax.naming.NamingException ; 32 import javax.rmi.PortableRemoteObject ; 33 import javax.transaction.HeuristicMixedException ; 34 import javax.transaction.HeuristicRollbackException ; 35 import javax.transaction.NotSupportedException ; 36 import javax.transaction.RollbackException ; 37 import javax.transaction.SystemException ; 38 import javax.naming.Context ; 39 import javax.naming.InitialContext ; 40 import javax.transaction.UserTransaction ; 41 import javax.transaction.Status ; 42 43 import org.apache.log4j.Logger; 45 46 import com.rift.coad.lib.configuration.Configuration; 48 import com.rift.coad.lib.configuration.ConfigurationFactory; 49 import com.rift.coad.lib.common.CommonException; 50 import com.rift.coad.lib.common.ObjectSerializer; 51 import com.rift.coad.lib.bean.BeanRunnable; 52 import com.rift.coad.hibernate.util.HibernateUtil; 54 import com.rift.coad.daemon.timer.db.Schedule; 55 import com.rift.coad.lib.thread.ThreadStateMonitor; 56 import com.rift.coad.lib.thread.CoadunationThread; 57 58 import org.hibernate.Session; 60 import org.hibernate.Transaction; 61 62 69 public class TimerImpl implements Timer, BeanRunnable { 70 71 75 public class TimerThread extends CoadunationThread { 76 77 private Object [] row = null; 79 82 public TimerThread(Object [] row) throws Exception { 83 this.row = row; 84 } 85 86 90 public void process() { 91 String jndi = null; 92 UserTransaction ut = null; 93 try { 94 ut = (UserTransaction )ctx.lookup("java:comp/UserTransaction"); 95 ut.begin(); 96 97 Session session = HibernateUtil 98 .getInstance(com.rift.coad.daemon.timer.TimerImpl.class) 99 .getSession(); 100 101 jndi = (String ) row[0]; 102 byte[] b_event = (byte[]) row[1]; 103 byte recure = ((Byte ) row[2]).byteValue(); 104 int id = Integer.parseInt(row[3].toString()); 105 if (recure == 0) { 106 session.createQuery( 107 "DELETE FROM Schedule as sche WHERE " + 108 "sche.id = ?"). 109 setInteger(0,id).executeUpdate(); 110 } 111 Object obj = ctx.lookup(jndi); 112 com.rift.coad.daemon.timer.TimerEventHandler 113 beanInterface = 114 (com.rift.coad.daemon.timer.TimerEventHandler) 115 PortableRemoteObject.narrow(obj, 116 com.rift.coad.daemon.timer. 117 TimerEventHandler.class); 118 Serializable event = (Serializable ) 119 ObjectSerializer.deserialize(b_event); 120 beanInterface.processEvent(event); 121 122 ut.commit(); 123 } catch (Exception ex) { 124 log.warn("Failed to process timer event for [" + jndi 125 + "] because :" + ex.getMessage(), ex); 126 try { 127 ut.rollback(); 128 } catch (Exception ex2) { 129 log.error("Failed to rollback the transaction " + 130 "event :" + ex2.getMessage(), ex2); 131 } 132 } 133 } 134 135 136 139 public void terminate() { 140 } 142 } 143 144 private final static String TIMER_USERNAME = "timer_user"; 146 147 protected Logger log = 149 Logger.getLogger(TimerImpl.class.getName()); 150 151 private Context ctx = null; 152 private ThreadStateMonitor state = null; 153 private String username = null; 154 155 156 public TimerImpl() throws NamingException , TimerException { 157 ctx = new InitialContext (); 158 state = new ThreadStateMonitor(60000); 160 try { 161 Configuration config = ConfigurationFactory.getInstance(). 162 getConfig(TimerImpl.class); 163 username = config.getString(TIMER_USERNAME); 164 } catch (Exception ex) { 165 log.error("Failed to instanciate the timer : " + 166 ex.getMessage(),ex); 167 throw new TimerException("Failed to instanciate the timer : " + 168 ex.getMessage()); 169 } 170 } 171 172 189 public void register(String JNDI, int month, int day, int hour, int minute, 190 Serializable event, boolean recure) throws RemoteException , 191 TimerException { 192 try { 193 194 Session session = HibernateUtil 195 .getInstance(com.rift.coad.daemon.timer.TimerImpl.class) 196 .getSession(); 197 198 byte[] b_event; 199 byte recure2 = 0; 200 if (recure == false) { 201 recure2 = 0; 202 } else { 203 recure2 = 1; 204 } 205 b_event = ObjectSerializer.serialize(event); 206 Schedule schedule = new Schedule(JNDI,month,day,hour,minute,b_event, 207 recure2); 208 session.save(schedule); 209 210 log.info("Registered a new event for : " + JNDI); 211 212 } catch (Exception ex) { 213 log.error("Failed to register timer events :" 214 + ex.getMessage(),ex); 215 throw new TimerException("Failed to register timer events :" + 216 ex.getMessage()); 217 } 218 } 219 220 223 public void terminate() { 224 state.terminate(true); 225 } 226 227 228 233 public void process() { 234 while (!state.isTerminated()) { 235 state.monitor(); 237 if (state.isTerminated()) { 238 break; 240 } 241 242 UserTransaction ut = null; 243 try { 244 245 ut = (UserTransaction )ctx.lookup("java:comp/UserTransaction"); 246 247 ut.begin(); 248 249 Session session = HibernateUtil 250 .getInstance(com.rift.coad.daemon.timer.TimerImpl.class) 251 .getSession(); 252 253 Calendar currentDate = Calendar.getInstance(); 254 255 List list = session.createSQLQuery("SELECT Schedule.jndi, " + 256 "Schedule.event, Schedule.recure, Schedule.id FROM " + 257 "Schedule WHERE " + 258 "(month=? OR month=-1)" + 259 " AND " + 260 "(day=? OR day=-1)" + 261 " AND " + 262 "(hour=? OR hour=-1)" + 263 " AND " + 264 "(minute=? OR minute=-1)") 265 .setInteger(0,currentDate.get(Calendar.MONTH)) 266 .setInteger(1,currentDate.get(Calendar.DAY_OF_MONTH)) 267 .setInteger(2,currentDate.get(Calendar.HOUR)) 268 .setInteger(3,currentDate.get(Calendar.MINUTE)) 269 .list(); 270 271 java.util.ArrayList copy = new java.util.ArrayList (); 272 copy.addAll(list); 273 274 ut.commit(); 275 276 for (int index = 0; index < list.size() && !state.isTerminated(); 277 index++){ 278 TimerThread timerThread = new TimerThread((Object [])list. 279 get(index)); 280 timerThread.start(username); 281 } 282 283 } catch (Exception ex) { 284 log.error("Failed to process timer events :" + ex.getMessage(), 285 ex); 286 try { 287 ut.rollback(); 288 } catch (Exception ex2) { 289 log.error("Rollback failed because :" + ex2.getMessage(), 290 ex2); 291 } 292 } 293 } 294 } 295 296 297 305 public TimerEvent[] listEvents() throws RemoteException , TimerException { 306 String returnString = ""; 307 TimerEvent[] returnList = null; 308 try { 309 310 Session session = HibernateUtil 311 .getInstance(com.rift.coad.daemon.timer.TimerImpl.class) 312 .getSession(); 313 314 List list = session.createSQLQuery("SELECT id,jndi,month,day," + 315 "hour,minute,event,recure FROM Schedule ORDER BY id").list(); 316 317 returnList = new TimerEvent[list.size()]; 318 for (int i = 0; i < list.size(); i ++) { 319 Object [] row = (Object []) list.get(i); 320 TimerEvent tempEvent = new TimerEvent(); 321 tempEvent.setId(((Integer )row[0]).intValue()); 322 tempEvent.setJndi(row[1].toString()); 323 tempEvent.setMonth(((Integer )row[2]).intValue()); 324 tempEvent.setDay(((Integer )row[3]).intValue()); 325 tempEvent.setHour(((Integer )row[4]).intValue()); 326 tempEvent.setMinute(((Integer )row[5]).intValue()); 327 byte[] b_event = (byte[]) row[6]; 328 Serializable event = (Serializable ) 329 ObjectSerializer.deserialize(b_event); 330 tempEvent.setEvent(event); 331 if (((Byte )row[7]).byteValue() == 0) { 332 tempEvent.setRecure(false); 333 } else { 334 tempEvent.setRecure(true); 335 } 336 returnList[i] = tempEvent; 337 } 338 339 } catch (Exception ex) { 340 log.error("Failed to list timer events :" + ex.getMessage(), 341 ex); 342 throw new TimerException("Failed to list timer events :" + 343 ex.getMessage()); 344 } 345 return returnList; 346 } 347 348 354 public void deleteEvent(int eventId) throws RemoteException , 355 TimerException { 356 try { 357 log.info("Remove event Event id : " + eventId); 358 359 Session session = HibernateUtil 360 .getInstance(com.rift.coad.daemon.timer.TimerImpl.class) 361 .getSession(); 362 363 session.createQuery( 364 "DELETE FROM Schedule as sche WHERE sche.id = ?") 365 .setInteger(0,eventId) 366 .executeUpdate(); 367 368 } catch (Exception ex) { 369 log.error("Failed to delete timer event :" + ex.getMessage(), 370 ex); 371 throw new TimerException("Failed to delete timer event :" + 372 ex.getMessage()); 373 } 374 } 375 376 } 377 | Popular Tags |