KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > util > ComplexStage


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 /*
47  * Created on Dec 31, 2003
48  * Manta LTD
49  */

50 package org.mr.core.util;
51
52 import org.apache.commons.logging.LogFactory;
53 import org.mr.core.persistent.PersistentQueue;
54
55 /**
56  * The same as Stage, but with N number of queues.
57  * queue 1 is the most important queue and N is the least important queue.
58  * @see AbstractStage
59  *
60  * Created Dec 31, 2003
61  * Ver 1.0
62  * @author Amir Shevat
63  */

64 public class ComplexStage extends AbstractStage {
65     Queue[] queues;
66     
67     /**
68      * Constructs a ComplexStage.
69      * @param params The parameters of the complex stage.
70      */

71     public ComplexStage(ComplexStageParams params){
72         super(params);
73         queues= new Queue[params.numberOfQueues];
74         for (int i = 0; i < params.numberOfQueues; i++) {
75             if(params.isPersistent()){
76                 queues[i] = new PersistentQueue(params.getStageName()+i,true, params.isBlocking());
77             }else{
78                 queues[i] = new SynchronizedQueue();
79             }//if
80

81     
82         }//for
83

84         for(int index =0 ; index < params.getNumberOfStartThreads() ; index++){
85             StageExecutionThread exe = new StageExecutionThread(this);
86             exe.setName(params.getStageName()+"ExecutionThread"+(index+1));
87             exe.start();
88             stageExecutionThreads.add(exe);
89         }
90     }
91     
92     /**
93      * @return An element from the first ready most important queue
94      */

95     protected synchronized Object JavaDoc dequeue(){
96         //return select().dequeue();
97

98         Queue result = null;
99         
100         while((result=allQueuesEmpty())==null) {
101             try {
102                 wait();
103             }//try
104
catch (InterruptedException JavaDoc ex){
105                 if(LogFactory.getLog("ComplexStage").isFatalEnabled()){
106                     LogFactory.getLog("ComplexStage").fatal("Stage Queue has got an Exception." , ex);
107                 }//if
108
}//catch
109
}//while
110
//if (result!=null)
111
return result.dequeue();
112         //throw new RuntimeException("queue select fail ");
113
}//dequeue
114

115     /**
116      * @return The first ready most queue.
117
118     private synchronized Queue select(){
119         Queue result = null;
120         
121         while((result=allQueuesEmpty())==null) {
122             try {
123                 wait();
124             } catch (InterruptedException ex){
125                 if(LogFactory.getLog("ComplexStage").isFatalEnabled()){
126                     LogFactory.getLog("ComplexStage").fatal("Stage Queue has got an Exception" , ex);
127                 }
128             }
129         }
130         if (result!=null)
131             return result;
132 // for (int i = 0; i < queues.length; i++) {
133 // if(!queues[i].isEmpty())
134 // return queues[i];
135 // }
136         throw new RuntimeException("Queue select fail ");
137     }
138     
139          */

140     /**
141      * @return True if all queues are empty else false.
142      */

143     private final Queue allQueuesEmpty(){
144         for (int i = 0; i < queues.length; i++) {
145             if(!queues[i].isEmpty()) {
146                 return queues[i];
147             }//if
148
}//for
149
return null;
150     }//allQueuesEmpty
151

152     /**
153      * Puts a logical event in the queue, the handler will take care of this event.
154      * @param event The logical event
155      * @param queueNumber The queue number to enqueue in.
156      */

157     public synchronized void enqueue(Object JavaDoc event , int queueNumber){
158         queues[queueNumber].enqueue(event);
159         notifyAll();
160     }
161     
162 }
163
Popular Tags