Source of QuoteServerImpl.java


  1: 
  2: import java.rmi.*;
  3: import java.rmi.server.UnicastRemoteObject;
  4: 
  5: public class QuoteServerImpl extends UnicastRemoteObject 
  6:      implements QuoteServer, Runnable {
  7: 
  8:   public QuoteServerImpl() throws java.rmi.RemoteException {} 
  9: 
 10:   static protected StockQuote quote[] = { 
 11:     new StockQuote("IBM", 100), 
 12:     new StockQuote("Sun", 100),  
 13:     new StockQuote("Intel", 100), 
 14:     new StockQuote("Apple", 100), 
 15:     new StockQuote("Compaq", 100) }; 
 16:   static protected int n = 5; 
 17: 
 18:   public StockQuote[] getQuote() throws java.rmi.RemoteException {
 19:     return quote; 
 20:   }
 21: 
 22:   public static void main(String[] args) {
 23:     System.out.println("Start RMI QuoteServer."); 
 24:     System.setSecurityManager(new RMISecurityManager());
 25: 
 26:     try {
 27:       QuoteServerImpl obj = new QuoteServerImpl();
 28:       Naming.rebind("QuoteServer", obj);
 29:       System.out.println("QuoteServerImpl created and bound in the registry to the name QuoteServer");
 30:       new Thread(obj).start();       
 31:     } catch (Exception e) {
 32:       System.out.println("QuoteServerImpl.main: an exception occurred:");
 33:       e.printStackTrace();
 34:     }
 35:   }
 36: 
 37:   public void run() {
 38:     while (true) {
 39:       try {
 40:         Thread.currentThread().sleep(10000); 
 41:         System.out.println("New quote:");
 42:         for (int i = 0; i < n; i++) {
 43:           quote[i].quote += (Math.random() - 0.4) * ( 10.0 * Math.random());   
 44:           System.out.println(quote[i].name + " " + quote[i].quote);
 45:         }
 46:         System.out.println("End quote.");
 47:       } catch (Exception e) {
 48:         System.err.println(e); 
 49:       }
 50:     }
 51:   }
 52: 
 53: }