快捷搜索:  as

JLDAP 连接池的创建及测试程序

因为是基于web开拓,以是自己想写一个像tomcat治理数据库连接池一样的东东。

大年夜体思路是这样的,由LDAPConectionDataFactory来创建LDAPConectionData类,在把创建好的实例放到治理类LDAPConectionDataPool(池)里边;用户可以定义最多的LDAPConection链接的数量。

1、LDAPConectionData类

Java代码

import com.novell.ldap.LDAPConnection;

import com.novell.ldap.LDAPException;

public class LDAPConectionData {

private boolean conetIsFree=true;//判断该链接是否被占用(链接是可用的,照样已经在应用中的)

private boolean isClosed=false;//判断链接是否关闭,在这里险些我没有用到该标志。

private LDAPConnection conn=null;//LDAP链接。

public LDAPConectionData(String ldapURl,int port,String user,String passwd) throws LDAPException{//初始化LDAP链接

conn=new LDAPConnection();

conn.connect(ldapURl, port);

conn.bind(LDAPConnection.LDAP_V3, user, passwd);

System.out.println("------LDAPConnection create successfully!-----");

}

public boolean getConetIsFree() {

return conetIsFree;

}

public void setConetIsFree(boolean conetIsFree) {

this.conetIsFree = conetIsFree;

}

public boolean getClosed() {

return isClosed;

}

public void setClosed(boolean isClosed) {

this.isClosed = isClosed;

}

public LDAPConnection getConn() {

return this.conn;

}

public void closeLDAPConnection(){

if(this.conn!=null){

try {

conn.disconnect();

} catch (LDAPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

import com.novell.ldap.LDAPConnection;

import com.novell.ldap.LDAPException;

public class LDAPConectionData {

private boolean conetIsFree=true;//判断该链接是否被占用(链接是可用的,照样已经在应用中的)

private boolean isClosed=false;//判断链接是否关闭,在这里险些我没有用到该标志。

private LDAPConnection conn=null;//LDAP链接。

public LDAPConectionData(String ldapURl,int port,String user,String passwd) throws LDAPException{//初始化LDAP链接

conn=new LDAPConnection();

conn.connect(ldapURl, port);

conn.bind(LDAPConnection.LDAP_V3, user, passwd);

System.out.println("------LDAPConnection create successfully!-----");

}

public boolean getConetIsFree() {

return conetIsFree;

}

public void setConetIsFree(boolean conetIsFree) {

this.conetIsFree = conetIsFree;

}

public boolean getClosed() {

return isClosed;

}

public void setClosed(boolean isClosed) {

this.isClosed = isClosed;

}

public LDAPConnection getConn() {

return this.conn;

}

public void closeLDAPConnection(){

if(this.conn!=null){

try {

conn.disconnect();

} catch (LDAPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

2、LDAPConectionDataPool类,该类用来获取连接,并开释链接(治理连接资本)。

Java代码

import java.util.ArrayList;

import java.util.List;

import com.novell.ldap.LDAPException;

final class LDAPConectionDataPool {

private List pool = new ArrayList();//天生的LDAPConectionData实例,一切的都放到该List中,进行治理。

private static LDAPConectionDataPool LDAPConnDataPool = null;//把该类设置成一个单点模式。

private LDAPConectionDataPool() {

}

public static synchronized LDAPConectionDataPool getInstance() {

if (LDAPConnDataPool == null) {

return new LDAPConectionDataPool();

} else {

return LDAPConnDataPool;

}

}

public void add(LDAPConectionData aa) {

aa.setConetIsFree(false);

this.pool.add(aa);

}

public synchronized void setConectionDataFree(LDAPConectionData da) {//用完后设置该连接为余暇状态,以便其他线程重用,并把壅闭的线程唤醒。

if (!da.getConetIsFree()) {

da.setConetIsFree(true);

this.notifyAll();

}

}

public synchronized LDAPConectionData getLDAPConectionData()

throws InterruptedException {//调用该措施返回一个LDAPConectionData

LDAPConectionData datatemp = null;

if (this.getSize() < LDAPConectionDataFactory.MAX_NUM_CONN) {//假如孕育发生的LDAPConectionData还没有达到最大年夜连接数,则可以从工厂里获取。

if (getSize() == 0 || datatemp == null) {

try {

datatemp = LDAPConectionDataFactory.createLDAPConection();

System.out.println(getSize());

this.add(datatemp);

} catch (LDAPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

} else {//假如已经达到最大年夜连接数了。那么就的等待,知道有余暇的LDAPConectionData可以应用。

wait();

for (LDAPConectionData data : pool) {

if (data.getConetIsFree())

datatemp = data;

break;

}

}

return datatemp;

}

public int getSize() {

return pool.size();

}

public void removeAll() {

closeAll();

pool.clear();

}

public void closeAll() {

for (LDAPConectionData data : pool) {

data.closeLDAPConnection();

}

}

public LDAPConectionData hasFree() {//这个措施险些没有用。

if (getSize() < 1)

return null;

LDAPConectionData datatemp = null;

for (LDAPConectionData data : pool) {

if (data.getConetIsFree())

datatemp = data;

break;

}

return datatemp;

}

}

import java.util.ArrayList;

import java.util.List;

import com.novell.ldap.LDAPException;

final class LDAPConectionDataPool {

private List pool = new ArrayList();//天生的LDAPConectionData实例,一切的都放到该List中,进行治理。

private static LDAPConectionDataPool LDAPConnDataPool = null;//把该类设置成一个单点模式。

private LDAPConectionDataPool() {

}

public static synchronized LDAPConectionDataPool getInstance() {

if (LDAPConnDataPool == null) {

return new LDAPConectionDataPool();

} else {

return LDAPConnDataPool;

}

}

public void add(LDAPConectionData aa) {

aa.setConetIsFree(false);

this.pool.add(aa);

}

public synchronized void setConectionDataFree(LDAPConectionData da) {//用完后设置该连接为余暇状态,以便其他线程重用,并把壅闭的线程唤醒。

if (!da.getConetIsFree()) {

da.setConetIsFree(true);

this.notifyAll();

}

}

public synchronized LDAPConectionData getLDAPConectionData()

throws InterruptedException {//调用该措施返回一个LDAPConectionData

LDAPConectionData datatemp = null;

if (this.getSize() < LDAPConectionDataFactory.MAX_NUM_CONN) {//假如孕育发生的LDAPConectionData还没有达到最大年夜连接数,则可以从工厂里获取。

if (getSize() == 0 || datatemp == null) {

try {

datatemp = LDAPConectionDataFactory.createLDAPConection();

System.out.println(getSize());

this.add(datatemp);

} catch (LDAPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

} else {//假如已经达到最大年夜连接数了。那么就的等待,知道有余暇的LDAPConectionData可以应用。

wait();

for (LDAPConectionData data : pool) {

if (data.getConetIsFree())

datatemp = data;

break;

}

}

return datatemp;

}

public int getSize() {

return pool.size();

}

public void removeAll() {

closeAll();

pool.clear();

}

public void closeAll() {

for (LDAPConectionData data : pool) {

data.closeLDAPConnection();

}

}

public LDAPConectionData hasFree() {//这个措施险些没有用。

if (getSize() < 1)

return null;

LDAPConectionData datatemp = null;

for (LDAPConectionData data : pool) {

if (data.getConetIsFree())

datatemp = data;

break;

}

return datatemp;

}

}

3.LDAPConectionDataFactory工厂类。用来创建连接的类。

Java代码

import com.novell.ldap.LDAPException;

public class LDAPConectionDataFactory {

private static final String LDAP_URL = "192.168.1.21";//连接地址

private static final int LDAP_PORT = 20000;//端口。

private static final String MANAGER_USR = "cn=admin";//用户名

private static final String MANAGER_PASSWD = "12345678";//密码

publicstatic final int MAX_NUM_CONN = 2;//用户可以在这儿设置最多天生几个链接。

public staticLDAPConectionData createLDAPConection() throws LDAPException {

return new LDAPConectionData(

LDAPConectionDataFactory.LDAP_URL,

LDAPConectionDataFactory.LDAP_PORT,

LDAPConectionDataFactory.MANAGER_USR,

LDAPConectionDataFactory.MANAGER_PASSWD);

}

}

import com.novell.ldap.LDAPException;

public class LDAPConectionDataFactory {

private static final String LDAP_URL = "192.168.1.21";//连接地址

private static final int LDAP_PORT = 20000;//端口。

private static final String MANAGER_USR = "cn=admin";//用户名

private static final String MANAGER_PASSWD = "12345678";//密码

publicstatic final int MAX_NUM_CONN = 2;//用户可以在这儿设置最多天生几个链接。

public staticLDAPConectionData createLDAPConection() throws LDAPException {

return new LDAPConectionData(

LDAPConectionDataFactory.LDAP_URL,

LDAPConectionDataFactory.LDAP_PORT,

LDAPConectionDataFactory.MANAGER_USR,

LDAPConectionDataFactory.MANAGER_PASSWD);

}

}

以上便是先容的整个。

还有,再贴上测试类和运行结果。

Java代码

import com.novell.ldap.LDAPAttribute;

import com.novell.ldap.LDAPAttributeSet;

import com.novell.ldap.LDAPConnection;

import com.novell.ldap.LDAPEntry;

import com.novell.ldap.LDAPException;

public class ThreadTest extends Thread {

private int uid;

public LDAPConectionDataPool pool=null;

public ThreadTest(int uid,LDAPConectionDataPool pl) {

this.uid = uid;

this.pool=pl;

}

public void run() {

System.out.println("Thread:"+uid+" is run!");

LDAPAttributeSet attributeSet= new LDAPAttributeSet();

attributeSet.add(new LDAPAttribute("objectclass", new String(

"inetOrgPerson")));

attributeSet.add(new LDAPAttribute("cn", new String[] { "李",

"Jim Smith", "Jimmy Smith" }));

attributeSet.add(new LDAPAttribute("givenname", new String[] { "测试",

"Jim", "Jimmy" }));

attributeSet.add(new LDAPAttribute("sn", new String("Smith")));

attributeSet.add(new LDAPAttribute("telephonenumber", new String(

"1 801 555 1212")));

attributeSet.add(new LDAPAttribute("mail",

new String("JSmith@Acme.com")));

attributeSet.add(new LDAPAttribute("userpassword", new String(

"newpassword")));

LDAPEntry entry = new LDAPEntry("uid="+uid+",ou=Code,cn=Lizl,dc=4a,dc=4adomain",

attributeSet);

LDAPConectionData data=null;

try {

data = pool.getLDAPConectionData();

} catch (InterruptedException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

try {

LDAPConnection lc=data.getConn();

lc.add(entry);

} catch (LDAPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

if(data!=null)

pool.setConectionDataFree(data);

}

}

public static void main(String args[]) {

LDAPConectionDataPool pool=LDAPConectionDataPool.getInstance();

ThreadTesta=new ThreadTest(400,pool);

ThreadTestb=new ThreadTest(401,pool);

ThreadTestc=new ThreadTest(402,pool);

ThreadTestd=new ThreadTest(403,pool);

ThreadTeste=new ThreadTest(404,pool);

ThreadTestf=new ThreadTest(405,pool);

ThreadTestg=new ThreadTest(406,pool);

ThreadTesth=new ThreadTest(407,pool);

ThreadTesti=new ThreadTest(408,pool);

ThreadTestj=new ThreadTest(409,pool);

ThreadTestk=new ThreadTest(410,pool);

a.start();

b.start();

c.start();

d.start();

e.start();

f.start();

g.start();

h.start();

i.start();

j.start();

k.start();

}

}

您可能还会对下面的文章感兴趣: