LDAP的SizeLimitExceededException

来源:互联网 发布:python 2.6 2.7 编辑:程序博客网 时间:2024/06/03 13:39

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

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

public void getAllPerson() throws NamingException, IOException {SearchControls schCtrls = new SearchControls();// 返回属性String[] returnAttrs = { "userPrincipalName", "distinguishedName" };schCtrls.setReturningAttributes(returnAttrs);schCtrls.setSearchScope(SearchControls.SUBTREE_SCOPE);int pageSize = 100;byte[] cookie = null;ContextSource contextSource = ldapTemplate.getContextSource();DirContext ctx = contextSource.getReadWriteContext();LdapContext lCtx = (LdapContext) ctx;lCtx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.CRITICAL) });int totalResults = 0;do {AndFilter andF = new AndFilter();andF.and(new EqualsFilter("objectclass", "person")).and(new LikeFilter("userPrincipalName", "*"));NamingEnumeration<SearchResult> results = lCtx.search("",andF.toString(), schCtrls);while (results != null && results.hasMoreElements()) {SearchResult sr = results.next();Attributes attrs = sr.getAttributes();System.out.println(attrs.get("userPrincipalName").get());System.out.println(attrs.get("distinguishedName").get());totalResults++;}cookie = parseControls(lCtx.getResponseControls());lCtx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, cookie, Control.CRITICAL) });} while ((cookie != null) && (cookie.length != 0));lCtx.close();System.out.println("Total" + totalResults);}private static byte[] parseControls(Control[] controls)throws NamingException {byte[] cookie = null;if (controls != null) {for (int i = 0; i < controls.length; i++) {if (controls[i] instanceof PagedResultsResponseControl) {PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];cookie = prrc.getCookie();System.out.println(">>Next Page \n");}}}return (cookie == null) ? new byte[0] : cookie;}

其中的cookie是告诉服务器目前已获得的条数,便于下页获取。

原创粉丝点击