Directory Notification

Active Directory provides a control for clients to register to receive changes notifications. Note the following constraints when configuring your search:

scope Must be one-level or object-level
filter Must be ‘(objectClass=*)’
attributes list of attributes to be returned when a change occurs. Does not specify attributes which will generate notifications.

A notification client is provided to encapsulate the asynchronous search and expose a blocking queue.

NotificationClient

SingleConnectionFactory factory = new SingleConnectionFactory(ConnectionConfig.builder()
  .url("ldap://directory.ldaptive.org")
  .connectionInitializers(
    new BindConnectionInitializer("cn=manager,ou=people,dc=ldaptive,dc=org", new Credential("manager_password")))
  .build());
factory.initialize();
NotificationClient client = new NotificationClient(factory);
SearchRequest request = SearchRequest.builder()
  .dn("ou=people,dc=ldaptive,dc=edu")
  .filter("(objectClass=*)")
  .scope(SearchScope.ONELEVEL)
  .build();
BlockingQueue<NotificationClient.NotificationItem> results = client.execute(request);
while (true) {
  NotificationClient.NotificationItem item = results.take(); // blocks until result is received
  if (item.isEntry()) {
    LdapEntry entry = item.getEntry();
  } else if (item.isException()) {
    break;
  }
}
factory.close();