package zero.net.contact;
import java.io.IOException;
import java.net.SocketException;
import java.util.List;
import java.util.ArrayList;
import zero.net.tool.SocketToolException;
/**
* TCP连接池
*
* @author 黄龙
*
*/
public class TCPConnectionPool{
protected List<TCPSocket> m_TCPSocketList; //TCP连接列表
protected String m_strHost; //连接池目标地址
protected int m_nPort; //连接池目标端口
protected int m_nMinCount; //最小连接数量
protected int m_nMaxCount; //最大连接数量
protected int m_nRequestCount; //当前请求数量
protected boolean m_bIsDestroy; //是否被销毁
protected static final int DEFAULT_CONNECTION_COUNT; //默认连接数量
protected static final int DEFAULT_SLEEP_TIME; //默认的睡眠时间
protected static final int DEFAULT_LINGER_TIME; //默认等待时间
protected static final int DEFAULT_MIN_COUNT; //默认最小连接数量
protected static final int DEFAULT_MAX_COUNT; //默认最大连接数量
static{
DEFAULT_CONNECTION_COUNT = 30;
DEFAULT_SLEEP_TIME = 30;
DEFAULT_LINGER_TIME = 2000;
DEFAULT_MIN_COUNT = 1;
DEFAULT_MAX_COUNT = 40;
}
public TCPConnectionPool(){
m_TCPSocketList = null;
m_nRequestCount = 0;
m_bIsDestroy = true;
}
/**
* 创建连接池
*
* @param strHost 目标地址
* @param nPort 目标端口
*
* @throws IOException
* @throws SocketToolException
*/
public void create( String strHost, int nPort )
throws IOException, SocketToolException
{
destroy();
m_strHost = strHost;
m_nPort = nPort;
m_bIsDestroy = false;
initConnection(DEFAULT_CONNECTION_COUNT);
openAll(m_strHost, m_nPort);
setSoLingerAll(DEFAULT_LINGER_TIME);
setCountBound(DEFAULT_MIN_COUNT, DEFAULT_MAX_COUNT);
}
/**
* 销毁连接池
*
* @throws IOException
*/
public void destroy()
throws IOException
{
m_bIsDestroy = true;
if (null != m_TCPSocketList){
int nCount = getCount();
for (int nIndex = 0; nIndex < nCount; ++nIndex){
m_TCPSocketList.get(nIndex).close();
}
m_TCPSocketList = null;
m_nRequestCount = 0;
}
}
/**
* 设置延迟时间
*
* @param nTime 延迟时间(豪秒)
*
* @throws SocketToolException
* @throws SocketException
*/
public void setSoLingerAll( int nTime )
throws SocketException, SocketToolException
{
for (int nIndex = 0; nIndex < m_TCPSocketList.size(); ++nIndex){
m_TCPSocketList.get(nIndex).setSoLinger(nTime);
}
}
public void setCountBound( int nMin, int nMax )
throws SocketToolException
{
if (0 > nMin || 0 > nMax){
throw new SocketToolException
("setCountBound function error : parameter cannot negative");
}
if (nMin > nMax){
throw new SocketToolException
("setCountBound function error : nMin greater than nMax");
}
m_nMinCount = nMin;
m_nMaxCount = nMax;
}
public int getMinCount(){
return m_nMinCount;
}
public int getMaxCount(){
return m_nMaxCount;
}
/**
* 检查是否被销毁
*
* @return 如果被销毁返回true,否则返回false
*/
public boolean isDestroy(){
return m_bIsDestroy;
}
/**
* 获取请求数量
*
* @return 返回当前请求数量
*/
public int getRequestCount(){
return m_nRequestCount;
}
/**
* 借用连接
*
* @return 返回一个TCP连接,如果被销毁了则返回null
* @throws InterruptedException
*/
public TCPSocket borrowConnection()
throws InterruptedException
{
if (isDestroy()){
return null;
}
++m_nRequestCount;
synchronized(this){
int nCount = getCount();
while (true){
for (int nIndex = 0; nIndex < nCount; ++nIndex){
TCPSocket result= m_TCPSocketList.get(nIndex).borrowConnection();
if (null != result){
return result;
}
}
Thread.sleep(DEFAULT_SLEEP_TIME);
}
}
}
/**
* 归还连接
*
* @param connection 要归还的连接,必须是从该连接池中借出的
*/
public void returnConnection( TCPSocket connection ){
connection.returnConnection();
--m_nRequestCount;
}
/**
* 调整连接池的连接数量
*
* @param nCount 需要调整的数量
*
* @return 返回实际调整后的数量
*
* @throws IOException
*/
public synchronized int adjustCount( int nCount )
throws IOException
{
if (isDestroy()){
return 0;
}
if (nCount > getCount()){
addCount(nCount);
} else if (nCount < getCount()) {
reduceCount(getCount() - nCount);
}
return getCount();
}
/**
* 获取连接数量
*
* @return 返回当前连接数量
*/
public int getCount(){
return m_TCPSocketList.size();
}
/**
* 检查所有连接是否正常
*
* @throws IOException
*/
public synchronized void check()
throws IOException
{
if (isDestroy()){
return;
}
int nCount = getCount();
for (int nIndex = 0; nIndex < nCount; ++nIndex){
TCPSocket socket= m_TCPSocketList.get(nIndex);
if (!socket.isActive()){
socket.open(m_strHost, m_nPort);
}
}
}
/**
* 初始化所有连接
*
* @param nCount 连接数量
*/
protected void initConnection( int nCount ){
m_TCPSocketList = new ArrayList<TCPSocket>(nCount);
for (int nIndex = 0; nIndex < nCount; ++nIndex){
m_TCPSocketList.add(new TCPSocket());
}
}
/**
* 打开所有连接
*
* @param strHost 目标地址
* @param nPort 目标端口
*
* @throws IOException
*/
protected void openAll( String strHost, int nPort )
throws IOException
{
int nCount = getCount();
for (int nIndex = 0; nIndex < nCount; ++nIndex){
m_TCPSocketList.get(nIndex).open(strHost, nPort);
}
}
/**
* 添加连接
*
* @param nCount 添加后的数量
*
* @throws IOException
*/
protected synchronized void addCount( int nCount )
throws IOException
{
for (int nIndex = getCount(); nIndex < nCount; ++nIndex){
TCPSocket socket = new TCPSocket();
socket.open(m_strHost, m_nPort);
m_TCPSocketList.add(socket);
}
}
/**
* 减少连接
*
* @param nCount 减少的数量
*
* @throws IOException
*/
protected synchronized void reduceCount( int nCount )
throws IOException
{
for (int nIndex = 0, nReduceNum = 0; nIndex < getCount();){
if (nReduceNum == nCount || nIndex == getCount()){
break;
}
TCPSocket socket = m_TCPSocketList.get(nIndex);
if (!socket.isBusy()){
socket.close();
m_TCPSocketList.remove(nIndex);
nIndex = 0;
++nReduceNum;
} else {
++nIndex;
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
java做的短信平台 含JSP
共544个文件
java:180个
class:180个
gif:95个
5星 · 超过95%的资源 需积分: 9 22 下载量 159 浏览量
2011-01-21
18:27:08
上传
评论
收藏 798KB RAR 举报
温馨提示
java短信平台 socket连接池 包含jsp和java源文件 oracle数据库(无表结构)
资源推荐
资源详情
资源评论
收起资源包目录
java做的短信平台 含JSP (544个子文件)
LoginBean.class 5KB
TCPConnectionPool.class 4KB
Pager.class 4KB
WrapBuffer.class 4KB
DBConnBean.class 4KB
Pager5.class 4KB
Pager2.class 4KB
Pager1.class 4KB
Pager3.class 4KB
Pager6.class 4KB
Pager4.class 4KB
UserMessage.class 4KB
CollectionNoteAction.class 4KB
PageList1Action.class 4KB
PageList2Action.class 4KB
PageList3Action.class 4KB
PackHead.class 4KB
SearchAction.class 4KB
EditnoteAction.class 3KB
NoteManager.class 3KB
SocketTool.class 3KB
InfochangeAction.class 3KB
GetNoteAction.class 3KB
LoginAction.class 3KB
NoteMessage.class 3KB
NoteTypeManAction.class 3KB
GetpasswordAction.class 3KB
RemoveText.class 3KB
TCPSocket.class 3KB
IntegralcompositorAction.class 3KB
SearchAwardAction.class 3KB
PageAction.class 3KB
InitTcpServer.class 3KB
Note.class 3KB
Register1Action.class 3KB
DownloadnoteAction.class 2KB
Time.class 2KB
ManagerLoginAction.class 2KB
UsermanageAction.class 2KB
AuditingedAction.class 2KB
Register3Action.class 2KB
ModuleAction.class 2KB
TCPPoolHelper.class 2KB
AdminupdatepwdAction.class 2KB
Register2Action.class 2KB
ListBean.class 2KB
AwardQueryAction.class 2KB
AwardLogAction.class 2KB
DeleteNoteTypeAction.class 2KB
NoteDownloadRequest.class 2KB
SearchIntegralAction.class 2KB
UserCollNoteAction.class 2KB
PackBody.class 2KB
LeaguerRegisterRequest.class 2KB
UpdateNoteTypeAction.class 2KB
AuditingfalseAction.class 2KB
AuditingAction.class 2KB
LeaguerRegisterResponse.class 2KB
NoteCheckResultNotice.class 2KB
BufferOffset.class 2KB
Register3Form.class 2KB
AddsensitivityAction.class 2KB
AdminoperatelogAction.class 2KB
LeaguerGetPasswordResponse.class 2KB
LeaguerPasswordUpdNotice.class 2KB
LeaguerGetPasswordRequest.class 2KB
GetfriendAction.class 2KB
TodayHotNoteAction.class 2KB
SelectsensitivityAction.class 2KB
YesterdayHotNoteAction.class 2KB
NoteDownloadResponse.class 2KB
AddNoteTypeAction.class 2KB
SensitivitycongealAction.class 2KB
SelectadminAction.class 2KB
SensitivityfreeAction.class 2KB
AddadminAction.class 2KB
ModulemanageupdateAction.class 2KB
UsermanageBean.class 2KB
Auditing.class 2KB
InfochangeForm.class 2KB
DeletesensitivityAction.class 2KB
AwardBean.class 2KB
DeleteCollAction.class 2KB
Note.class 2KB
IntegralQueueAction.class 2KB
PrizeListAction.class 2KB
UnfreeNoteAction.class 2KB
PrizeIntegralBean.class 2KB
FreeNoteAction.class 2KB
AdminupdatepwdForm.class 2KB
Register2Form.class 2KB
CongealAction.class 2KB
UnPackBuffer.class 2KB
FreeAction.class 2KB
IndexAction.class 2KB
Register1Form.class 2KB
PackBuffer.class 2KB
PackHeadBean.class 2KB
SearchIntegralForm.class 2KB
Usermanage.class 2KB
共 544 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
- tty10002012-06-28内容不够多 ~~!!将就着用
qq123476556
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功