📄 connectionpool.java
字号:
/**
* File Name:ConnectionPool.java Company: 中国移动集团公司 Date : 2004-1-9
*/
package com.cmcc.mm7.vasp.common;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;
import com.cmcc.mm7.vasp.conf.MM7Config;
import com.cmcc.mm7.vasp.conf.MM7ConfigManager;
public class ConnectionPool implements Runnable {
// public static List ClientList;
private static final Logger log = Logger.getLogger(ConnectionPool.class);
public List ClientList;
public static boolean isCreate;
public HashMap hashmap;
private long time;
private int IPCount;
private MM7Config Mm7Config;
private String NonceCount;
private int ServerMaxSize;
private String KeepAlive;
private static final ConnectionPool m_instance = new ConnectionPool();
public ConnectionPool() {
hashmap = new HashMap();
isCreate = false;
ClientList = null;
IPCount = 0;
Mm7Config = null;
NonceCount = "00000001";
ServerMaxSize = 0;
KeepAlive = "off";
}
public static ConnectionPool getInstance() {
return m_instance;
}
public void setConfig(MM7Config mm7config) {
Mm7Config = mm7config;
if (ClientList == null) {
if (log.isDebugEnabled())
log.debug("连接池对象初始化...........");
init();
}
if (!isCreate) {
//
// Thread thread = new Thread(this);
// thread.run();
isCreate = true;
}
}
// 主要是为了实现MMSCIP的平均分配而设置的顺序。
public void setIPCount(int count) {
IPCount = count;
}
// 得到当前应该分配给第几个MMSCIP。
public int getIPCount() {
return (IPCount);
}
public void setNonceCount(String nc) {
NonceCount = nc;
}
public String getNonceCount() {
return NonceCount;
}
public void setInitNonceCount() {
setNonceCount("00000001");
}
public MM7Config getConfig() {
return Mm7Config;
}
private void setServerMaxSize(int size) {
ServerMaxSize = size;
}
public int getServerMaxSize() {
return ServerMaxSize;
}
private void setKeepAlive(String conn) {
KeepAlive = conn;
}
public String getKeepAlive() {
return KeepAlive;
}
// 从配置文件中获得一些基本信息
private void init() {
hashmap.clear();
MM7ConfigManager confManager = new MM7ConfigManager();
String name = Mm7Config.getConnConfigName();
if (!name.equals("")) {
confManager.load(name);
hashmap = confManager.hashmap;
if (!hashmap.isEmpty()) {
this.setKeepAlive((String) hashmap.get("KeepAlive"));
this.setServerMaxSize(Integer.parseInt((String) hashmap.get("ServerMaxKeepAlive")));
if (log.isDebugEnabled())
log.debug("KeepAlive:" + this.getKeepAlive() + ",最大连接数:" + hashmap.get("MaxKeepAliveRequests")
+ ",最小连接数:" + hashmap.get("MinKeepAliveRequests"));
}
}
if (this.getKeepAlive().equals("on")) {
/** 若支持长连接,则建最小长连接数,若不支持,则建一条短连接 */
addURL(Integer.parseInt((String) hashmap.get("MinKeepAliveRequests")));
}
}
// 获得空闲的连接
public synchronized ConnectionWrap getConnWrap() {
if(ClientList!=null&&log.isDebugEnabled())
log.debug("现在连接池中的个数:"+ClientList.size());
if (ClientList == null) {
addURL(1);
ConnectionWrap connWrap;
if (ClientList.isEmpty()) {
return null;
}
else {
connWrap = (ConnectionWrap) ClientList.get(0);
connWrap.setFree(false);
connWrap.setConnectIndex(0);
if (log.isDebugEnabled())
log.debug("ClientList is null,新建一连接,从而获取一有效链接!!!");
return connWrap;
}
}
else {
// 寻找空闲的连接
for (int i = 0; i < ClientList.size(); i++) {
ConnectionWrap conn = (ConnectionWrap) ClientList.get(i);
if (conn != null && conn.getFree()) {
conn.setFree(false);
conn.setConnectIndex(i);
conn.start = System.currentTimeMillis();
return conn;
}
else if (conn == null) {
continue;
}
}
// 没有空闲连接的话,若size小于最大连接数,则建step步长的连接。
int MaxCount = Integer.parseInt((String) hashmap.get("MaxKeepAliveRequests"));
if (ClientList.size() < MaxCount) {
int step = Integer.parseInt((String) hashmap.get("step"));
/**
* 判断目前已有连接加入要新建的连接是否超过最大连接数,若不超,则建setp个连接,否则
* 新建(最大连接数-现有连接数)个连接。
*/
if (ClientList.size() + step <= MaxCount) {
log.info("无可用链接,且当前个数:" + ClientList.size() + "加步长个数:" + step + "<=最大个数:" + MaxCount + ",新建" + step
+ "个链接!!!");
addURL(step);
}
else {
log.info("无可用链接,且当前个数:" + ClientList.size() + "加步长个数:" + step + ">最大个数:" + MaxCount + ",新建"
+ (MaxCount - ClientList.size()) + "个链接!!!");
addURL(MaxCount - ClientList.size());
}
if (ClientList.isEmpty() == true) {
// addURL(step);
return null;
}
else {
ConnectionWrap conn = (ConnectionWrap) ClientList.get(ClientList.size() - step);
conn.setFree(false);
conn.setConnectIndex(ClientList.size() - step);
// added by huafeng on 2006/7/20
conn.setStart(System.currentTimeMillis());
return conn;
}
}
// 建一条短连接
else {
log.info("无可用链接,且当前个数:" + ClientList.size() + ">=最大个数:" + MaxCount + ",建立短链接!!!");
try {
ConnectionWrap conn = new ConnectionWrap(Mm7Config);
if (conn.BuidLink())
return conn;
else
return null;
}
catch (Exception e) {
// System.err.println(e);
log.error("超出最大可用连接后,新建连接失败:" + e);
return null;
}
}
}
}
// 增加count个新的URL连接
public void addURL(int count) {
// System.out.println("addURL"+count);
if (log.isDebugEnabled())
log.debug("加入" + count + "条连接到ClientList中");
if (ClientList == null)
ClientList = new ArrayList(count);
try {
for (int i = 0; i < count; i++) {
ConnectionWrap conn = new ConnectionWrap(Mm7Config);
if (conn.BuidLink()) {
ClientList.add(conn);
}
}
}
catch (Exception e) {
// e.printStackTrace();
log.error("ConnectionPool.addURL:" + e);
}
}
// 删除一个连接
public boolean deleteURL(ConnectionWrap connwrap) {
if (connwrap != null) {
int index = connwrap.getConnectIndex();
// System.out.println("connwrap != null!index="+index);
if (log.isDebugEnabled())
log.debug("deleteURL:从连接池中删除掉一个链接:index=" + index);
if (ClientList.isEmpty() == true)
return false;
else {
// System.out.println("size="+ClientList.size());
for (int i = 0; i < ClientList.size(); i++) {
if (index == i) {
ClientList.remove(index);
// added by liuhuafeng on
// 2006/7/27,删除掉一个连接的时候,顺便把这个连接给关闭掉
try {
connwrap.getSocket().close();
if (log.isDebugEnabled())
log.debug("关闭从连接池中删除掉的连接......");
}
catch (IOException e) {
log.error(e);
}
// 重新排序
Collections.reverse(ClientList);
return true;
}
}
return false;
}
}
else
return false;
}
/**
* 如下为对链接池中的链接进行管理,将超时的没再使用的链接,期待重新建立
*
*/
public void run() {
long interval = Long.parseLong((String) hashmap.get("KeepAliveTimeout"));
// modified by liuhuafeng on 2006/7/27,轮询检测是否有连接常时间没用,如果是的话,则将其从连接池中清掉
while (true) {
try {
Thread.sleep(interval*10);
}
catch (Exception e) {
e.printStackTrace();
}
scan();
}
}
// 管理连接。一些超时的连接刷新
// private void scan() {
public void scan(){
// int timeout = Integer.parseInt((String) hashmap.get("KeepAliveTimeout")); // 取得最长连接时间
int timeout=Mm7Config.getTimeOut();
if(log.isDebugEnabled())
log.debug("对连接进行管理.......");
if (ClientList != null) {
if (!ClientList.isEmpty()) {
for (int i = 0; i < ClientList.size(); i++) {
ConnectionWrap conn = (ConnectionWrap) ClientList.get(i);
time=System.currentTimeMillis()-conn.start;
if(time>=timeout){
synchronized(conn){
log.warn("连接"+timeout+"毫秒没有使用,从连接池中清除!!!");
deleteURL(conn);
}
}
// if (conn.start > 0) {
// time = System.currentTimeMillis() - conn.start;
// if (time >= timeout)
// conn.setFree(true);
// }
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -