I am writing a java program and I use rmi for net communication. I export a remote object and more clients can connect. This object contains a method that returns another remote object, different for every client. The returned objects contains two LinkedBlockingQueue. When multiple clients are connected, it seems that they take messages from the same queue. Here the first interface
public interface RMIInterface extends Remote {
public ConnectionInterface createConnection () throws RemoteException;
}
this is the first object
public class RMIImplementation extends UnicastRemoteObject implements RMIInterface {
@Override
public ConnectionInterface createConnection() throws RemoteException {
...
}
}
this is the second interface
public interface ConnectionInterface extends Remote {
public void sendMessage (Message info) throws RemoteException;
public Message getMessage() throws RemoteException;
}
this is the second object
public class RMIConnection implements ConnectionInterface{
private LinkedBlockingQueue<Message> in;
private LinkedBlockingQueue<Message> out;
ServerRMIConnection() {
in = new LinkedBlockingQueue<Message> ();
out = new LinkedBlockingQueue<Message> ();
try {
UnicastRemoteObject.exportObject(this, 0);
} catch (RemoteException e) {
}
}
@Override
public void sendMessage (Message info){
in.offer(info);
}
@Override
public Message getMessage() {
Message message=null;
try {
info = out.poll(120, TimeUnit.SECONDS);
} catch (InterruptedException e) {
}
return message;
}
}
When two or more clients call the getMessage method, they receive the same message. Any ideas?
As you're ignoring exceptions calling
exportObject()
, it is probably failing. Never do that. Print the exception.