Invalid managed object reference eror

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
Ghetas
Posts: 19
Joined: 19. Dec 2015, 17:35

Invalid managed object reference eror

Post by Ghetas »

Hi everyone
I am using VirtualBox API to get the CPU usage for running Virtual machine the code gives my CPU usage but some times it throws the following exception

Code: Select all

Exception in thread "Thread-2" org.virtualbox_5_0.VBoxException: VirtualBox error: Invalid managed object reference "0cfd805b35aebcdb-0000000000000003"
	at org.virtualbox_5_0.IPerformanceCollector.queryMetricsData(IPerformanceCollector.java:337)
	at vmutilization.VmIPerformanceCollector.CollectCpuUtilization(VmIPerformanceCollector.java:75)
	at vmutilization.VmPerformanceCollectorsThread.run(VmPerformanceCollectorsThread.java:57)
Caused by: org.virtualbox_5_0.jaxws.InvalidObjectFaultMsg: VirtualBox error: Invalid managed object reference "0cfd805b35aebcdb-0000000000000003"
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
	at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(SOAPFaultBuilder.java:136)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:108)
	at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:78)
	at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(SEIStub.java:135)
	at com.sun.proxy.$Proxy24.iPerformanceCollectorQueryMetricsData(Unknown Source)
	at org.virtualbox_5_0.IPerformanceCollector.queryMetricsData(IPerformanceCollector.java:325)
	... 2 more
my code is

Code: Select all

package vmutilization;

import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.List;

import org.virtualbox_5_0.Holder;
import org.virtualbox_5_0.IMachine;
import org.virtualbox_5_0.IPerformanceCollector;
import org.virtualbox_5_0.IUnknown;
import org.virtualbox_5_0.IVirtualBox;
import org.virtualbox_5_0.VBoxException;
import org.virtualbox_5_0.VirtualBoxManager;

import configuration.Vm;
import loadbalancer.LoadBalancerInterface;
import print.Error;
import print.Out;


public class VmIPerformanceCollector {
	Vm vm;
	IVirtualBox vbox;
	IMachine machine;
	IPerformanceCollector oCollector;
	VirtualBoxManager mgr;
	String VboxsrvUrl;

	long scale;
	double Cpuutilization;
	
	public VmIPerformanceCollector(Vm vm){
		this.vm=vm;	
	}
	@SuppressWarnings("finally")
	public boolean ConnecttoVirtualbox(){
		boolean connectsucess=false;
		mgr = VirtualBoxManager.createInstance(null);
		try{
			mgr.connect(vm.getVboxsrvUrl(), null, null);
			vbox=mgr.getVBox();
			machine=vbox.findMachine(vm.getVmName());
			oCollector = vbox.getPerformanceCollector();	
		}catch (VBoxException e){
			Error.println(e.getMessage());
			Out.println("Cannot connect, start webserver first!");
			 System.err.println(e.getMessage());
			
		}finally{
		return connectsucess;
		}
	}
	

	
	public void setup(long Period, long Count){
		 
        oCollector.setupMetrics(Arrays.asList(new String[]{"*"}),
       		 Arrays.asList(new IUnknown[]{machine}),
                Period, Count);
	}

	public void CollectCpuUtilization(double time, LoadBalancerInterface loadbalancer) throws InterruptedException{
		

        Holder<List<String>> names = new Holder<List<String>> ();
        Holder<List<IUnknown>> objects = new Holder<List<IUnknown>>() ;
        Holder<List<String>> units = new Holder<List<String>>();
        Holder<List<Long>> scales =  new Holder<List<Long>>();
        Holder<List<Long>> sequenceNumbers =  new Holder<List<Long>>();
        Holder<List<Long>> indices =  new Holder<List<Long>>();
        Holder<List<Long>> lengths =  new Holder<List<Long>>();
 
        
        List<Integer> values = oCollector.queryMetricsData(Arrays.asList(new String[]{"Guest/CPU/Load/Kernel:avg,Guest/CPU/Load/User:avg"}),
                Arrays.asList(new IUnknown[]{machine}),
                names, objects, units, scales,
                sequenceNumbers, indices, lengths);
        Cpuutilization=0;

        for (int i=0; i < names.value.size(); i++){
        		scale=scales.value.get(i);
        		
                for (int j = 0; j < lengths.value.get(i); j++)
                {
                    long val= values.get(indices.value.get(i).intValue() + j).longValue();
                    Long val2=val;
                    double val3=val2.doubleValue()/scale;
                    Cpuutilization=Cpuutilization + val3; 
                }
        }
        Cpuutilization=Math.round(Cpuutilization*100)/100.00d;
        System.out.println("Time:"+ time+" CPU Usage: "+ Cpuutilization+ " vm: "+vm.getVmName());
        vm.addStateHistoryEntry(time,Cpuutilization , 1);
        try {
			loadbalancer.UpdateVmsUsage(vm.getVmName(), Cpuutilization);
		} catch (RemoteException e) {

			e.printStackTrace();
			 System.err.println(e.getMessage());
		}
        
	}
	
	public void disconnect(){
        try {
        	oCollector.releaseRemote();
            mgr.disconnect();
        } catch (VBoxException e) {
        	e.printStackTrace();
			Error.println(e.getMessage());
			Out.println("Cannot disconnect, start webserver first!");
			 System.err.println(e.getMessage());
        }
        mgr.cleanup();
	}
	
}

I setup the authentication to be null
My Virtual Box Version is 5.14
OS: Centos 6.7
Language Java

any help will be appreciated thanks all
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Invalid managed object reference eror

Post by noteirak »

The exception is telling you that the object you're using has lost its server counterpart - in case of performance metrics, it usually happens when the VM is powered off per example, or restarted.
The objects you are manipulating in Java are simply proxy objects which are linked to the real object on the server side, so any "managed object reference error" means that specific object is gone.

At that point, you need to ask or recreate the object throwing the exception via the API.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Ghetas
Posts: 19
Joined: 19. Dec 2015, 17:35

Re: Invalid managed object reference eror

Post by Ghetas »

noteirak wrote:The exception is telling you that the object you're using has last its server counterpart - in case of performance metrics, it usually happens when the VM is powered off per example, or restarted.
The objects you are manipulating in Java are simply proxy objects which are linked to the real object on the server side, so any "managed object reference error" means that specific object is gone.

At that point, you need to ask or recreate the object throwing the exception via the API.
Thanks noteirak, this solves my problem
Post Reply