📄 accountmanagerimpl.java
字号:
package Implement;
import java.util.*;
import Bank.*;
import Database.AccountDAO;
import Database.AccountValObj;
import Pool.AccountImplPool;
import org.omg.CORBA.Object;
import org.omg.PortableServer.*;
public class AccountManagerImpl extends AccountManagerPOA {
/**
*获得服务器端送来的策略
*/
private String strategy;
/**
*命中的次数
*/
private int count = 0;
/**
*获得一个AccountDAO实例
*/
private AccountDAO database = new AccountDAO();
/**
*获得资源池 AccountImplPool一个实例
*/
private AccountImplPool accountImplPool = new AccountImplPool();
/**
*定义资源池里面存放的对象
*/
private AccountImpl accountImplPooledObject = null;
/**
*定义资源池里面存放的临时对象
*/
private AccountImpl temperoryPooledObject = null;
/**
*构造方法
*/
public AccountManagerImpl(String strategy) {
this.strategy = strategy;
}
/**
*@param name 帐户名称
*新开帐户
*/
public Account open(String name) {
System.out.println("新开帐户" + name);
Account account = null;
// 采用先来先淘汰策略
if (accountImplPool.getInusePoolLength() >= 300) {
realessePool(accountImplPool);
}
AccountValObj vo = database.AddAcounts(name);
accountImplPooledObject = (AccountImpl) accountImplPool.getPooledObject();
accountImplPooledObject.setId(vo.getId());
accountImplPooledObject.setName(vo.getName());
accountImplPooledObject.setBalance(vo.getBalance());
try {
Object obj = _default_POA().servant_to_reference(accountImplPooledObject);
account = AccountHelper.narrow(obj);
} catch (Exception e) {
e.printStackTrace();
}
return account;
}
/**
*@param id 根据用户的id获取用户帐户的信息.
*@param RecordIsNullException记录为空的异常
*查询帐户
*/
public Account getAccountById(String id) throws RecordIsNullException {
Account account = null;
boolean flag = false;
System.out.println("查询帐户" + id);
//判断资源池是否已满, 满则释放资源
if (accountImplPool.getInusePoolLength() >= 300) {
System.out.println("正在采用"+strategy+"策略释放资源池里面的实例, 并更新数据库里相应的信息....");
realessePool(accountImplPool);
}
//判断查询的对象是否在资源池里面
for (int i = 0; i < accountImplPool.getInusePoolLength(); i++) {
temperoryPooledObject = (AccountImpl) accountImplPool.getPooledObjectById(i);
if (temperoryPooledObject.getId().equals(id)) {
flag = true ;
break;
}
}
//查询对象在资源池里面, 直接从里面取出
if (flag) {
count++;
System.out.println("命中次数为" + count);
accountImplPooledObject = temperoryPooledObject;
}
//查询对象没在资源池里面, 从数据库里面取出
else {
accountImplPooledObject = (AccountImpl) accountImplPool.getPooledObject();
AccountValObj vo = database.getAccountById(id);
accountImplPooledObject.setId(vo.getId());
accountImplPooledObject.setName(vo.getName());
accountImplPooledObject.setBalance(vo.getBalance());
}
try {
//用根POA激活伺服对象
Object obj = _default_POA().servant_to_reference(accountImplPooledObject);
//将对象收窄为帐户类型
account = AccountHelper.narrow(obj);
} catch (Exception e) {
e.printStackTrace();
}
//返回找到的帐户
return account;
}
/**
*@param id 根据用户的id获取用户帐户的信息.
*@param RecordIsNullException记录为空的异常
*查询帐户
*/
public Account getAccountByIdNotpooling(String id) throws RecordIsNullException {
Account account = null;
System.out.println(id);
AccountImpl accountServant = new AccountImpl();
AccountValObj vo = database.getAccountById(id);
accountServant.setId(vo.getId());
accountServant.setName(vo.getName());
accountServant.setBalance(vo.getBalance());
try {
//用根POA激活伺服对象
Object obj = _default_POA().servant_to_reference(accountServant);
//将对象收窄为帐户类型
account = AccountHelper.narrow(obj);
} catch (Exception e) {
e.printStackTrace();
}
//返回找到的帐户
return account;
}
/**
*@param id, 根据用户的id删除帐户的信息
*删除帐户
*/
public void close(String id) {
database.deleteAccounts(id);
}
/**
*@param id用户id, name用户名称, balance用户存款额.
*更新帐户
*/
public void update(String id, String name, float balance) {
database.uptadeAccount(id, name, balance);
}
/**
*@param AccountImplPool释放资源池里的对象, 并把对象的状态写入数据库
*/
public void realessePool(AccountImplPool pool) {
// 采用先来先淘汰策略
if (strategy.equals("FirstInFirstOut")) {
for (int i = 0; i < 5; i++) {
System.out.println(i);
AccountImpl account = (AccountImpl)pool.getPooledObjectById(i);
update(account.getId(), account.getName(), account.getBalance());
pool.releasePooledObjectById(i);
}
}
// 采用随机策略
if (strategy.equals("Random")) {
for (int i = 0; i < 5; i++) {
Random random = new Random();
int j = Math.abs(random.nextInt())%100;
AccountImpl account = (AccountImpl)pool.getPooledObjectById(j);
update(account.getId(), account.getName(), account.getBalance());
pool.releasePooledObjectById(j);
}
}
// 采用最近使用策略
if (strategy.equals("RecentUse")) {
for (int i = 299; i > 295; i--) {
AccountImpl account = (AccountImpl)pool.getPooledObjectById(i);
update(account.getId(), account.getName(), account.getBalance());
pool.releasePooledObjectById(i);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -