It looks like sometimes VirtualMachine.getConfig() returns null, but other times it throws:
Caused by: java.lang.RuntimeException: com.vmware.vim25.ManagedObjectNotFound
Bothered by SLOW Web UI to manage vSphere? Want to manage ALL your VMware vCenters, AWS, Azure, Openstack, container behind a SINGLE pane of glass? Want to search, analyze, report, visualize VMs, hosts, networks, datastores, events as easily as Google the Web? Find out more about vSearch 3.0: the search engine for all your private and public clouds.
As the vSphere API reference points out, the config property could be unset.
The virtual machine configuration is not guaranteed to be available. For example, the configuration information would be unavailable if the server is unable to access the virtual machine files on disk, and is often also unavailable during the initial phases of virtual machine creation.
So it’s expected for you to get null in Java while calling getConfig on a VirtualMachine object due to various reasons.
Still, it’s puzzling to get the ManagedObjectNotFound exception – it means the managed object is not there. Because the PropertyCollector managed object is used to retrieve the config property, there are actually two managed objects involved: the PropertyCollector, and the VirtualMachine of your interest. Which is not found? Because the PropertyCollector is a built-in singleton managed object within a target server either vCenter or ESX, it should be there always. So it’s the VirtualMachine object that is likely not found.
The question then becomes, “why the PropertyCollector cannot find the virtual machine whose ManagedObjectReference exists already?” As questioned in the bug, “this can happen when ESX and vCenter are temporarily out of sync or VMs are found in the ‘Discovered virtual machine’ folder.” I think there is a possibility for the first case. For the second case, the “Discovered virtual machine” folder is just like any other folders even though the system has it as default for VMs added without specifying folder like the following cmd in this article, therefore it shouldn’t make a VM not found.
vmware-cmd -s register “/home/vmware/W2K3 STD SP1/W2K3 STD SP1.vmtx”
Having said that, there is a valid case for a VirtualMachine not found: the VirtualMachine is unregistered from inventory even though it’s not deleted in datastore. On the client side, you can still have a valid VirtualMachine object but it’s no longer referring to a valid virtual machine on the server side. If that happens, throwing exception is consistent not intermittent.
Overall it’s not typical for VirtualMachine object and other ManagedEntity objects because the inventory is relatively stable. However, it can be very common for the Task objects, which remain live only for several minutes. After that, calling any method on them will fail.
While I don’t have a good explanation on the intermittent exception, hearing what goes wrong definitely help improve the Java API. It also helps to find bugs on the core vSphere API.