I am tring to learn rmi and having some problems
Why when I call getStockPrice on the client side does it print the starting
value (1.34) and no the current value that prints out on the server side
from the System.out.println statements?
public class Stock
{
private String Name;
private double value;
Stock(String name, double val)
{
this.Name=name;
this.value=val;
}
public double getvalue()
{
return value;
}
public void setvalue(double val)
{
this.value=val;
}
public String getname()
{
return Name;
}
}
import java.util.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.net.MalformedURLException;
public class StockClient
{
public static void main(String args[])
{
String name="A";
try {
StockInterface a = (StockInterface)
Naming.lookup("rmi://localhost:41111/StockImpl");
System.out.println("price of "+ name + " is " +
a.getStockPrice(name));
}
catch (Exception re)
{
System.out.println(re);
}
}
}
import java.rmi.*;
import java.util.*;
import java.rmi.server.*;
import java.net.MalformedURLException;
public class StockImpl extends UnicastRemoteObject
implements StockInterface
{
private ArrayList<Stock> list;
public StockImpl() throws RemoteException
{
list=new ArrayList<Stock>();
list.add(new Stock("A",1.34));
list.add(new Stock("B",0.84));
list.add(new Stock("C",5.04));
list.add(new Stock("D",2.03));
list.add(new Stock("E",5.00));
list.add(new Stock("F",7.00));
list.add(new Stock("G",11.34));
list.add(new Stock("H",2.40));
list.add(new Stock("I",8.21));
list.add(new Stock("J",1.45));
}
public void sim()
{
Random rand =new Random();
Stock temp;
int abc=rand.nextInt(list.size());
temp=list.get(abc);
System.out.println(temp.getvalue() + " rand = "+abc);
temp.setvalue(temp.getvalue()+rand.nextDouble());
System.out.println(temp.getvalue());
}
public double getStockPrice(String Name) throws RemoteException
{
Stock temp;
//System.out.println("List size "+list.size());
//System.out.println("Name "+Name);
for(int i=0;i<list.size();i++)
{
temp = list.get(i);
if(Name.compareTo(temp.getname())==0)
{
return temp.getvalue();
}
}
return -1.0;
}
public ArrayList<Stock> getlist()
{
return list;
}
public static void main(String args[])
{
//System.setSecurityManager(new RMISecurityManager());
try {
StockImpl server = new StockImpl();
Naming.rebind("rmi://localhost:41111/StockImpl",server);
System.out.println("Created and registered StockImpl object");
StockImpl a= new StockImpl();
while(true)
{
a.sim();
try{ Thread.sleep(100); }
catch(Exception e) { }
System.out.println("Out "+a.getStockPrice("A"));
}
}
catch (RemoteException re) { }
catch (MalformedURLException me) { }
}
}
import java.rmi.*;
public interface StockInterface extends java.rmi.Remote
{
double getStockPrice(String Name) throws RemoteException;
}
Wretched Excess - 05 May 2006 17:29 GMT
I'm no RMI expert, but haven't you created two separate remote objects
('server' and 'a'), each with their own copy of the list and its contents?
The first is the one that your client accesses, the second is the one that
you periodically update. So the data seen by the client never gets updated.