1 64 65 70 package com.jcorporate.expresso.core.misc; 71 72 import com.jcorporate.expresso.core.db.DBException; 73 import com.jcorporate.expresso.core.registry.ExpressoThread; 74 import com.jcorporate.expresso.core.security.User; 75 import com.jcorporate.expresso.services.dbobj.Event; 76 import org.apache.log4j.Logger; 77 78 import java.util.Vector ; 79 80 81 87 public class EventHandler extends ExpressoThread { 88 private static Vector queue = new Vector (3); 89 private static EventHandler myInstance = null; 90 91 95 private static int idleTimes = 0; 96 private static int maxIdleTimes = 10; 97 98 102 private static int maxWrites = 5; 103 104 107 private static int sleepTime = 30; 108 private static Logger log = Logger.getLogger(EventHandler.class); 109 private static final String thisClass = EventHandler.class.getName() + "."; 110 111 114 public EventHandler() { 115 116 } 117 118 125 private synchronized void checkQueue() 126 throws DBException { 127 String myName = (thisClass + "checkQueue()"); 128 129 if (queue.size() == 0) { 130 idleTimes++; 131 132 if (log.isDebugEnabled()) { 133 log.debug("Event queue empty - idle " + idleTimes + " times"); 134 } 135 136 return; 137 } 138 if (log.isDebugEnabled()) { 139 log.debug("Checking event queue. " + queue.size() + " entries"); 140 } 141 142 EventQueueEntry oneEventQueueEntry = null; 143 144 try { 145 int writeCount = 0; 146 147 while (queue.size() > 0) { 150 oneEventQueueEntry = (EventQueueEntry) queue.firstElement(); 151 queue.removeElementAt(0); 152 153 if (oneEventQueueEntry.isNotice()) { 155 User myUser = new User(); 157 myUser.setDataContext(oneEventQueueEntry.getDataContext()); 158 myUser.setUid(oneEventQueueEntry.getUid()); 159 160 if (myUser.find()) { 161 myUser.notify(oneEventQueueEntry.getSubject(), 162 oneEventQueueEntry.getTheMessage(), 163 false , 164 oneEventQueueEntry.getAttachments()); 165 } else { 166 log.error("Unable to send notice to user " + 167 oneEventQueueEntry.getUid() + ": No such user"); 168 } 169 } else { 170 new Event(oneEventQueueEntry.getDataContext(), 172 oneEventQueueEntry.getTheEvent(), 173 oneEventQueueEntry.getTheMessage(), 174 oneEventQueueEntry.isSuccess()); 175 } 176 177 writeCount++; 178 179 if (writeCount > maxWrites) { 180 break; 181 } 182 } 183 184 } catch (Exception de) { 185 log.error("Error sending event from queue", de); 186 throw new DBException(myName + ":Error sending event from queue", de); 187 } 188 } 189 190 191 199 public static void Event(String dbName, String theEvent, String theMessage, boolean success) { 200 201 if (log.isDebugEnabled()) { 202 log.debug("New event queued:" + theEvent + ":" + theMessage); 203 } 204 205 EventQueueEntry newEntry = new EventQueueEntry(theEvent, theMessage, success); 206 newEntry.setDataContext(dbName); 207 queue.addElement(newEntry); 208 startUp(); 209 } 210 211 212 215 public static void flush() 216 throws DBException { 217 startUp(); 218 myInstance.checkQueue(); 219 } 220 221 229 public static void notify(String dbName, int uid, String subject, String message) { 230 notify(dbName, uid, subject, message, null); 231 } 232 233 242 public static void notify(String dbName, int uid, String subject, String message, 243 ByteArrayDataSource attachments[]) { 244 if (log.isDebugEnabled()) { 245 String msg = ""; 246 if (attachments != null) { 247 msg = " with " + attachments.length + " virtual data source attachments."; 248 } 249 250 log.debug("Queueing notice to user " + uid + " of '" + subject + "'" + msg); 251 } 252 253 EventQueueEntry newEntry = new EventQueueEntry("", message, true); 254 newEntry.setSubject(subject); 255 newEntry.setUid(uid); 256 newEntry.setNotice(true); 257 newEntry.setDataContext(dbName); 258 newEntry.setAttachments(attachments); 259 queue.addElement(newEntry); 260 startUp(); 261 } 262 263 266 public void run() { 267 super.run(); 268 log.info("Event Handler starts"); 269 try { 270 while (true) { 271 if (idleTimes >= maxIdleTimes) { 272 log.info("Event handler idle more than " + maxIdleTimes + 273 " times - shutting down"); 274 return; 275 } 276 try { 277 checkQueue(); 278 } catch (DBException ae) { 279 log.error("Unable to check event queue", ae); 280 } 281 282 yield(); 283 sleep(sleepTime * 1000); 284 } 285 286 } catch (InterruptedException ie) { 287 log.error("EventHandler was interrupted", ie); 288 } 289 } 290 291 295 public synchronized static void startUp() { 296 idleTimes = 0; 297 298 boolean restart = false; 299 300 if (myInstance == null) { 301 restart = true; 302 } else { 303 if (!myInstance.isAlive()) { 304 restart = true; 305 } 306 } 307 if (restart) { 308 myInstance = new EventHandler(); 309 myInstance.start(); 310 } 311 } 312 313 } 314 | Popular Tags |