JAVA RMI dispatchUncaughtException - 接口转换问题

时间:2010-02-06 13:56:11

标签: java casting rmi

对于学校我们使用RMI创建了一个Java应用程序,有3个应用程序:Server,Client和Rekenaar。

出错的行是Line:“test =(Range [])m.getAllRange();”,它给出了dispatchUncaughtException。

package rekenaar;

import server.Opdracht;
import java.rmi.Naming;
import java.util.logging.Level;
import java.util.logging.Logger;
import interfaces.*;
import java.rmi.RemoteException;

/**
 *
 * @author Windows
 */
public class Rekenaar implements Runnable{

    private Range range;
    private IRekenCoördinator coordinator;
    private long[] priemgetallen;
    private Opdracht op;
    private Range[] test;

    public Rekenaar()
    {
        try{
        this.coordinator = null;
        this.coordinator = (IRekenCoördinator) Naming.lookup("rmi://127.0.0.1:1099/RekenC");
        this.priemgetallen = coordinator.getPriemgetallen();
        this.op = null;
        }catch(Exception ex){ex.printStackTrace();}

    }


    public void Bereken() 
    {
        try{
        long rangeOndergrens = (op.getGetal()/2000) * 2000;
        range = new Range(priemgetallen, rangeOndergrens);
        op.setRange(range);
        op.setUitkomst(range.geefSemiPriem(op.getGetal()));
        coordinator.RemoveID(op.CheckRange());

        IMagazijn m = op.getMagazijn();
        test = (Range[])m.getAllRange();
        String hoi = m.hoi();


        m.AddRange(op);
        }catch(RemoteException ex){ex.printStackTrace();}
    }


    @Override
    public void run() {
                KrijgtOpdracht();
    }

    private void KrijgtOpdracht() 
    {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Rekenaar.class.getName()).log(Level.SEVERE, null, ex);
        }
        if(coordinator != null)
        {
            try{
        op = (Opdracht)coordinator.getOpdracht();
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        if(op != null ) this.Bereken();

        this.run();

        }
    }

}

接口:

package interfaces;

import java.io.IOException;
import java.rmi.Remote;
import java.rmi.RemoteException;

/**
 *
 * @author Windows
 */
public interface IMagazijn extends Remote {

    /**
     *
     * @param op        Uitgerekende opdracht waarvan de gegevens verwerkt moeten worden op de server
     * @throws IOException
     */
    public String hoi() throws RemoteException;
    public IRange[] getAllRange() throws RemoteException;
    public void AddRange(IOpdracht op) throws RemoteException;

}

以下是Magazijn本身给出的IRange超过RMI:

package server;


import java.rmi.RemoteException;
import java.util.ArrayList;

import interfaces.*;
import java.rmi.server.UnicastRemoteObject;



public class Magazijn extends UnicastRemoteObject implements IMagazijn{

private IRange[] ranges;
private long ondergrens;

public void setOndergrens(long ondergrens) {
    this.ondergrens = ondergrens;
}
private long bovengrens;

public void setBovengrens(long bovengrens) {
    this.bovengrens = bovengrens;
}
private transient MagazijnManager MM;
private transient ArrayList<Opdracht> dubbeleOpdrachten;
private transient RekenCoördinator RC;
private transient int MAGAZIJN_GROOTTE;

public Magazijn(long ondergrens, long bovengrens, MagazijnManager mm, RekenCoördinator RC)throws RemoteException
{
    ranges = new IRange[(int)(bovengrens-ondergrens)/2000];
    this.bovengrens = bovengrens;
    this.ondergrens = ondergrens;
    dubbeleOpdrachten = new ArrayList<Opdracht>();
    this.MM = mm;
    this.RC = RC;
    MAGAZIJN_GROOTTE = (int)(bovengrens - ondergrens) + 1;
}

public void AddRange(IOpdracht op) throws RemoteException
{
    ArrayList<IOpdracht> returnList = new ArrayList<IOpdracht>();
    ranges[op.getRangeIndex()] = op.getRange();
    returnList.add(op);
    for(Opdracht o : dubbeleOpdrachten)
    {
        if(o.getRangeIndex() == op.getRangeIndex())
        {
            o.setRange(op.getRange());
            o.setUitkomst(o.getRange().geefSemiPriem(o.getGetal()));
            returnList.add(o);
        }
    }
    MM.StuurOpdrachtenTerug(returnList);
}

public IRange[] getAllRange() throws RemoteException
{
    return ranges;
}

 public String hoi() throws RemoteException
{
    return "poep";
}

public IRange getRange(int rangeIndex)
{
    return ranges[rangeIndex];
}

public void StuurOpdracht(Opdracht op)
{
    Opdracht o =null;
        o = RC.AddOpdracht(op);
    if(o!=null)
    {
        dubbeleOpdrachten.add(o);
    }
}

public long getOndergrens()
{
    return ondergrens;
}

 public long getBovengrens()
{
    return bovengrens;
}

 public int getMagazijnGrootte()
 {
     return MAGAZIJN_GROOTTE;
 }

}

Rekenaar得到了全班“Range”,所以这不是问题。问题在于将IRange投射到Range之间。范围本身是Serializable并实现IRange。

我得到的错误:线程“main”中的异常java.lang.ClassCastException:[Linterfaces.IRange;不能投[Lrekenaar.Range;

1 个答案:

答案 0 :(得分:2)

使用RMI时,您将无法获得与服务器端相同的实现。相反,你得到一个代理类,它将调用你的服务器。所以你不能施展你的方法。 将测试更改为

IRange[] test

应该这样做。