KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > threadpool > TaskQueue


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //NOTE: Tabs are used instead of spaces for indentation.
25
// Make sure that your editor does not replace tabs with spaces.
26
// Set the tab length using your favourite editor to your
27
// visual preference.
28

29 /*
30  * $RCSfile: TaskQueue.java,v $
31  *
32  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
33  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
34  * All rights reserved.
35  *
36  * This software is the confidential and proprietary information
37  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
38  * You shall not disclose such Confidential Information and shall
39  * use it only in accordance with the terms of the license
40  * agreement you entered into with iPlanet/Sun Microsystems.
41  */

42
43 /**
44  * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/threadpool/TaskQueue.java,v $</I>
45  * @author $Author: tcfujii $
46  * @version $Revision: 1.3 $ $Date: 2005/12/25 04:12:33 $
47  */

48  
49 package com.sun.enterprise.util.threadpool;
50
51 import com.sun.enterprise.util.collection.BlockingQueue;
52 import com.sun.enterprise.util.collection.TooManyTasksException;
53 import com.sun.enterprise.util.collection.QueueClosedException;
54 import java.util.ArrayList JavaDoc;
55
56 /**
57  * TaskQueue provides the user the ability to queue his objects without
58  * worring about the usage of the TaskQueue by the ThreadPool. The
59  * responsibility of the Task creation is now delegated to the TaskFactory.
60  * TaskFactory will create or pool the task objects that wrap the users
61  * object. The tasks need to implement the Serviceable interface
62  */

63 public class TaskQueue extends BlockingQueue
64 {
65     
66     private TaskFactory taskFactory;
67     
68     /**
69      * Create a TaskQueue that has an infinite queuelength with the
70      * specified timeout.
71      * @param The maximum time remove() will block.
72      * @see remove()
73      */

74     public TaskQueue(TaskFactory factory) {
75         super();
76         taskFactory = factory;
77     }
78     
79     /**
80      * Create a TaskQueue that has the specified queue limit with the
81      * specified timeout.
82      * @param The maximum time remove() will block.
83      * @param The queue length after which TooManyTasksException is thrown.
84      * @see remove()
85      */

86     public TaskQueue(int queueLimit, TaskFactory factory) {
87         super(queueLimit);
88         taskFactory = factory;
89     }
90     
91     /**
92      * Add to the head of the queue. Probably a high priority job?
93      */

94     public void addFirst(Object JavaDoc o)
95         throws TooManyTasksException, QueueClosedException
96     {
97         super.addFirst(taskFactory.createTask(o));
98     }
99
100     /**
101      * Add to the head of the queue. Probably a high priority job?
102      */

103     public void addFirst(Servicable ser)
104         throws TooManyTasksException, QueueClosedException
105     {
106         super.addFirst(ser);
107     }
108     
109     /**
110      * Add to the tail of the queue.
111      */

112     public void addLast(Object JavaDoc o)
113         throws TooManyTasksException, QueueClosedException
114     {
115         super.addLast(taskFactory.createTask(o));
116     }
117     
118     /**
119      * Add to the tail of the queue.
120      */

121     public void addLast(Servicable ser)
122         throws TooManyTasksException, QueueClosedException
123     {
124         super.addLast(ser);
125     }
126     
127     /**
128      * Add the job at the specified position. Probably based on priority?
129      */

130     public void add(int index, Object JavaDoc object)
131         throws TooManyTasksException, QueueClosedException
132     {
133         super.add(index, taskFactory.createTask(object));
134     }
135     
136     public void addAll(ArrayList JavaDoc arrayList)
137         throws TooManyTasksException, QueueClosedException
138     {
139         for (int i=0; i < arrayList.size(); i++) {
140             Object JavaDoc o = taskFactory.createTask(arrayList.get(i));
141             arrayList.set(i, o);
142         }
143         super.addAll(arrayList);
144     }
145     
146     /**
147      * Hook to enable factory to deleteTask. The deleteTask
148      * can potentially return the object to some pool
149      */

150     public void destroyTask(Object JavaDoc o)
151     {
152         taskFactory.deleteTask(o);
153     }
154 }
155
Popular Tags