文档

Java™教程
隐藏目录
LDAP非请求通知
路径:Java命名和目录接口
课程:LDAP用户的高级主题
章节:更多LDAP操作

LDAP非请求通知

LDAP v3(RFC 2251)定义了“非请求通知(unsolicited notification)”,即服务器在没有任何客户端请求的情况下向客户端发送的消息。在JNDI中,非请求通知通过UnsolicitedNotification接口表示。

由于非请求通知是由服务器异步发送的,因此您可以使用与接收有关命名空间更改和对象内容更改通知的事件模型相同的模型。通过向EventContextEventDirContext注册一个UnsolicitedNotificationListener来表示对非请求通知的兴趣。

以下是一个UnsolicitedNotificationListener的示例

public class UnsolListener implements UnsolicitedNotificationListener {
    public void notificationReceived(UnsolicitedNotificationEvent evt) {
        System.out.println("received: " + evt);
    }

    public void namingExceptionThrown(NamingExceptionEvent evt) {
        System.out.println(">>> UnsolListener got an exception");
            evt.getException().printStackTrace();
    }
}

以下是一个示例,通过事件源注册了UnsolicitedNotificationListener的实现。请注意,只有EventContext.addNamingListener()的监听器参数是相关的,名称和范围参数与非请求通知无关。

// 获取用于注册监听器的事件上下文
EventContext ctx = (EventContext)
    (new InitialContext(env).lookup("ou=People"));

// 创建监听器
NamingListener listener = new UnsolListener();

// 使用上下文注册监听器(所有目标等效)
ctx.addNamingListener("", EventContext.ONELEVEL_SCOPE, listener);

运行此程序时,您需要将其指向一个能够生成不请自来的通知并促使服务器发出通知的LDAP服务器。否则,程序将在一分钟后默默退出。

实现UnsolicitedNotificationListener的侦听器还可以实现其他NamingListener接口,如NamespaceChangeListenerObjectChangeListener


上一页:搜索结果
下一页:连接管理