LDAP 主动通知

LDAP v3(RFC 2251)定义了未经请求的通知,这是 LDAP 服务器发送给 Client 端的消息,没有来自 Client 端的任何挑衅。 JNDI 中的UnsolicitedNotificationinterface代表了未经请求的通知。

由于未经请求的通知是由服务器异步发送的,因此可以使用与接收有关名称空间更改和对象内容更改的通知相同的event model。您可以通过在EventContextEventDirContext上注册UnsolicitedNotificationListener来注册接收不请自来的通知的兴趣。

这是UnsolicitedNotificationListener中的an example

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实现的an example。请注意,只有EventContext.addNamingListener()的 listener 参数是相关的。名称和范围参数与未经请求的通知无关。

// Get the event context for registering the listener
EventContext ctx = (EventContext)
    (new InitialContext(env).lookup("ou=People"));

// Create the listener
NamingListener listener = new UnsolListener();

// Register the listener with the context (all targets equivalent)
ctx.addNamingListener("", EventContext.ONELEVEL_SCOPE, listener);

运行此程序时,您需要将其指向可以生成未经请求的通知并促使服务器发出通知的 LDAP 服务器。否则,一分钟后,程序将静默退出。

实现UnsolicitedNotificationListener的侦听器还可以实现其他NamingListenerinterface,例如NamespaceChangeListenerObjectChangeListener