priceserverimpl.java

来自「100多M的J2EE培训内容」· Java 代码 · 共 97 行

JAVA
97
字号
package bible.rmi.example2;

import java.util.*;
import javax.naming.*;
import weblogic.rmi.*;
import java.rmi.RemoteException;

/**
 * PriceServerImpl implements the PriceServer remote interface, providing
 * prices on securities for remote clients.
 */
public class PriceServerImpl implements PriceServer {

  // Stock price database, implemented as a Hashtable.

  /** stockTable           */
  private Hashtable stockTable = null;

  /**
   * Creates the stock price 'database'.
   */
  public PriceServerImpl() {

    stockTable = new Hashtable();

    Float f = new Float(0);
    stockTable.put("SUNW", f.valueOf("20"));
    stockTable.put("INTC", f.valueOf("30"));
    stockTable.put("BEAS", f.valueOf("40"));
  }

  /**
   * Returns the price of the given security.
   * @param symbol
   */
  public float getPrice(String symbol) throws java.rmi.RemoteException{

    Float f = (Float) stockTable.get(symbol);
    System.out.println("PriceServer: Call to getPrice for symbol : "
                       + symbol);

    return f.floatValue();
  }

  /**
   * Sets the price for the given security.
   * @param symbol
   * @param price
   */
  public void setPrice(String symbol, float price) throws java.rmi.RemoteException{

    Float f = new Float(price);
    stockTable.put(symbol, f);
    System.out.println("PriceServer: Call to setPrice for " + symbol + " to "
                       + price);
  }

  /**
   * Returns all ticker symbols in this server's database.
   */
  public String[] getSecurities() throws java.rmi.RemoteException{

    if (stockTable.size() > 0) {
      String[]    securities = new String [stockTable.size()];
      Enumeration enum       = stockTable.keys();
      int         i          = 0;
      while (enum.hasMoreElements()) {
        securities [i] = (String) enum.nextElement();

        i++;
      }

      return securities;
    }

    return null;
  }

  /**
   * Instanciates and binds a PriceServer.
   * @param args
   */
  public static void main(String[] args) {

    try {
      PriceServerImpl server = new PriceServerImpl();
      Context         ctx    = new InitialContext();
      ctx.bind(PRICESERVERNAME, server);
      System.out.println("PriceServer was started and bound in the registry "
                         + "to the name " + PRICESERVERNAME);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?