i have a simple RMI - Client, Server architecture. As I want to do callbacks from the server I defined following Interfaces:
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;
}
And here the Impls (Server):
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;
}
}
Client:
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();
}
}
}
when trying to register the client, following exception occurs on the client side:
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)
Any suggestions?
You can't use exported remote object types as remote method parameter or return types. Change it to: