1 3 package org.objectweb.clusterDemo; 4 5 import java.util.Date ; 6 import javax.naming.Context ; 7 import javax.naming.InitialContext ; 8 import javax.rmi.PortableRemoteObject ; 9 import javax.transaction.UserTransaction ; 10 import java.lang.Thread ; 11 12 import org.objectweb.clusterDemo.*; 13 14 public class BankClient { 15 16 private static final int m_MAXTHREADS = 100; 17 18 private static int m_loops = 1; 19 20 private static int m_clients = 1; 21 22 private static void usage() { 23 System.out.println("Usage: java org.objectweb.clusterDemo.BankClient <args>"); 24 System.out.println(" -l <loop> number of loops per thread"); 25 System.out.println(" -c <client> number of client threads"); 26 System.out.println("default args:"); 27 System.out.println(" -l " + m_loops + " -c " + m_clients); 28 } 29 30 public static void main(String args[]) { 31 32 for (int argn = 0; argn < args.length; argn++) { 34 String s_arg = args[argn]; 35 Integer i_arg; 36 if ((s_arg.equals("-l") == true)) { 37 s_arg = args[++argn]; 38 i_arg = java.lang.Integer.valueOf(s_arg); 39 m_loops = i_arg.intValue(); 40 } else if ((s_arg.equals("-c") == true)) { 41 s_arg = args[++argn]; 42 i_arg = java.lang.Integer.valueOf(s_arg); 43 m_clients = i_arg.intValue(); 44 } else if (s_arg.equals("-?") == true) { 45 usage(); 46 System.exit(0); 47 } else { 48 usage(); 49 System.exit(2); 50 } 51 } 52 53 if (m_loops < 1) { 55 usage(); 56 System.exit(2); 57 } 58 if (m_clients < 0) { 59 usage(); 60 System.exit(2); 61 } 62 63 System.out.println("BankClient" + " -b " + m_loops + " -c " + m_clients); 64 65 BankClientThread t_thr[] = new BankClientThread[m_MAXTHREADS]; 67 long ttime[] = new long[m_MAXTHREADS]; 68 for (int p = 0; p < m_clients; p++) { 69 t_thr[p] = new BankClientThread(m_loops); 70 t_thr[p].start(); 71 } 72 for (int p = 0; p < m_clients; p++) { 73 try { 74 t_thr[p].join(); 75 ttime[p] = t_thr[p].getTime(); 76 77 } catch (InterruptedException e) { 78 System.out.println("ERROR: Problem in BankClientThread.join():\n" + e); 79 System.exit(2); 80 } 81 } 82 83 System.out.println("BankClient " + " -l " + m_loops + " -c " + m_clients); 84 for (int p = 0; p < m_clients; p++) { 85 System.out.println(" " + ttime[p]); 86 } 87 System.out.println(""); 88 } 89 90 } 91 92 class BankClientThread extends Thread { 93 94 private int m_loops; 95 96 private long m_time; 97 98 private Integer m_accountId; 99 100 public BankClientThread(int b) { 101 m_loops = b; 102 } 103 104 public void run() { 105 106 System.out.println("Running a BankClientThread..."); 107 BankSessionHome bsh; 108 BankSession bs = null; 109 110 Context initialContext = null; 111 try { 112 initialContext = new InitialContext (); 113 bsh = (BankSessionHome) initialContext.lookup("BankEJBBankSessionHome"); 114 bs = bsh.create(); 115 } catch (Exception e) { 116 System.out.println("ERROR: Problem initializing :\n" + e); 117 System.exit(2); 118 } 119 120 String clientName = "Client" + System.currentTimeMillis(); 122 try { 123 m_accountId = bs.createAccount(clientName, 1000); 124 } catch (Exception e) { 125 System.out.println("ERROR: Problem creating Account :\n" + e); 126 System.exit(2); 127 } 128 129 long d_begin = System.currentTimeMillis(); 131 132 System.out.println("Start loop"); 133 System.out.println("Each loop does 1 debit and 1 credit operations"); 134 135 for (int l = 0; l < m_loops; l++) { 136 137 try { 139 bs.debit(m_accountId, clientName, 100, "Retrait ATM"); 140 bs.credit(m_accountId, clientName, 100, "Depot ATM"); 142 144 } catch (Exception e) { 145 System.out.println("ERROR: Problem during debit / credit phase :\n" + e); 146 System.exit(2); 147 } 148 149 System.out.println("End of loop " + (l + 1) + " OK"); 150 } 151 152 long d_end = System.currentTimeMillis(); 153 m_time = (d_end - d_begin) / (m_loops); 154 System.out.println("Time per transaction in millisec: " + m_time); 156 } 157 158 public long getTime() { 159 return m_time; 160 } 161 } 162 | Popular Tags |