QuoteClient.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package bible.rmi.example4;
- import weblogic.rmi.*;
- import java.util.*;
- import javax.naming.*;
- /**
- * Periodically ask the PriceServer for prices of all securities.
- */
- public class QuoteClient extends TimerTask implements Runnable {
- /**
- * Get a reference to the PriceServer, get a list of securites, and display 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();
- }
- }
- /**
- * Create a QuoteClient and schedule it for periodic running.
- * @param args
- */
- public static void main(String[] args) {
- Timer t = new Timer();
- QuoteClient client = new QuoteClient();
- System.out.println("QuoteClient started.");
- try {
- InitialContext ctx = Environment.getInitialContext();
- ExecutionAlert tc = (ExecutionAlert) ctx.lookup("TradingClient");
- Execution exec = new Execution();
- exec.setNumber(-1);
- exec.setPrice(-1);
- exec.setShares(-1);
- exec.setSymbol("Hi there!");
- tc.notifyOfExecution(exec);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // Starting in 10 secs, check every 20 secs.
- t.schedule(client, 10000, 20000);
- }
- }