package examples.ejb.basic.statefulSession; import javax.ejb.*; import javax.naming.*; import java.rmi.RemoteException; import java.util.Properties; /** * This class demonstrates calling an statefulsession EJBean * using multiple colocated clients: * * * @author Copyright (c) 1998-1999 by BEA Systems, Inc. All Rights Reserved. */ public class MultiClient { static String url = "t3://localhost:7001"; static String user = null; static String password = null; static int numTraders = 10; static final int numTransactionsPerTrader = 10; /** * Runs this example from the command line. Example: *

* java examples.ejb.basic.statefulSession.MultiClient "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 statefulSession.MultiClient...\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 { MultiClientBigTrader[] traders = new MultiClientBigTrader[numTraders]; Context ctx = getInitialContext(); TraderHome brokerage = (TraderHome) ctx.lookup("statefulSession.TraderHome"); for (int i = 0; i < numTraders; i++) { // Create a trader object, who'll later help us execute trades // Give this trader a name Trader trader = brokerage.create("Terry" + i); System.out.println("Creating trader " + trader.getTraderName() + "\n"); traders[i] = new MultiClientBigTrader(i, trader, customerName); traders[i].start(); } for (int i = 0; i < numTraders; i++) { try { traders[i].join(0); } catch (Exception e) { System.out.println(":::::::: Thread Join Error :::::::::::\n" + e); } } } catch (Exception e) { System.out.println(":::::::::::::: Error :::::::::::::::::"); e.printStackTrace(); } System.out.println("\nEnd statefulSession.MultiClient...\n"); } /** * Parses an argument list for a url, user or password. * * @param args[] String array of arguments */ static void parseArgs(String args[]) { if ((args == null) || (args.length == 0)) return; for (int i = 0; i < args.length; i++) { if (args[i].equals("-url")) url = args[++i]; else if (args[i].equals("-user")) user = args[++i]; else if (args[i].equals("-password")) password = args[++i]; } } /** * 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 { Properties p = new Properties(); p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); p.put(Context.PROVIDER_URL, url); if (user != null) { System.out.println ("user: " + user); p.put(Context.SECURITY_PRINCIPAL, user); if (password == null) password = ""; p.put(Context.SECURITY_CREDENTIALS, password); } return new InitialContext(p); } } /** * This class creates the MultiClientBigTrader threads used by * MultiClient. * * @author Copyright (c) 1998-1999 by BEA Systems, Inc. All Rights Reserved. * @see MultiClient */ class MultiClientBigTrader extends Thread { int id; Trader trader; String customer; /** * Creates a MultiClientBigTrader with an ID and * an account. * * @param id int ID * @param trader Trader Trader * @param customer String Customer * @see MultiClient#main */ public MultiClientBigTrader(int id, Trader trader, String customer) { this.id = id; this.trader = trader; this.customer = customer; } /** * Runs a MultiClientBigTrader and makes it * conduct transactions for its account. * @see MultiClient#main */ public void run() { try { String traderName = trader.getTraderName(); for (int i = 0; i < MultiClient.numTransactionsPerTrader; i++) { System.out.println(traderName + ": Start of Transaction " + i + " for " + customer); String stockName; int numberOfShares; // Buying stockName = "WEBL"; numberOfShares = 100 * i; System.out.println(traderName + ": Buying " + numberOfShares + " of " + stockName); TradeResult tr = trader.buy(customer, stockName, numberOfShares); System.out.println(traderName + ": Bought " + tr.numberTraded + " at $" + tr.priceSoldAt); // Selling stockName = "INTL"; numberOfShares = 100 * (i+1); System.out.println(traderName + ": Selling " + numberOfShares + " of " + stockName); tr = trader.sell(customer, stockName, numberOfShares); System.out.println(traderName + ": Sold " + tr.numberTraded + " at $" + tr.priceSoldAt); // Get change in Cash Account from EJBean System.out.println(traderName + ": Change in Cash Account: $" + trader.getBalance()); System.out.println(traderName + ": End of Transaction " + i + "\n"); } System.out.println(traderName + ": Change in Cash Account: $" + trader.getBalance() + "\n"); } catch (Exception e) { System.out.println("Aborting thread " + id + ": " + weblogic.utils.StackTraceUtils.throwable2StackTrace(e)); } } }