1 46 package org.mr.kernel.services; 47 48 import java.util.HashSet ; 49 import java.util.Iterator ; 50 import java.util.Set ; 51 52 53 54 import org.apache.commons.logging.LogFactory; 55 import org.mr.MantaAgent; 56 import org.mr.kernel.services.queues.QueueMaster; 57 import org.mr.kernel.world.WorldModeler; 58 59 64 public class ServiceRecallShutdownHook extends Thread { 65 public void run() { 66 int consumerNum = 0; 67 int producerNum = 0; 68 int coordinatorNum = 0; 69 try { 70 MantaAgent manta = MantaAgent.getInstance(); 71 WorldModeler world =manta.getSingletonRepository().getWorldModeler(); 72 Set services =world.getMyConsumedServices(); 73 Iterator serviceItr = services.iterator(); 74 while(serviceItr.hasNext()){ 75 MantaService service =(MantaService) serviceItr.next(); 76 77 Iterator actors = service.getConsumersByAgentId(manta.getAgentName()).iterator(); 78 while(actors.hasNext()){ 79 ServiceConsumer actor = (ServiceConsumer) actors.next(); 80 try { 82 consumerNum++; 83 manta.recallService(actor); 84 } catch (Throwable e) { 85 LogFactory.getLog("ServiceRecallShutdownHook").error("problem in ShutdownHook",e); 86 } 87 } 89 90 } services =world.getMyProducedServices(); 92 93 serviceItr = services.iterator(); 94 while(serviceItr.hasNext()){ 95 MantaService service =(MantaService) serviceItr.next(); 96 97 Iterator actors = service.getProducersByAgentId(manta.getAgentName()).iterator(); 98 while(actors.hasNext()){ 99 ServiceProducer actor = (ServiceProducer) actors.next(); 100 try { 102 producerNum++; 103 manta.recallService(actor); 104 } catch (Throwable e) { 105 LogFactory.getLog("ServiceRecallShutdownHook").error("problem in ShutdownHook",e); 106 } 107 } 109 110 } 112 services =world.getMyCoordinatedServices(); 113 114 serviceItr = services.iterator(); 115 while(serviceItr.hasNext()){ 116 MantaService service =(MantaService) serviceItr.next(); 117 118 QueueMaster actor = manta.getSingletonRepository().getVirtualQueuesManager().getQueueMaster(service.getServiceName()); 119 if(actor.getAgentName().equals(manta.getAgentName())){ 120 try { 121 coordinatorNum++; 122 manta.recallService(actor); 123 } catch (Throwable e) { 124 LogFactory.getLog("ServiceRecallShutdownHook").error("problem in ShutdownHook",e); 125 } 126 } } 128 int timeToSleep = 100; 129 String shutdownHookSleep = System.getProperty("sdhs"); 130 if(shutdownHookSleep !=null){ 131 timeToSleep = Integer.parseInt(shutdownHookSleep); 132 } 133 sleep(timeToSleep); 134 } catch (Throwable e) { 135 LogFactory.getLog("ServiceRecallShutdownHook").error("problem in ShutdownHook",e); 136 } 137 LogFactory.getLog("ServiceRecallShutdownHook").info("Recalled "+consumerNum+" service consumers , "+producerNum+" service producers and "+coordinatorNum+" coordinators. (So long, and thanks for all the fish)"); 138 } 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 } 155 | Popular Tags |