|
![]() |
This program is a very simple one : it creates an active object that migrates between virtual machines. It is a extension of the previous client-server example, the server now being mobile.
The conditions for MigratableHello to be a migratable active object are :
- it must have a constructor without parameters : this is a result of a ProActive restriction : the active object having to implement a no-arg constructor. p>
- implement the Serializable interface (as it will be transferred through the network).>
Hello, the superclass, must be able to be serialized, in order to be transferred remotely. It does not have to implement directly java.io.Serializable, but its attributes should be serializable - or transient. For more information on this topic, check the manual.
We want to further enhance InitializedHello it by making migratable : we'd like to be able to move it across virtual machines.
Thus, we create a MigratableHello class, that derives from InitializedHello. This class will implement all the non-functionnal behavior concerning the migration, for which this example is created. The Hello class (and InitializedHello) is left unmodified.
Note that the migration has to be initiated by the active object itself. This explains why we have to write the moveTo method in the code of MigratableHello - i.e. a method that contains an explicit call to the migration primitive. (cf migration documentation)
MigratableHello also implements a factory method for instanciating itself as an
active object : static MigratableHello createMigratableHello(String : name)
The class diagram for the application is the following :
The code of the MigratableHello class is here.
MigratableHello derives from the Hello class from the previous example
MigratableHello being the active object itself, it has to :
- implement the Serializable interface
- provide a no-arg constructor
- provide an implementation for using ProActive's migration mechanism.
A new method getCurrentNodeLocation is added for the object to tell the node where it resides..
A factory static method is added for ease of creation.
The migration is initiated by the moveTo method :
/** method for migrating * @param destination_node destination node */ public void moveTo(String destination_node) { System.out.println("\n-----------------------------"); System.out.println("starting migration to node : " + destination_node); System.out.println("..."); try { // THIS MUST BE THE LAST CALL OF THE METHOD ProActive.migrateTo(destination_node); } catch (MigrationException me) { System.out.println("migration failed : " + me.toString()); } }
Note that the call to the ProActive primitive migrateTo
is the last one of the method
moveTo. See the manual for more information.
The entry point of the program is written in a separate class : MigratableHelloClient
It takes as arguments the locations of the nodes the object will be migrated to.
The program calls the factory method of MigratableHello to create an instance of an active object. It then moves it from node to node, pausing for a while between the transfers.
- start several nodes using the startnode
script.
- compile and run the program (run MigratableHelloClient), passing in parameter the urls of the nodes you'd like the agent to migrate to.
- don't forget to set the security permissions
- observe the instance of MigratableHello migrating :
During the execution, a default node is first created. It then hosts the
created
active object. Then the active object is migrated from node to node, each time
returning "hello" and telling the client program where it is located.