KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > body > request > BlockingRequestQueueImpl


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

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 JavaDoc,BlockingRequestQueue {
38
39     public static Logger logger = Logger.getLogger(BlockingRequestQueueImpl.class.getName());
40
41   //
42
// -- PROTECTED MEMBERS -----------------------------------------------
43
//
44

45   protected boolean shouldWait;
46   
47   //
48
// -- CONSTRUCTORS -----------------------------------------------
49
//
50

51   public BlockingRequestQueueImpl(UniqueID ownerID) {
52     super(ownerID);
53     shouldWait = true;
54   }
55
56
57   //
58
// -- PUBLIC METHODS -----------------------------------------------
59
//
60

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 JavaDoc 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 JavaDoc 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 JavaDoc e) {}
123     }
124   }
125
126
127
128   //
129
// -- PRIVATE METHODS -----------------------------------------------
130
//
131

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 JavaDoc e) {}
140       r = oldest ? removeOldest(requestFilter) : removeYoungest(requestFilter);
141     }
142     return r;
143   }
144
145
146   /**
147    * Blocks the calling thread until there is a request of name methodName
148    * Returns immediately if there is already one. The request returned is non
149    * null unless the thread has been asked not to wait anymore.
150    * @param methodName the name of the method to wait for
151    * @param oldest true if the request to remove is the oldest, false for the youngest
152    * @return the request of name methodName found in the queue.
153    */

154   protected Request blockingRemove(String JavaDoc 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 JavaDoc e) {}
162       r = oldest ? removeOldest(methodName) : removeYoungest(methodName);
163     }
164     return r;
165   }
166
167
168   /**
169    * Blocks the calling thread until there is a request available
170    * Returns immediately if there is already one. The request returned is non
171    * null unless the thread has been asked not to wait anymore.
172    * @param oldest true if the request to remove is the oldest, false for the youngest
173    * @return the request found in the queue.
174    */

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 JavaDoc e) {}
183     }
184     return oldest ? removeOldest() : removeYoungest();
185   }
186
187
188   /**
189    * Blocks the calling thread until there is a request available but try
190    * to limit the time the thread is blocked to timeout.
191    * Returns immediately if there is already one. The request returned is non
192    * null if a request has been found during the given time.
193    * @param timeout the maximum time to wait
194    * @param oldest true if the request to remove is the oldest, false for the youngest
195    * @return the request found in the queue or null.
196    */

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 JavaDoc 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