Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

RMI newbie

Thread view: 
Bob - 05 May 2006 08:31 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.