在项目中不同系统的交互要不断的创建和销毁对象,为了减少对象的创建和销毁,采用相关的连接池使用,减少内存的占用。
package org.quickserver.util.pool;
import org.apache.commons.pool.PoolableObjectFactory;
public abstract interface PoolableObject
{
public abstract boolean isPoolable();
public abstract PoolableObjectFactory getPoolableObjectFactory();
}
自动连接处的实现如下:
package com.easyway.opensource.quickserver.chat.server;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.PoolableObjectFactory;
import org.quickserver.net.server.ClientData;
import org.quickserver.net.server.ClientIdentifiable;
import org.quickserver.util.pool.PoolableObject;
/***
* 实现相关的对象池,以及各个系统中交互对象ClientData的实现
* ,由于在系统可能对象之间交互比较频繁那么最好创建一个对象池以减轻
* 服务器的压力的,要实现对象池必须实现相关的PoolableObject或者 BasePoolableObjectFactory类。
*
* ClientIdentifiable用于设置关于客户端的唯一标识的接口
*
* ClientData实现交互对象必须提供的接口对象
*
* @author longgangbai
*
*/
public class ChatData implements ClientData, ClientIdentifiable, PoolableObject
{
private static final Logger logger = Logger.getLogger(ChatData.class.getName());
private static Set usernameList = new HashSet();
private String username;
private String room;
private String info;
private String lastAsked;
private byte[] password;
public ChatData()
{
this.username = null;
this.room = null;
this.info = null;
this.lastAsked = null;
this.password = null; }
public void setLastAsked(String lastAsked) {
this.lastAsked = lastAsked; }
public String getLastAsked() {
return this.lastAsked;
}
public void setPassword(byte[] password) {
this.password = password; }
public byte[] getPassword() {
return this.password;
}
public boolean registerUsername(String username) {
return usernameList.add(username); }
public void deregisterUsername(String username) {
usernameList.remove(username); }
public void setUsername(String username) {
this.username = username; }
public String getUsername() {
return this.username;
}
public void setRoom(String room) {
this.room = room; }
public String getRoom() {
return this.room;
}
public String getClientId() {
return this.username;
}
public String getClientKey() {
if (this.room == null) {
return this.username;
}
return this.username + "@" + this.room;
}
public void setClientInfo(String info) {
this.info = info; }
public String getClientInfo() {
return getClientKey() + "\t- " + this.info;
}
public String toString() {
return getClientInfo();
}
/**
* 情况相关的信息
*/
public void clean()
{
usernameList.remove(this.username);
this.username = null;
this.room = null;
this.info = null;
this.lastAsked = null;
}
/**
* 是否具有对象池的能力
*/
public boolean isPoolable() {
return true;
}
/***
* 实现相关的对象池相关的内部类
*/
public PoolableObjectFactory getPoolableObjectFactory() {
/***
* 实现相关的对象池相关的内部类
*/
return new BasePoolableObjectFactory() {
/**
* 创建相关的对象的方法
*/
public Object makeObject() {
return new ChatData();
}
/**
* 激活相关的方法
*/
public void passivateObject(Object obj) {
ChatData ed = (ChatData)obj;
ed.clean();
}
/**
* 销毁相关对象的方法
*/
public void destroyObject(Object obj) {
if (obj == null) return;
passivateObject(obj);
obj = null;
}
/**
* 检测对象是否有效
*/
public boolean validateObject(Object obj) {
return (obj != null);
}
};
}
}
相关的连接池的参数配置如下:
<object-pool>
<max-active>-1</max-active>
<max-idle>20</max-idle>
<thread-object-pool>
<max-active>-1</max-active>
<max-idle>20</max-idle>
</thread-object-pool>
<client-handler-object-pool>
<max-active>-1</max-active>
<max-idle>20</max-idle>
</client-handler-object-pool>
<byte-buffer-object-pool>
<max-active>-1</max-active>
<max-idle>20</max-idle>
</byte-buffer-object-pool>
<client-data-object-pool>
<max-active>-1</max-active>
<max-idle>20</max-idle>
</client-data-object-pool>
</object-pool>
在服务端的获取如下:
获取:ClientData clientData=handler.getClientData();
发送如下: handler.sendClientObject(new Date());