1 22 package org.jboss.resource.adapter.mail.inflow; 23 24 import javax.resource.spi.work.Work ; 25 import javax.resource.spi.work.WorkManager ; 26 import javax.resource.spi.work.WorkException ; 27 import javax.resource.spi.work.WorkEvent ; 28 import javax.resource.spi.work.WorkListener ; 29 30 import EDU.oswego.cs.dl.util.concurrent.BoundedPriorityQueue; 31 import org.jboss.logging.Logger; 32 33 37 public class NewMsgsWorker 38 implements Work , WorkListener 39 { 40 private static Logger log = Logger.getLogger(NewMsgsWorker.class); 41 private boolean released; 42 private WorkManager mgr; 43 private BoundedPriorityQueue pollQueue; 44 private boolean trace; 45 46 public NewMsgsWorker(WorkManager mgr) 47 { 48 this.mgr = mgr; 49 this.pollQueue = new BoundedPriorityQueue(1024); 51 this.trace = log.isTraceEnabled(); 52 } 53 54 public void watch(MailActivation activation) throws InterruptedException 55 { 56 long now = System.currentTimeMillis(); 57 activation.updateNextNewMsgCheckTime(now); 58 pollQueue.put(activation); 59 } 60 61 public void release() 62 { 63 released = true; 64 if( trace ) 65 log.trace("released"); 66 } 67 public void run() 68 { 69 if( trace ) 70 log.trace("Begin run"); 71 while( released == false ) 72 { 73 try 74 { 75 MailActivation ma = (MailActivation) pollQueue.take(); 76 if( ma.isReleased() ) 77 continue; 78 long now = System.currentTimeMillis(); 80 long nextTime = ma.getNextNewMsgCheckTime(); 81 long sleepMS = nextTime - now; 82 Thread.sleep(sleepMS); 83 if( released ) 84 break; 85 86 mgr.scheduleWork(ma, WorkManager.INDEFINITE, null, this); 88 } 89 catch(InterruptedException e) 90 { 91 log.warn("Interrupted waiting for new msg check", e); 92 } 93 catch (WorkException e) 94 { 95 log.warn("Failed to schedule new msg check", e); 96 } 97 } 98 if( trace ) 99 log.trace("End run"); 100 } 101 102 public void workAccepted(WorkEvent e) 104 { 105 if( trace ) 106 log.trace("workAccepted, e="+e); 107 } 108 109 public void workRejected(WorkEvent e) 110 { 111 if( trace ) 112 log.trace("workRejected, e="+e); 113 } 114 115 public void workStarted(WorkEvent e) 116 { 117 if( trace ) 118 log.trace("workStarted, e="+e); 119 } 120 121 public void workCompleted(WorkEvent e) 122 { 123 if( trace ) 124 log.trace("workCompleted, e="+e); 125 MailActivation activation = (MailActivation) e.getWork(); 126 try 127 { 128 watch(activation); 129 } 130 catch(InterruptedException ex) 131 { 132 log.warn("Failed to reschedule new msg check", ex); 133 } 134 } 135 137 } 138 | Popular Tags |