package examples.ejb.basic.statelessSession; import javax.ejb.*; import javax.naming.*; import java.rmi.RemoteException; import java.util.*; /** * This class illustrates using a stateless SessionBean: * * * @author Copyright (c) 1998 by WebLogic, Inc. All Rights Reserved. * @author Copyright (c) 1998-1999 by BEA WebXpress. All Rights Reserved. */ public class Client { static String url = "t3://localhost:7001"; static String user = null; static String password = null; /** * Runs this example from the command line. Example: *

* java examples.ejb.basic.statelessSession.Client "t3://localhost:7001" scott tiger *

* The parameters are optional, but if any are supplied, * they are interpreted in this order: *

* @param url URL such as "t3://localhost:7001" of Server * @param user User name, default null * @param password User password, default null */ public static void main(String[] args) { System.out.println("\nBeginning statelessSession.Client...\n"); // Parse the argument list if ((args == null) || (args.length == 0)) {} else for (int i = 0; i < args.length; i++) { switch(i) { case 0: url = args[i]; break; case 1: user = args[i]; break; case 2: password = args[i]; break; default: } } String customerName = "Erin"; try { // Create a trader object, who'll later help us execute trades Context ctx = getInitialContext(); TraderHome brokerage = (TraderHome) ctx.lookup("statelessSession.TraderHome"); // Unlike the statefulSession example, // we don't give this trader a name System.out.println("Creating trader\n"); Trader trader = brokerage.create(); String stockName; int numberOfShares; // Unlike the statefulSession example, // we have to keep track of the balance over the // life of our use of the session bean double cashBalance = 0.0; for (int i = 1 ; i <= 5; i++) { System.out.println("Start of Transaction " + i + " for " + customerName); // Buying stockName = "WEBL"; numberOfShares = 100 * i; System.out.println("Buying " + numberOfShares + " of " + stockName); TradeResult tr = trader.buy(customerName, stockName, numberOfShares); System.out.println("...Bought " + tr.numberTraded + " at $" + tr.priceSoldAt); // Keep track of the change in the Cash Account cashBalance = cashBalance - (tr.numberTraded * tr.priceSoldAt); // Selling stockName = "INTL"; numberOfShares = 100 * (i+1); System.out.println("Selling " + numberOfShares + " of " + stockName); tr = trader.sell(customerName, stockName, numberOfShares); System.out.println("...Sold " + tr.numberTraded + " at $" + tr.priceSoldAt); // Keep track of the change in the Cash Account cashBalance = cashBalance + (tr.numberTraded * tr.priceSoldAt); // Print change in Cash Account System.out.println("Change in Cash Account: $" + cashBalance); System.out.println("End of Transaction " + i + "\n"); } System.out.println("Change in Cash Account: $" + cashBalance + "\n"); System.out.println("Removing trader"); trader.remove(); } catch (ProcessingErrorException pe) { System.out.println("Processing Error: " + pe); } catch (Exception e) { System.out.println(":::::::::::::: Error :::::::::::::::::"); e.printStackTrace(); } System.out.println("\nEnd statelessSession.Client...\n"); } /** * Gets an initial context. * * @return Context * @exception java.lang.Exception if there is * an error in getting a Context */ static public Context getInitialContext() throws Exception { Hashtable h = new Hashtable(); h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); h.put(Context.PROVIDER_URL, url); if (user != null) { System.out.println ("user: " + user); h.put(Context.SECURITY_PRINCIPAL, user); if (password == null) password = ""; h.put(Context.SECURITY_CREDENTIALS, password); } return new InitialContext(h); } }