GBase异步查询示例分析

本示例说明在单个服务器的不同子树上执行多重查询,需要以下步骤:

1. 连接到 LDAP 服务器。

2. 对其中的一个查询创建查询监听器。

3. 其它的查询共享(多重)查询监听器。

4. 获得和处理查询结果,由于是多重查询,每检索到一条满足条件的

条目,就返回给客户端,客户端需要将条目区分为是哪一个请求的

结果,将不同的请求结果分别存储至不同的集合,待所有满足条目

的结果都返回后,从结果集合中输出条目给客户。

5. 断开与服务器的连接。

代码:

import itec.ldap.*;

import java.util.*

public class ITECAsychSearch

{

public static void main(String[] args)

{

int status = -1;

//LDAP 连接对象

LDAPConnection[] ld = new LDAPConnection[3];

//连接 LDAP 服务器的 IP 及端口号

//用户在使用本程序时,将这些数据

//更改为用户所使用的 IP(域名)及端口号

final String MY_HOST = "127.0.0.1";

final int MY_PORT = 123;

//查询过滤条件

final String MY_FILTER = "objectclass=*";

//查询范围

final String[] bases = {"sn=abc0001,dc=AB",

"sn=abc0002,dc=AB",

"sn=abc0003,dc=AB"};

try

{

//连接 LDAP 服务器

for (int i = 0; i < 3; i++)

{

ld[i] = new LDAPConnection();

ld[i].connect(MY_HOST, MY_PORT);

}

//获取查询监听器

LDAPSearchlistener ls = ld[0].search(bases[0],

ld[0].SCOPE_SUB,

MY_FILTER,

null,

false,

(LDAPSearchlistener)null);

//其它连接共享查询监听器

for (int j = 1; j < bases.length; j++)

{

ld[j].search(bases[j],

ld[j].SCOPE_SUB,

MY_FILTER,

null,

false,

ls);

}

//返回所有请求的信息 IDs

int[] ids = ls.getMessageIDs();

//每一次查询返回的序号并不是查询请求

//的序号,而是一组连续的随机序号.为得到

//请求时相应的序号,这些返回的序号之间以

//最小的序号为基线,其它的减去这个基线.

int baseline = 0;

int i = 0;

//从最小的查询返回序号中获取基线

while (i < ids.length - 1)

{

for (int j = i + 1; j < ids.length; j++)

{

if (ids[i] > ids[j])

{

i = j;

}

}

baseline = ids[i];

break;

}

//对结果进行循环操作直到结束

LDAPMessage msg = null;

for (int n = 0; (msg = ls.getResponse()) != null; n++)

{

//因为查询返回的序号是一个随机数

//减去基线,则得到查询请求时的序号

m_messageid = msg.getMessageID() - baseline;

//查询结果是 Reference

//用户在使用本程序时,不要在目录服务器中存放有

Referral

//的条目.有关 Referral 的示例在别的程序中提供

//为保持代码的完整性,以下保留了有关 Referral 的代

if (msg instanceof LDAPSearchResultReference)

{

try

{

m_references

=

(list)m_referencelist.get(m_messageid);

}

catch (IndexOutOfBoundsException e)

{

m_references = new Arraylist();

for (int j = m_referencelist.size(); j <=

m_messageid; j++)

{

m_referencelist.add(j, new Arraylist());

}

}

m_references.add(

((LDAPSearchResultReference)msg).getUrls());

m_referencelist.set(m_messageid, m_references);

}

//查询条目结果

else if (msg instanceof LDAPSearchResult)

{

try

{

m_entrys = (list)m_entrylist.get(m_messageid);

}

catch (IndexOutOfBoundsException e)

{

m_entrys = new Arraylist();

for (int j = m_entrylist.size(); j <= m_messageid;

j++)

{

m_entrylist.add(j, new Arraylist());

}

}

//每个条目放入一个 list 类型的容器 entrys

m_entrys.add(((LDAPSearchResult)msg).getEntry());

//存放条目的 list 又放入一个 list 类型的容器

entrylist

//其中重复的 list 被忽略

m_entrylist.set(m_messageid, m_entrys);

}

else

{

LDAPResponse res = (LDAPResponse)msg;

int result = res.getResultCode();

//查询结束

if (result == LDAPException.SUCCESS)

{

System.out.println("Asychronous

Search

end.");

}

}

}

status = 0;

}

catch (LDAPException e)

{

System.out.println(e.toString());

}

list ref = null;

list ent = null;

for (int i = 0; i < 3; i++)

{

//打印 Referral 查询结果

if (!m_referencelist.isEmpty())

{

if (i < m_referencelist.size())

{

ref = (list)m_referencelist.get(i);

if (!ref.isEmpty())

{

System.out.println("Search url result:");

Iterator it = ref.iterator();

while (it.hasNext())

{

System.out.println(it.next());

}

}

}

}

//打印条目查询结果

if (!m_entrylist.isEmpty())

{

if (i < m_entrylist.size())

{

ent = (list)m_entrylist.get(i);

if (!ent.isEmpty())

{

System.out.println("Search entry result "

+ (i+1) + ":");

Iterator it = ent.iterator();

while (it.hasNext())

{

System.out.println(it.next());

}

}

}

}

}

//断开与 LDAP 服务器的连接

for (int k = 0; k < ld.length; k++)

{

if ((ld[k] !=null) && (ld[k].isConnected()))

{

try

{

ld[k].disconnect();

}

catch (LDAPException e)

{

System.out.println("Error: " + e.toString());

}

}

}

System.exit(status);

}

//存放一个 Referral 查询结果

private static list m_references = null;

//存放所有 references

private static list m_referencelist = new Arraylist();

//存放一个条目查询结果

private static list m_entrys = null;

//存放所有 entrys

private static list m_entrylist = new Arraylist();

//查询请求返回的 ID

private static int m_messageid = -1;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值