KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > plugins > coordinators > DynamicQueuesCoordinator


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 package org.mr.plugins.coordinators;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.Collection JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.Set JavaDoc;
52
53 import org.apache.commons.logging.Log;
54 import org.apache.commons.logging.LogFactory;
55 import org.mr.MantaAgent;
56 import org.mr.api.jms.MantaConnection;
57 import org.mr.core.configuration.ConfigurationElement;
58 import org.mr.core.util.SystemTime;
59 import org.mr.kernel.services.MantaService;
60 import org.mr.kernel.services.queues.QueueMaster;
61 import org.mr.kernel.world.WorldModeler;
62
63 /**
64  * Looks for queues that need coordination, a queue will be coordinated if this peer is a producer of the
65  * queue and there is no other coordinator
66  *
67  * @author Amir Shevat
68  *
69  */

70 public class DynamicQueuesCoordinator extends Thread JavaDoc {
71     public static final String JavaDoc COORDINATED_QUEUES_KEY = "plug-ins.queues-coordinator.queue";
72     public static final String JavaDoc EXCLUDE_QUEUES_KEY = "plug-ins.queues-coordinator.exclude_prefix";
73     
74     public static Log log;
75     boolean go = true;
76     public static MantaAgent agent;
77     public static WorldModeler world;
78     
79     public static ArrayList JavaDoc excludeList = new ArrayList JavaDoc();
80     
81     public DynamicQueuesCoordinator(){
82         log=LogFactory.getLog("DynamicQueuesCoordinator");
83         agent = QueuesCoordinator.agent;
84         world = agent.getSingletonRepository().getWorldModeler();
85         this.setName("DynamicQueuesCoordinator");
86         // build the excludeList
87
ArrayList JavaDoc queueList= MantaAgent.getInstance()
88         .getSingletonRepository().getConfigManager()
89             .getConfigurationElements(EXCLUDE_QUEUES_KEY);
90     
91         // parse the queues name
92
if(queueList!= null && queueList.size()>0 ){
93             for (int i = 0; i < queueList.size(); i++) {
94                 excludeList.add(((ConfigurationElement)queueList.get(i)).getValue()) ;
95             }
96         }
97     }
98     /**
99      * runs and looks for queues that need coordination
100      */

101     public void run(){
102         
103         while(go){
104             // we need to sleep between 2000 and 5000 millis
105
long rand = System.currentTimeMillis();
106             rand =rand%3000;
107             rand = rand +2000;
108             try {
109                 sleep(rand);
110                 Set JavaDoc servicesSet =world.getServices(world.getDefaultDomainName());
111                 if(servicesSet != null){
112                     Iterator JavaDoc services =servicesSet.iterator();
113                     int i = 0;
114                     while(services.hasNext()){
115                         i++;
116                         MantaService service = (MantaService) services.next();
117                         if(service.getServiceType() ==MantaService.SERVICE_TYPE_QUEUE ){
118                             //this is not a temp queue
119
QueueMaster coordinator= agent.getSingletonRepository().getVirtualQueuesManager().getQueueMaster(service.getServiceName());
120                             if(coordinator == null || (coordinator.getValidUntil()<SystemTime.currentTimeMillis())){
121                                 // if i am a producer and there is no coordinator then start a coordinator
122
Collection JavaDoc producers = service.getProducersByAgentId(agent.getAgentName());
123                                 if(producers != null && producers.size() >0 && !isExcluded(service.getServiceName())){
124                                     QueueMaster myCoordinator = new QueueMaster(agent.getAgentName(),service.getServiceName() );
125                                     if (log.isInfoEnabled()) {
126                                         log.info("Starting to coordinate queue "+myCoordinator);
127                                     }
128                                     agent.advertiseService(myCoordinator);
129                                     QueuesCoordinator.queueToCoordinatorMap.put(service ,myCoordinator );
130                                 }
131                             }//if
132
}//if
133
}//while
134
}//if
135
} catch (Exception JavaDoc e) {
136                 if (log.isErrorEnabled()) {
137                     log.error("Error in DynamicQueuesCoordinator",e);
138                 }
139             }//try
140
}//while
141
}//run
142

143     
144     public static boolean isExcluded(String JavaDoc queueName){
145         if(queueName.startsWith(MantaConnection.TMP_DESTINATION_PREFIX)){
146             return true;
147         }
148         for (int i = 0; i < excludeList.size(); i++) {
149             String JavaDoc exclude = (String JavaDoc) excludeList.get(i);
150             if(exclude != null && queueName.startsWith(exclude)){
151                 return true;
152             }
153         }
154         return false;
155     }
156
157 }
158
Popular Tags