LDAP的SizeLimitExceededException

来源:互联网 发布:数据信息加工 编辑:程序博客网 时间:2024/06/06 03:23

LDAP.search()当查询的数据较多时,数据条目大于LDAP服务器设置的最多数据时,就会出现SizeLimitExceededException。

解决方法之一是分页查询,控制每次查询的数目。


  1. public void getAllPerson() throws NamingException, IOException {  
  2.     SearchControls schCtrls = new SearchControls();  
  3.     // 返回属性  
  4.     String[] returnAttrs = { "userPrincipalName""distinguishedName" };  
  5.     schCtrls.setReturningAttributes(returnAttrs);  
  6.   
  7.     schCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);  
  8.   
  9.     int pageSize = 100;  
  10.     byte[] cookie = null;  
  11.   
  12.     ContextSource contextSource = ldapTemplate.getContextSource();  
  13.     DirContext ctx = contextSource.getReadWriteContext();  
  14.     LdapContext lCtx = (LdapContext) ctx;  
  15.     lCtx.setRequestControls(new Control[] { new PagedResultsControl(  
  16.             pageSize, Control.CRITICAL) });  
  17.     int totalResults = 0;  
  18.     do {  
  19.         AndFilter andF = new AndFilter();  
  20.         andF.and(new EqualsFilter("objectclass""person")).and(  
  21.                 new LikeFilter("userPrincipalName""*"));  
  22.   
  23.         NamingEnumeration<SearchResult> results = lCtx.search("",  
  24.                 andF.toString(), schCtrls);  
  25.         while (results != null && results.hasMoreElements()) {  
  26.             SearchResult sr = results.next();  
  27.             Attributes attrs = sr.getAttributes();  
  28.             System.out.println(attrs.get("userPrincipalName").get());  
  29.             System.out.println(attrs.get("distinguishedName").get());  
  30.             totalResults++;  
  31.         }  
  32.         cookie = parseControls(lCtx.getResponseControls());  
  33.         lCtx.setRequestControls(new Control[] { new PagedResultsControl(  
  34.                 pageSize, cookie, Control.CRITICAL) });  
  35.     } while ((cookie != null) && (cookie.length != 0));  
  36.     lCtx.close();  
  37.     System.out.println("Total" + totalResults);  
  38. }  
  39.   
  40. private static byte[] parseControls(Control[] controls)  
  41.         throws NamingException {  
  42.     byte[] cookie = null;  
  43.     if (controls != null) {  
  44.         for (int i = 0; i < controls.length; i++) {  
  45.             if (controls[i] instanceof PagedResultsResponseControl) {  
  46.                 PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];  
  47.                 cookie = prrc.getCookie();  
  48.                 System.out.println(">>Next Page \n");  
  49.             }  
  50.         }  
  51.     }  
  52.     return (cookie == null) ? new byte[0] : cookie;  

转自:http://blog.csdn.net/whuqin/article/details/7448531



0 0