我的应用程序应该自动迁移虚拟机,以防它的 vHost 发生故障,可能会迁移到同一 vCentre 中可用的另一个 vHost。
此外,我应该使用最新快照的冷迁移。我正在使用以下代码迁移 VM,但它会引发异常Remote Host can not be connected
。我怎样才能做到这一点???
这是我的代码:
try {
Folder rootFolder = Connect.getServiceInstance().getRootFolder();
String cloneName=vmname+"_clone";
// String AlternateHostName="130.65.133.21";
VirtualMachine vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname);
// ResourcePool rp = (ResourcePool) new InventoryNavigator(rootFolder).searchManagedEntity("ResourcePool", "Resources");
HostSystem targetHost = (HostSystem) new InventoryNavigator(rootFolder).searchManagedEntity("HostSystem",alternateHost);
// HostSystem targetHost = (HostSystem) new InventoryNavigator(rootFolder).searchManagedEntity("HostSystem",A);
// ResourcePool rp = new ResourcePool(Connect.getServiceInstance().getServerConnection(),);
ManagedEntity[] rps = new InventoryNavigator(rootFolder).searchManagedEntities(new String[][] {{"ResourcePool", "name" }, }, true);
//needs a datastore
Datastore[] dc = vm.getDatastores();
// System.out.println("DataStore"+dc[0].getName());
//configuration setup to relocate clone file on another vHost
VirtualMachineRelocateSpec relocateSpec = new VirtualMachineRelocateSpec();
relocateSpec.setDatastore(dc[0].getMOR());
relocateSpec.setHost(targetHost.getMOR());
relocateSpec.setPool(rps[1].getMOR());
//config setup to clone this VM using relocation specification above.
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
cloneSpec.setSnapshot(vm.getCurrentSnapShot().getMOR()); // should be working issue here
cloneSpec.setPowerOn(true);
cloneSpec.setLocation(relocateSpec);
//cloneSpec.setMemory(true);
Task task = vm.cloneVM_Task((Folder) vm.getParent(),cloneName, cloneSpec);
System.out.println("Launching the VM clone task. " +"Please wait ...");
String status = task.waitForTask(); // shall be waitfortask ??
if(status==Task.SUCCESS)
{
System.out.println("VM got cloned successfully.");
}
else
{
System.out.println("Failure -: VM cannot be cloned");
}`