1 31 package org.objectweb.proactive.core.body.migration; 32 33 import org.apache.log4j.Logger; 34 35 import org.objectweb.proactive.Body; 36 import org.objectweb.proactive.core.ProActiveException; 37 import org.objectweb.proactive.core.body.UniversalBody; 38 import org.objectweb.proactive.core.body.reply.ReplyReceiver; 39 import org.objectweb.proactive.core.body.reply.ReplyReceiverForwarder; 40 import org.objectweb.proactive.core.body.request.RequestReceiver; 41 import org.objectweb.proactive.core.body.request.RequestReceiverForwarder; 42 import org.objectweb.proactive.core.event.AbstractEventProducer; 43 import org.objectweb.proactive.core.event.MigrationEvent; 44 import org.objectweb.proactive.core.event.MigrationEventListener; 45 import org.objectweb.proactive.core.event.ProActiveEvent; 46 import org.objectweb.proactive.core.event.ProActiveListener; 47 import org.objectweb.proactive.core.node.Node; 48 import org.objectweb.proactive.core.node.NodeFactory; 49 import org.objectweb.proactive.core.runtime.ProActiveRuntime; 50 51 52 public class MigrationManagerImpl extends AbstractEventProducer 53 implements MigrationManager, java.io.Serializable { 54 protected static Logger logger = Logger.getLogger(MigrationManagerImpl.class.getName()); 55 56 public MigrationManagerImpl() { 60 super(true); 61 } 62 63 public Node checkNode(Node node) throws MigrationException { 70 if (node == null) { 71 throw new MigrationException( 72 "The RemoteNodeImpl could not be found"); 73 } 74 75 if (NodeFactory.isNodeLocal(node)) { 77 MigrationException me = new MigrationException("The given node " + 78 node.getNodeInformation().getURL() + 79 " is in the same virtual machine"); 80 if (hasListeners()) { 81 notifyAllListeners(new MigrationEvent(me)); 82 } 83 throw me; 84 } 85 return node; 86 } 87 88 public UniversalBody migrateTo(Node node, Body body) 89 throws MigrationException { 90 if (hasListeners()) { 91 notifyAllListeners(new MigrationEvent(body, 92 MigrationEvent.BEFORE_MIGRATION)); 93 } 94 try { 95 long l1 = 0; 96 if (logger.isDebugEnabled()) { 97 l1 = System.currentTimeMillis(); 98 } 99 100 ProActiveRuntime part = node.getProActiveRuntime(); 104 UniversalBody remoteBody = part.receiveBody(node.getNodeInformation() 105 .getName(), body); 106 107 if (logger.isDebugEnabled()) { 108 logger.debug("runtime = " + part); 109 logger.debug("remoteBody = " + remoteBody); 110 } 111 112 long l2 = 0; 116 if (logger.isDebugEnabled()) { 117 l2 = System.currentTimeMillis(); 118 logger.debug("Migration took " + (l2 - l1)); 119 } 120 if (hasListeners()) { 121 notifyAllListeners(new MigrationEvent(body, 122 MigrationEvent.AFTER_MIGRATION)); 123 } 124 return remoteBody; 125 } catch (ProActiveException e) { 126 MigrationException me = new MigrationException("Exception while sending the Object", 127 e.getTargetException()); 128 if (hasListeners()) { 129 notifyAllListeners(new MigrationEvent(me)); 130 } 131 throw me; 132 } 133 } 134 135 public void startingAfterMigration(Body body) { 136 if (hasListeners()) { 137 notifyAllListeners(new MigrationEvent(body, 138 MigrationEvent.RESTARTING_AFTER_MIGRATING)); 139 } 140 } 141 142 public RequestReceiver createRequestReceiver(UniversalBody remoteBody, 143 RequestReceiver currentRequestReceiver) { 144 return new RequestReceiverForwarder(remoteBody); 145 } 146 147 public ReplyReceiver createReplyReceiver(UniversalBody remoteBody, 148 ReplyReceiver currentReplyReceiver) { 149 return new ReplyReceiverForwarder(remoteBody); 150 } 151 152 public void addMigrationEventListener(MigrationEventListener listener) { 153 addListener(listener); 154 } 155 156 public void removeMigrationEventListener(MigrationEventListener listener) { 157 removeListener(listener); 158 } 159 160 protected void notifyOneListener(ProActiveListener listener, 164 ProActiveEvent event) { 165 MigrationEvent migrationEvent = (MigrationEvent) event; 166 MigrationEventListener migrationEventListener = (MigrationEventListener) listener; 167 switch (event.getType()) { 168 case MigrationEvent.BEFORE_MIGRATION: 169 migrationEventListener.migrationAboutToStart(migrationEvent); 170 break; 171 case MigrationEvent.AFTER_MIGRATION: 172 migrationEventListener.migrationFinished(migrationEvent); 173 break; 174 case MigrationEvent.MIGRATION_EXCEPTION: 175 migrationEventListener.migrationExceptionThrown(migrationEvent); 176 break; 177 case MigrationEvent.RESTARTING_AFTER_MIGRATING: 178 migrationEventListener.migratedBodyRestarted(migrationEvent); 179 break; 180 } 181 } 182 } 183 | Popular Tags |