0
我的項目,使用RMI,是如此劃分:RMI註冊表和非法參數異常
兩個接口,ISportello和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;
}
兩個類實現應用程序的邏輯和實現的接口:GestoreSportelliImplementazione和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;
}
}
第二類是存根(我將開發,如果進一步的時候我就知道爲什麼該程序無法正常工作)。
兩類與主要功能即 「包裝」 上述兩個類
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()); }
}
}
我運行主類兩種differend提示窗口,第三個提示窗口運行RMIREGISTRY。 SportelloServer類不起作用。它運行到一個例外:
java.lang.IllegalArgumentException: [email protected]
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)
[email protected]
的第19行是:
gestore.sottoscrivi(sportello);
有沒有更多的堆棧跟蹤? – EJP
不,沒有。我不知道我做錯了什麼,這是一個非常簡單的代碼。 – EagleOne
誰印的是底線?只讀'java.lang.ClassCastException @ ...'的那個?所有這些類和接口是否在同一個包中? – EJP