// Let this thread sleep because the next person must ready first
// This is also here to avoid that the threads start at the same time
Thread.sleep( lRandom.nextInt( 2 * Constants.ONE_MINUTE ) );
// Get bank first
Collection lBanks = getBankSession().getBanks();
BankData lBank = (BankData) lBanks.iterator().next();
mLog.debug( "run(), tread id: " + mId + ", got bank: " + lBank );
// Loop of business interactions
while( !mExit && mException == null ) {
CustomerData lCustomer = null;
mLog.debug( "run(), tread id: " + mId + ", create or find customer" );
if( lRandom.nextInt( 100 ) < 10 ) {
mLog.debug( "run(), thread id: " + mId + ", create new customer" );
lCustomer = getCustomerSession().createCustomer( lBank.getId(), "test", 100 );
mCustomerCount++;
mLog.debug( "run(), thread id: " + mId + ", new customer: " + lCustomer );
} else {
int i = 0;
while( lCustomer == null ) {
i++;
try {
int lCustomerId = lRandom.nextInt( mCustomerCount );
mLog.debug( "run(), thread id: " + mId + ", look up customer, id: " + lCustomerId );
lCustomer = getBankSession().getCustomer( "" + lCustomerId );
mLog.debug( "run(), thread id: " + mId + ", found customer: " + lCustomer );
}
catch( FinderException fe ) {
if( i > 100 ) {
throw fe;
}
}
}
}
mLog.debug( "run(), tread id: " + mId + ", create or find account" );
// Get accounts and decide if to create a new account
List lAccounts = (List) getCustomerSession().getAccounts( lCustomer.getId() );
AccountData lAccount = null;
if( lRandom.nextInt( 100 ) < 5 ) {
try {
lAccount = getCustomerSession().createAccount(
lCustomer.getId(), lRandom.nextInt( 3 ), 123
);
mLog.debug( "run(), thread id: " + mId + ", created account: " + lAccount );
}
catch( CreateException ce ) {
}
}
if( lAccount == null ) {
lAccount = (AccountData) lAccounts.get( lRandom.nextInt( lAccounts.size() ) );
mLog.debug( "run(), thread id: " + mId + ", got account: " + lAccount );
}
if( lAccount == null ) {
throw new RuntimeException( "Could not find an account" );
}
// Do some business methods
int lLoops = lRandom.nextInt( 10 );
for( int i = 0; i < lLoops; i++ ) {
int lSelection = lRandom.nextInt( 4 );
mLog.debug( "run(), thread: " + mId + ", business selection : " + lSelection );
switch( lSelection ) {
case 0:
// Withdraw money when balance is greater than 50
if( lAccount.getBalance() > 50 ) {
getAccountSession().withdraw( lAccount.getId(), lRandom.nextInt( 50 ) );
}
break;
case 1:
if( lAccounts.size() > 1 && lAccount.getBalance() > 50 ) {
AccountData lOtherAccount = null;
while( true ) {
lOtherAccount = (AccountData) lAccounts.get( lRandom.nextInt( lAccounts.size() ) );
if( lOtherAccount.getType() != lAccount.getType() ) {
// Found another account type
break;
}
}
while( true ) {
try {
getAccountSession().transfer( lAccount.getId(), lOtherAccount.getId(), lRandom.nextInt( 50 ) );
break;
}
catch( ServerException se ) {
checkServerException( se );
}
}
}
break;
case 2:
if( lAccount.getBalance() > 50 ) {
List lCustomers = (List) getBankSession().getCustomers( lBank.getId() );
if( lCustomers.size() > 1 ) {
CustomerData lOtherCustomer = null;
while( true ) {
lOtherCustomer = (CustomerData) lCustomers.get( lRandom.nextInt( lCustomers.size() ) );
if( !lOtherCustomer.getId().equals( lCustomer.getId() ) ) {