QuoteClient.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package bible.rmi.example2;
- 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()));
- InitialContext ctx = null;
- try {
- // Get the remote stub for the Price server via JNDI.
- 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();
- } finally {
- try {
- ctx.close();
- } 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);
- }
- }