Message Passing
Hi, everybody.
I'm new to RMI and I fear I may be thinking too much in an MPI paradigm but I'm looking for a way for a more "live" communication between my client and server applications.
I wrote a little toy application to prove a couple of concepts before I built something more important and it's a good thing I did because I've met with incomplete success.
The toy-app is some producer/consumer code where the producer posts Strings to a (self-defined) Buffer object at random time-intervals (0-10 seconds) and the consumer gets them out each String as soon as it is available.
The Producer has a method called start that takes a Buffer object. When I put the producer in the rmiregistry, it takes the Buffer from the client (the consumer) and pushes strings into it but they never become available in the client.
When I pass an object reference over RMI is it still the same object, or is it just a snapshot of the one I passed?
Producer.java:
import java.io.Serializable;
import java.net.InetAddress;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Random;
publicclass Producerextends UnicastRemoteObjectimplements iProducer{
class runnerextends Thread{
Buffer buffer;
int num;
public runner(Buffer buffer,int num){
this.buffer = buffer;
this.num = num;
}
publicvoid run(){
Random generator =new Random();
for(int i = 0; i < num; i++){
buffer.push("This is string " + i);
System.out.println("pushing \"This is string " + i +"\"");
int r = Math.abs(generator.nextInt() % 10);//between 0 and 10
try{
Thread.sleep(r * 1000);
}catch(InterruptedException e){
System.err.println(e);
}
}
System.out.println("closing buffer");
buffer.close();
}
};
Thread r;
public Producer()throws RemoteException{
super();
}
publicvoid start(Buffer buffer,int num)throws RemoteException{
System.out.println("recvd start");
r =new runner(buffer, num);
r.start();
}
publicstaticvoid main(String[] args)throws Exception{
String hostname = InetAddress.getLocalHost().getHostName();
String servicename ="//" + hostname +"/Producer";
Producer producer =new Producer();
Naming.rebind(servicename, producer);
System.out.println("Producer bound");
}
}
iProducer.java:
import java.rmi.Remote;
import java.rmi.RemoteException;
publicinterface iProducerextends Remote{
publicvoid start(Buffer buffer,int num)throws RemoteException;
}
Consumer.java:
import java.rmi.Naming;
publicclass Consumer{
publicstaticvoid main(String[] args)throws Exception{
String host =null;
if(args.length != 1)
host ="localhost";
else
host = args[0];
String serviceName ="//" + host +"/Producer";
iProducer producer = (iProducer) Naming.lookup(serviceName);
//iProducer producer = new Producer();
Buffer buffer =new Buffer();
if(producer ==null)
thrownew Exception("Null remote object");
producer.start(buffer, 1);
String ln;
while((ln = (String)buffer.pop()) !=null){
System.out.println(ln);
}
}
}

