由于是基于web开发,所以自己想写一个像tomcat管理数据库连接池一样的东东。
大体思路是这样的,由LDAPConectionDataFactory来创建LDAPConectionData类,在把创建好的实例放到管理类LDAPConectionDataPool(池)里边;用户可以定义最多的LDAPConection链接的数量。
1、LDAPConectionData类
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类,该类用来获取连接,并释放链接(管理连接资源)。
import java.util.ArrayList; import java.util.List; import com.novell.ldap.LDAPException; final class LDAPConectionDataPool implements java.io.Serializable { private List<LDAPConectionData> pool = new ArrayList<LDAPConectionData>();//生成的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; } public void finalize()throws Throwable{ this.removeAll(); } }
3.LDAPConectionDataFactory工厂类。用来创建连接的类。
import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchControls; import javax.naming.directory.SearchResult; import com.novell.ldap.LDAPException; public class LDAPConectionDataFactory { private static final String LDAP_URL = "192.168.16.21";// 连接地址 private static final int LDAP_PORT = 20000;// 端口。 private static final String MANAGER_USR = "cn=admin";// 用户名 private static final String MANAGER_PASSWD = "12345678";// 密码 public static final int MAX_NUM_CONN = 2;// 用户可以在这儿设置最多生成几个链接。 public static LDAPConectionData createLDAPConection() throws LDAPException { return new LDAPConectionData(LDAPConectionDataFactory.LDAP_URL, LDAPConectionDataFactory.LDAP_PORT, LDAPConectionDataFactory.MANAGER_USR, LDAPConectionDataFactory.MANAGER_PASSWD); } }
以上就是介绍的全部。
还有,再贴上测试类和运行结果。
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("[email protected]"))); 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); System.out.println("POOL's maxsize="+pool.getSize()); } 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(); ThreadTest a = new ThreadTest(431, pool); ThreadTest b = new ThreadTest(421, pool); ThreadTest c = new ThreadTest(422, pool); ThreadTest d = new ThreadTest(423, pool); ThreadTest e = new ThreadTest(424, pool); ThreadTest f = new ThreadTest(425, pool); ThreadTest g = new ThreadTest(426, pool); ThreadTest h = new ThreadTest(427, pool); ThreadTest i = new ThreadTest(428, pool); ThreadTest j = new ThreadTest(429, pool); ThreadTest k = new ThreadTest(430, pool); try { a.join(); b.join(); c.join(); d.join(); e.join(); f.join(); g.join(); h.join(); i.join(); k.join(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } a.start(); b.start(); c.start(); d.start(); e.start(); f.start(); g.start(); h.start(); i.start(); j.start(); k.start(); } }
运行结果如下:
Thread:400 is run!
Thread:401 is run!
Thread:403 is run!
Thread:405 is run!
Thread:402 is run!
Thread:404 is run!
Thread:406 is run!
Thread:408 is run!
Thread:407 is run!
Thread:409 is run!
Thread:410 is run!
------LDAPConnection create successfully!-----
0
------LDAPConnection create successfully!-----
1