Search code examples
javaproxyrmimiddleware

RMI Registry and Illegal Argument Exception


My project, that uses RMI, is so divided:

two interfaces, ISportello and IGestoreSportelli.

public interface IGestoreSportelli extends Remote {


public boolean sottoponiRichiesta (int id) throws RemoteException;
public void sottoscrivi (ISportello sportello) throws RemoteException;

}


public interface ISportello {
public boolean serviRichiesta(int id) throws RemoteException;
 }

Two classes that implements the logic of the application, and implements the interfaces: GestoreSportelliImplementazione and SportelloImplementazione

   public class GestoreSportelliImplementazione implements IGestoreSportelli{

public Vector<ISportello> sportelli = new Vector<ISportello>();
@Override
public boolean sottoponiRichiesta(int id) throws RemoteException {
    System.out.println("Processo la richiesta: id:" + id);
    int size = sportelli.size();
    int i=0;
    boolean esito = false;

    do {
        esito = sportelli.get(i++).serviRichiesta(id);
    }while(i<size && !esito);

    return esito;
}

@Override
public void sottoscrivi(ISportello sportello) throws RemoteException {
    System.out.println("[GESTORE] Aggiungo un nuovo sportello");
    System.out.println("SPORTELLO: " + sportello);
    sportelli.add(sportello);

}

  }


 @SuppressWarnings("serial")
 public class SportelloImplementazione extends UnicastRemoteObject implements ISportello{

protected SportelloImplementazione() throws RemoteException {
    super();
}

@Override
public boolean serviRichiesta(int id) throws RemoteException {
    // TODO Auto-generated method stub
    return true;
}

 }

The second class is a stub (I will develop if further when I'll know why the program doesn't work as it is).

Two classes with main functions that "wrap" the previous two classes

public class GestoreSportelliServer {

public static void main(String[] args) {
    try {
    IGestoreSportelli gestore = new GestoreSportelliImplementazione();
    Registry rmi = LocateRegistry.getRegistry();
    IGestoreSportelli gestoreRef = (IGestoreSportelli) UnicastRemoteObject.exportObject(gestore,0);
    System.out.println("[GESTORE] AVVIATO");
    System.out.println("[GESTORE] " + gestore);
    rmi.rebind("gestore", gestoreRef);
    System.out.println("[GESTORE] Registrato");
    }
    catch (RemoteException r)
    {
        r.printStackTrace();
    }
}
}



public class SportelloServer {

public static void main(String[] args) {
    try {
    Registry rmi = LocateRegistry.getRegistry();
    ISportello sportello = new SportelloImplementazione();
    System.out.println("[SPORTELLO] : " + sportello);
    IGestoreSportelli gestore  =(IGestoreSportelli) rmi.lookup("gestore");
    gestore.sottoscrivi(sportello);
    System.out.println("[SPORTELLO] Sottoscrizione avvenuta");
    }
    catch (RemoteException r)
    {
        r.printStackTrace();

    }

    catch (NotBoundException n)
    {
        n.printStackTrace();
    }

    catch (IllegalArgumentException i)
    {
        i.printStackTrace();

system.out.println(i.getmessage()); }

}

}

I run the classes with main in two differend prompt windows, and a third prompt windows runs the RMIRegistry. The SportelloServer class doesn't work. It runs into an exception:

 java.lang.IllegalArgumentException: java.lang.ClassCastException@547d534d
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
at sun.rmi.server.UnicastRef.invoke(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(Unknown Source)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(Unknown Source)
at com.sun.proxy.$Proxy1.sottoscrivi(Unknown Source)
at server.SportelloServer.main(SportelloServer.java:19)
java.lang.ClassCastException@547d534d

The #19 line is:

gestore.sottoscrivi(sportello);

Solution

  • Both interfaces have to extend Remote:

    public interface ISportello extends Remote{
    public boolean serviRichiesta(int id) throws RemoteException;
    }
    

    Is the correct ISportello code