RMI newbie

B

Bob

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;
}
 
W

Wretched Excess

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

ClassCastException in RMI application 4
java rmi error 1
Help in hangman game 1
JAVA RMI PROBLEM 0
RMI Objects Help 7
How to remotely construct a remote object with RMI 14
Java method query 2
Serializable 0

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top