⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 traderclient.java

📁 java的一系列产品中包括jsme,jmse,j2ee,本文件提供j2ee实现的源代码.
💻 JAVA
字号:
package stocktrader;

import java.rmi.RemoteException;
import java.util.Properties;
import java.io.*;

import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 * This class illustrates calling a stateful SessionBean and performing
 * the following exercises:
 * (1)Create a Trader
 * (2)Buy some shares using the Trader
 * (3)Sell some shares using the Trader
 * (4)Remove the Trader
 */
public class TraderClient
{
  private static final String JNDI_NAME = "StockTrader-TraderHome";
  private String url;
  private TraderHome home;
  
  public TraderClient(String url) throws NamingException
  {
    this.url   = url;    
    home = lookupHome();
  }
  
  /*
   Usage: java stocktrader.TraderClient t3://hostname:port");
   Default Usage: java stocktrader.TraderClient t3://localhost:7001");    
   */
  public static void main(String[] args)
  {       
    log("\nBeginning stock trade\n");    
    String url ="t3://localhost:7001";          
    // Parse the argument list 
    TraderClient client = null;
    try {
      client = new TraderClient(url);
    } catch (NamingException ne) {
      System.exit(1);
    }

    try {
      client.TraderAgent();
    }catch (Exception e){
      log("There was an exception while creating and using the Trader.");
      log("This indicates that there was a problem communicating with the server: "+e);
    } 

    log("\nEnd statefulSession.TraderClient...\n");
  }

  /**
   * Runs the TraderAgent.
   */
  public void TraderAgent() throws CreateException,RemoteException,
    ProcessingErrorException,RemoveException
  {
    String customerName = "LiMing"; 
    // Create a Trader
    Trader trader = (Trader)narrow(home.create(),Trader.class);

    //Sell some stock
    while(true){
    	byte flagbuf[]=new byte[8];
      //input Buy,Sell or Exit flag
      System.out.print("Buy,Sell or Exit(Buy,Sell,Exit):");
      try{
        System.in.read(flagbuf,0,8);
      }catch(Exception e){
    	  System.out.println("error");
      }
      String flagStr=new String(flagbuf,0);
      //System.out.println("flagStr="+flagStr.trim());
      if((flagStr.trim()).equalsIgnoreCase("Exit")){
        break;
    	}
      //input stockName and stockShare
      byte buffer[]=new byte[30];
    	String stockName=null,stockShare=null;
    	int numberOfShares=0,bytes;
    	TraderResult tr;
    	try{          
          System.out.print("Please StockName(JinQiao,EastAir):");        
          bytes=System.in.read(buffer,0,30);
          stockName = new String(buffer,0,0,bytes);
          //System.out.println("stock="+stockName.trim());
          
          System.out.print("numberOfShares(1~500):"); 
          bytes=System.in.read(buffer,0,30);
          stockShare = new String(buffer,0,0,bytes);
          //System.out.println("stockShare="+stockShare.trim());                 
    	    numberOfShares=Integer.parseInt(stockShare.trim());
    	    //System.out.println("Shares="+numberOfShares);
    	}catch(IOException e){
    		  System.out.println("Error readind from use");
    	}
    	  
    	if((flagStr.trim()).equalsIgnoreCase("Sell")){
    	  log("Selling " + numberOfShares + " of " + stockName);
        tr = trader.sell(customerName, stockName.trim(), numberOfShares);        
        log(tr.toString()+"\n");
    	}else if((flagStr.trim()).equalsIgnoreCase("Buy")){
    		log("Buying " + numberOfShares + " of " + stockName);
    	  tr = trader.buy(customerName, stockName.trim(), numberOfShares);  
    		log(tr.toString()+"\n");
    	}
    }
    // Get change in Cash Account from EJBean
    log("==========================");
    log("Change in Cash Account: $" + trader.getBalance()+ "\n");
    log("Removing trader");
    trader.remove();
  }

  /**
   * Lookup the EJBs home in the JNDI tree
   */
  private TraderHome lookupHome() throws NamingException
  {
    //Lookup the beans home using JNDI
    Context ctx = getInitialContext();   
    try {
      Object home = ctx.lookup(JNDI_NAME);
      return (TraderHome)narrow(home, TraderHome.class);
    } catch (NamingException ne){
      log("The client was unable to lookup the EJBHome.  Please make sure ");
      log("that you have deployed the ejb with the JNDI name "+JNDI_NAME+" on the WebLogic server at "+url);
      throw ne;
    }
  }

  /**
   * Using a Properties object to set initial properities
   */
  private Context getInitialContext() throws NamingException
  {    
    try {
      // Get an InitialContext
      Properties h = new Properties();
      h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
      h.put(Context.PROVIDER_URL, url);
      return new InitialContext(h);
    } catch (NamingException ne) {
      log("Unable to get a connection to the WebLogic server at "+url);
      log("Please make sure that the server is running.");
      throw ne;
    }
  }
  /**
   * RMI/IIOP clients should use this narrow function
   */
  private Object narrow(Object ref, Class c)
  {
    return PortableRemoteObject.narrow(ref, c);
  }
  
  private static void log(String s)
  {
    System.out.println(s);
  }
}

⌨️ 快捷键说明

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