MarketMakerClient.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- package bible.rmi.example2;
- import weblogic.rmi.*;
- import java.util.*;
- import javax.naming.*;
- /**
- * MarketMakerClient periodically sets prices in the PriceServer.
- */
- public class MarketMakerClient extends TimerTask implements Runnable {
- /** rand */
- private Random rand = new Random();
- /** securities */
- private String[] securities = null;
- /**
- * Gets a reference to the PriceServer, get a list of securites, and display prices.
- */
- public void run() {
- System.out.println("MarketMakerClient setting prices.");
- System.out.println(" Time: " + new Date(System.currentTimeMillis()));
- try {
- InitialContext ctx = Environment.getInitialContext();
- PriceServer server =
- (PriceServer) ctx.lookup(PriceServer.PRICESERVERNAME);
- securities = server.getSecurities();
- for (int i = 0; i < securities.length; i++) {
- boolean increase = rand.nextBoolean();
- Float amount = new Float(0.25 * rand.nextInt(2));
- if (increase) {
- server.setPrice(securities [i],
- server.getPrice(securities [i])
- + amount.floatValue());
- } else {
- server.setPrice(securities [i],
- server.getPrice(securities [i])
- - amount.floatValue());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Instanciates a new MarketMakerClient and schedule its functionality on a timer.
- * @param args
- */
- public static void main(String[] args) {
- Timer t = new Timer();
- MarketMakerClient client = new MarketMakerClient();
- // Starting in 5 secs, re-do pricing every 5 seconds.
- t.schedule(client, 5000, 5000);
- }
- }