title: LDAP Unsolicited Notification using spring
body: <p>Sorry if I don't get the exact phrases here, I'm pretty new to this area...</p>
  <p>I'm using Spring LDAP to authenticate/validate users. I want to be able to get notification sfrom LDAP once there have been changes like removing or updating users.</p>
  <p>I know I can have something like a scheduled task that will run and will check the specific users or groups i'm interested in, but I'm looking for something that gives me unsolicited notifications.</p>
  <p>I looked online and found the following:
  <a href="http://docs.oracle.com/javase/tutorial/jndi/ldap/unsol.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/tutorial/jndi/ldap/unsol.html</a>
  and this looks promising, but i don't understand how to use it, plus I don't think spring really supports it and that i'll have to use the jndi classes, like in the attached link.</p>
  <p>Also, it looks like the only notifications I will get are Notice of disconnection: <a href="https://www.rfc-editor.org/rfc/rfc4511#section-4.4" rel="nofollow noreferrer">https://www.rfc-editor.org/rfc/rfc4511#section-4.4</a> is that true?</p>
  <p>And finally, I used the sample code I found, but I didn't get any notification from my AD server, is it because I'll only get notification about disconnection, or is there a setting i need to set in AD to enable these notifications?</p>
  <p>Here is the sample code. I tried several DNs for the lookup and the <code>ctx.addNamingListener</code>, but maybe someone has a better idea for what I need to use there.</p>
  <pre><code>class RegUnsol {
  public static void main(String[] args) {
  
      // Set up environment for creating initial context
      Hashtable&lt;String, Object&gt; env = new Hashtable&lt;String, Object&gt;(11);
      env.put(Context.INITIAL_CONTEXT_FACTORY, &quot;com.sun.jndi.ldap.LdapCtxFactory&quot;);
      env.put(Context.PROVIDER_URL, &quot;ldap://MY_AD_IP&quot;);
  
      env.put(Context.SECURITY_AUTHENTICATION, &quot;simple&quot;);
      env.put(Context.SECURITY_PRINCIPAL,
              &quot;CN=Administrator,CN=Users,DC=sanity,DC=local&quot;);
      env.put(Context.SECURITY_CREDENTIALS, &quot;SOME_PASSWORD&quot;);
  
      try {
          // Get event context for registering listener
          EventContext ctx = new InitialContext(env)
                  .lookup(&quot;CN=Users,DC=sanity,DC=local&quot;);
  
          // Create listener
          NamingListener listener = new UnsolListener();
  
          // Register listener with context (all targets equivalent)
          ctx.addNamingListener(&quot;CN=Users,DC=sanity,DC=local&quot;,
                  EventContext.ONELEVEL_SCOPE, listener);
  
          // Wait 1 minutes for listener to receive events
          try {
              for (int i = 0; i &lt; 5; i++) {
                  Thread.sleep(60000);
              }
          } catch (InterruptedException e) {
              System.out.println(&quot;sleep interrupted&quot;);
          }
  
          // Not strictly necessary if we're going to close context anyhow
          ctx.removeNamingListener(listener);
  
          // Close context when we're done
          ctx.close();
  
      } catch (NamingException e) {
          e.printStackTrace();
      }
  }
  
  /**
   * A sample UnsolicitedNotificationListener.
   */
  static class UnsolListener implements UnsolicitedNotificationListener {
      @Override
      public void notificationReceived(UnsolicitedNotificationEvent evt) {
          System.out.println(&quot;received: &quot; + evt);
      }
  
      @Override
      public void namingExceptionThrown(NamingExceptionEvent evt) {
          System.out.println(&quot;&gt;&gt;&gt; UnsolListener got an exception&quot;);
          evt.getException().printStackTrace();
      }
  }
  </code></pre>
  <p>}</p>