2014-12-08 165 views
0

我有一個簡單的RMI - 客戶端,服務器體系結構。當我想從服務器做回調,我定義以下接口:註冊RMI客戶端導致IllegalArgumentException

public interface Meiern_RMI_Server extends Remote{ 
     public boolean registerClient(RMIClient client) throws RemoteException; 
    } 
    public interface Meiern_RMI_Client extends Remote{ 
     public void setTurn(boolean yourTurn) throws RemoteException; 
    } 

而這裏Impls(服務器):

public class RMIServer extends UnicastRemoteObject implements Meiern_RMI_Server{ 

    private Set<RMIClient> clients; 

    public RMIServer() throws RemoteException { 
     super(); 
     this.clients = new HashSet<RMIClient>(); 
    } 

    public static void main(String [] args){ 
     try { 
      Registry reg = LocateRegistry.createRegistry(1234); 
      reg.rebind("server", new RMIServer()); 
      System.out.println("server started"); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public synchronized boolean registerClient(RMIClient client) throws RemoteException { 
     boolean registered = false; 
     try { 
      registered = clients.add(client); 
      if(registered) { 
       System.out.println("Client " + client.getName() + " registered."); 
       System.out.println(clients); 
      } else { 
       System.out.println("Could not register client. Duplicate names " + client.getName());    
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return registered; 
    } 

} 

客戶:

public class RMIClient extends UnicastRemoteObject implements Meiern_RMI_Client {  public RMIClient() throws RemoteException { 
     super(); 
    } 

    @Override 
    public void setTurn(boolean yourTurn) throws RemoteException { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 
    public static void main(String [] args) { 
     try { 
      RMIClient client = new RMIClient(); 

      Registry reg = LocateRegistry.getRegistry("127.0.0.1", 1234); 
      Meiern_RMI_Server rmi = (Meiern_RMI_Server)reg.lookup("server"); 
      System.out.println("client connected"); 
      boolean registered = rmi.registerClient(client); 
      if(registered){ 
       System.out.println("Client registered"); 
      } else { 
       System.out.println("Could not register client, Change name"); 
      } 

      while (true) { 

      } 
     } catch (Exception e) { 
      System.out.println("Could not connect to server: "); 
      e.printStackTrace(); 
     } 
    } 


} 

嘗試註冊時客戶端,在客戶端發生以下異常:

java.lang.IllegalArgumentException: argument type mismatch 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322) 
    at sun.rmi.transport.Transport$1.run(Transport.java:177) 
    at sun.rmi.transport.Transport$1.run(Transport.java:174) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at sun.rmi.transport.Transport.serviceCall(Transport.java:173) 
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811) 
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:745) 
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:275) 
    at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:252) 
    at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:161) 
    at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:194) 
    at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:148) 
    at com.sun.proxy.$Proxy1.registerClient(Unknown Source) 
    at meiern.RMIClient.main(RMIClient.java:97) 

有什麼建議嗎?

回答

1
public boolean registerClient(RMIClient client) throws RemoteExceptio 

不能將導出的遠程對象類型用作遠程方法參數或返回類型。將其更改爲:

public boolean registerClient(Myeiern_RMI_Client client) throws RemoteException