quoteclient.java

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

JAVA
59
字号
package bible.rmi.example3;

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

/**
 * QuoteClient periodically asks the PriceServer for prices of all securities.
 */
public class QuoteClient extends TimerTask implements Runnable {

  /**
   * Gets a reference to the PriceServer, gets a list of securites, and displays prices.
   */
  public void run() {

    System.out.println("QuoteClient, Getting quotes.");
    System.out.println("Quote time: " + new Date(System.currentTimeMillis()));

    try {

      // Get the remote stub for the Price server via JNDI.
      InitialContext ctx    = Environment.getInitialContext();
      PriceServer    server =
        (PriceServer) ctx.lookup(PriceServer.PRICESERVERNAME);

      // Remote call
      String securities [] = server.getSecurities();
      if (securities != null) {
        for (int i = 0; i < securities.length; i++) {

          // Remote call
          System.out.println("Security: " + securities [i] + " Price: "
                             + server.getPrice(securities [i]));
        }
      } else {
        System.out
          .println("No securities registered with the server at this time.");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Creates a QuoteClient and schedules it for periodic running via a Timer.
   * @param args
   */
  public static void main(String[] args) {

    Timer       t      = new Timer();
    QuoteClient client = new QuoteClient();
    System.out.println("QuoteClient started.");

    // Starting in 10 secs, check every 20 secs.
    t.schedule(client, 10000, 20000);
  }
}

⌨️ 快捷键说明

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