1 31 package org.objectweb.proactive.examples.migration; 32 33 import java.io.Serializable ; 34 import java.net.InetAddress ; 35 import java.net.UnknownHostException ; 36 37 import org.apache.log4j.Logger; 38 import org.objectweb.proactive.ActiveObjectCreationException; 39 import org.objectweb.proactive.ProActive; 40 import org.objectweb.proactive.core.node.Node; 41 import org.objectweb.proactive.core.node.NodeException; 42 import org.objectweb.proactive.core.node.NodeFactory; 43 44 52 public class SimpleObjectMigration implements Serializable { 53 54 static Logger logger = Logger.getLogger(SimpleObjectMigration.class.getName()); 55 56 private static final int SLEEP_TIME = 9000; 57 58 private String name; private String hi = " say hello from "; 61 65 public SimpleObjectMigration() { 66 logger.info("SimpleObjectMigration> Empty constructor"); 67 } 68 69 75 public SimpleObjectMigration(String name) { 76 logger.info("SimpleObjectMigration> Constructor with a parameter : " + name); 77 this.name = name; 78 } 79 80 85 public String sayHello() { 86 logger.info("SimpleObjectMigration> sayHello()"); 87 String localhost = null; 88 try { 89 localhost = InetAddress.getLocalHost().toString(); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } 93 String sentence = name + hi + localhost; 94 logger.info("SimpleObjectMigration> sayHello() --> " + sentence); 95 return sentence; 96 } 97 98 104 public void moveTo(String t) { 105 try { 106 107 logger.info("SimpleObjectMigration> moveTo(" + t + ") " + "% start migration"); 108 ProActive.migrateTo(t); 109 logger.info("SimpleObjectMigration> moveTo(" + t + ") " + "% stop migration"); 110 } catch (Exception e) { 111 e.printStackTrace(); 112 } 113 } 114 115 127 public static void main(String [] args) { 128 String urlSourceNode = ""; 130 Node sourceNode = null; 131 132 String urlDestinationNode = ""; 134 Node destinationNode = null; 135 136 if (args.length == 2) { 138 urlSourceNode = args[0]; 139 urlDestinationNode = args[1]; 140 } else { 141 logger.info("USAGE : java SimpleObjectMigration " + "urlSourceNode urlDestinationNode "); 142 logger.info("Example : java SimpleObjectMigration " + "rmi://host1/Mynode1 jini://host2/MyNode2 "); 143 System.exit(1); 144 } 145 146 System.out.println( 147 "SimpleObjectMigration> main() > " 148 + "We are going to try to migrate a simple object from " 149 + urlSourceNode 150 + " to " 151 + urlDestinationNode); 152 153 try { 154 155 logger.info("SimpleObjectMigration> main() > " + "we try to get the source node : " + urlSourceNode); 156 157 sourceNode = NodeFactory.getNode(urlSourceNode); 158 159 logger.info("SimpleObjectMigration> main() > " + "we obtain the source node : " + urlSourceNode); 160 161 } catch (NodeException e) { 162 logger.info( 163 "SimpleObjectMigration> main() > " 164 + "Exception during the getting of " 165 + " the source node " 166 + urlSourceNode 167 + " (" 168 + e.getMessage() 169 + ")"); 170 e.printStackTrace(); 171 } 172 173 try { 174 175 logger.info( 176 "SimpleObjectMigration> main() > " + "we try to get the destination node : " + urlDestinationNode); 177 178 destinationNode = NodeFactory.getNode(urlDestinationNode); 179 180 logger.info("SimpleObjectMigration> main() > " + "we obtain the destination node : " + urlDestinationNode); 181 182 } catch (NodeException e) { 183 System.out.println( 184 "SimpleObjectMigration> main() > " 185 + "Exception during the getting of " 186 + " the destination node " 187 + urlDestinationNode 188 + " (" 189 + e.getMessage() 190 + ")"); 191 e.printStackTrace(); 192 } 193 194 logger.info( 195 "SimpleObjectMigration> main() > " + "We shows the state before" + " to create the Active Object"); 196 showIds(urlSourceNode, sourceNode, urlDestinationNode, destinationNode); 198 199 logger.info("SimpleObjectMigration> main() > " + "We try to create an simple active object"); 200 201 SimpleObjectMigration activeHello = null; 203 try { 204 String className = SimpleObjectMigration.class.getName(); 205 Object [] params = new Object [] { "Created by " + InetAddress.getLocalHost().toString()}; 206 207 activeHello = (SimpleObjectMigration) ProActive.newActive(className, params, sourceNode); 208 logger.info("SimpleObjectMigration> main() > " + "We created an simple active object"); 209 210 logger.info("SimpleObjectMigration> main() > " + "The simple active object want to say hello ;)"); 211 212 String helloAnswer = activeHello.sayHello(); 213 214 logger.info("SimpleObjectMigration> main() > " + "The simple active object said '" + helloAnswer + "'"); 215 216 } catch (UnknownHostException e) { 217 logger.info( 218 "SimpleObjectMigration> main() > " 219 + "Exception during the creation of the active object" 220 + " (" 221 + e.getMessage() 222 + ")"); 223 e.printStackTrace(); 224 } catch (ActiveObjectCreationException e) { 225 logger.info( 226 "SimpleObjectMigration> main() > " 227 + "Exception during the creation of the active object" 228 + " (" 229 + e.getMessage() 230 + ")"); 231 e.printStackTrace(); 232 } catch (NodeException e) { 233 logger.info( 234 "SimpleObjectMigration> main() > " 235 + "Exception during the creation of the active object" 236 + " (" 237 + e.getMessage() 238 + ")"); 239 e.printStackTrace(); 240 } 241 242 logger.info( 243 "SimpleObjectMigration> main() > " + "We show the state before" + " the migration of the Active Object"); 244 showIds(urlSourceNode, sourceNode, urlDestinationNode, destinationNode); 246 247 try { 248 logger.info("SimpleObjectMigration> main() > " + "begin sleep " + SLEEP_TIME + " ..."); 249 Thread.sleep(SLEEP_TIME); 250 logger.info("SimpleObjectMigration> main() > " + "... end of sleep " + SLEEP_TIME); 251 252 } catch (InterruptedException e2) { 253 } 254 255 logger.info("SimpleObjectMigration> main() > " + "migrate active object to " + urlDestinationNode); 256 257 logger.info( 258 "SimpleObjectMigration> main() > " + "We show the state after" + " the migration of the Active Object"); 259 260 logger.info(""); 261 262 activeHello.moveTo(urlDestinationNode); 264 265 logger.info(""); 266 267 try { 268 logger.info("SimpleObjectMigration> main() > " + "begin sleep " + SLEEP_TIME + " ..."); 269 Thread.sleep(SLEEP_TIME); 270 logger.info("SimpleObjectMigration> main() > " + "... end of sleep " + SLEEP_TIME); 271 272 } catch (InterruptedException e2) { 273 } 274 275 showIds(urlSourceNode, sourceNode, urlDestinationNode, destinationNode); 277 278 logger.info("SimpleObjectMigration> main() > " + "The simple active object want to say hello ;)"); 279 280 String helloAnswer = activeHello.sayHello(); 281 282 logger.info("SimpleObjectMigration> main() > " + "The simple active object said '" + helloAnswer + "'"); 283 284 logger.info("SimpleObjectMigration> main() > end of test"); 285 286 } 287 288 protected static void showIds( 289 String urlSourceNode, 290 Node sourceNode, 291 String urlDestinationNode, 292 Node destinationNode) { 293 try { 294 logger.info(""); 295 logger.info("SimpleObjectMigration> showIds() > "); 296 logger.info("-------- Ids on " + urlSourceNode + " ------"); 297 313 } catch (Exception e) { 314 logger.info( 315 "SimpleObjectMigration> showIds() > " 316 + "Exception during the of the node's state" 317 + " (" 318 + e.getMessage() 319 + ")"); 320 e.printStackTrace(); 321 } 322 } 323 } 324 | Popular Tags |