1 31 package org.objectweb.proactive.core.body.request; 32 33 import org.apache.log4j.Logger; 34 import org.objectweb.proactive.core.UniqueID; 35 import org.objectweb.proactive.core.event.RequestQueueEvent; 36 37 public class BlockingRequestQueueImpl extends RequestQueueImpl implements java.io.Serializable ,BlockingRequestQueue { 38 39 public static Logger logger = Logger.getLogger(BlockingRequestQueueImpl.class.getName()); 40 41 45 protected boolean shouldWait; 46 47 51 public BlockingRequestQueueImpl(UniqueID ownerID) { 52 super(ownerID); 53 shouldWait = true; 54 } 55 56 57 61 public synchronized void destroy() { 62 super.clear(); 63 shouldWait = false; 64 notifyAll(); 65 } 66 67 public synchronized boolean isDestroyed() { 68 return ! shouldWait; 69 } 70 71 public synchronized void add(Request r) { 72 super.add(r); 73 if (logger.isDebugEnabled()) { 74 logger.debug("adding request " + r.getMethodName()); 75 } 76 this.notifyAll(); 77 } 78 79 public synchronized void addToFront(Request r) { 80 super.addToFront(r); 81 this.notifyAll(); 82 } 83 84 public synchronized Request blockingRemoveOldest(RequestFilter requestFilter) { 85 return blockingRemove(requestFilter, true); 86 } 87 88 public synchronized Request blockingRemoveOldest(String methodName) { 89 return blockingRemove(methodName, true); } 90 91 public synchronized Request blockingRemoveOldest() { 92 return blockingRemove(true); 93 } 94 95 public synchronized Request blockingRemoveOldest(long timeout) { 96 return blockingRemove(timeout, true); 97 } 98 99 public synchronized Request blockingRemoveYoungest(RequestFilter requestFilter) { 100 return blockingRemove(requestFilter, false); 101 } 102 103 public synchronized Request blockingRemoveYoungest(String methodName) { 104 return blockingRemove(methodName, false); 105 } 106 107 public synchronized Request blockingRemoveYoungest() { 108 return blockingRemove(false); 109 } 110 111 public synchronized Request blockingRemoveYoungest(long timeout) { 112 return blockingRemove(timeout, false); 113 } 114 115 public synchronized void waitForRequest() { 116 while (isEmpty() && shouldWait) { 117 if (hasListeners()) { 118 notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.WAIT_FOR_REQUEST)); 119 } 120 try { 121 this.wait(); 122 } catch (InterruptedException e) {} 123 } 124 } 125 126 127 128 132 protected Request blockingRemove(RequestFilter requestFilter, boolean oldest) { 133 Request r = oldest ? removeOldest(requestFilter) : removeYoungest(requestFilter); 134 while (r == null && shouldWait) { 135 if (hasListeners()) 136 notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.WAIT_FOR_REQUEST)); 137 try { 138 this.wait(); 139 } catch (InterruptedException e) {} 140 r = oldest ? removeOldest(requestFilter) : removeYoungest(requestFilter); 141 } 142 return r; 143 } 144 145 146 154 protected Request blockingRemove(String methodName, boolean oldest) { 155 Request r = oldest ? removeOldest(methodName) : removeYoungest(methodName); 156 while (r == null && shouldWait) { 157 if (hasListeners()) 158 notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.WAIT_FOR_REQUEST)); 159 try { 160 this.wait(); 161 } catch (InterruptedException e) {} 162 r = oldest ? removeOldest(methodName) : removeYoungest(methodName); 163 } 164 return r; 165 } 166 167 168 175 protected Request blockingRemove(boolean oldest) { 176 while (isEmpty() && shouldWait) { 177 if (hasListeners()) { 178 notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.WAIT_FOR_REQUEST)); 179 } 180 try { 181 this.wait(); 182 } catch (InterruptedException e) {} 183 } 184 return oldest ? removeOldest() : removeYoungest(); 185 } 186 187 188 197 protected Request blockingRemove(long timeout, boolean oldest) { 198 long timeStartWaiting = System.currentTimeMillis(); 199 while (isEmpty() && shouldWait) { 200 if (hasListeners()) { 201 notifyAllListeners(new RequestQueueEvent(ownerID, RequestQueueEvent.WAIT_FOR_REQUEST)); 202 } 203 try { 204 this.wait(timeout); 205 } catch (InterruptedException e) {} 206 if (System.currentTimeMillis() - timeStartWaiting > timeout) { 207 return oldest ? removeOldest() : removeYoungest(); 208 } 209 } 210 return oldest ? removeOldest() : removeYoungest(); 211 } 212 213 214 } 215 | Popular Tags |