mardi 23 juin 2015

How to finish threads in Java EE webapp when connecting to ldap

I have a Java EE webapp that does searches for personnel information (phone number, office address, e-mail, etc.) from 4 different ldap server.

When debugging the application in Eclipse on localhost (tomcat 6), I noticed that when I call my init() function (initialize the contexts and put them in a single map) 4 threads are created. This init() function is called in the doGet() of my servlet so every time a page is loaded. I'm trying to terminate those threads but it doesn't seem to work so far. Any ideas ?

This is my first webapp with Java EE and I might fully understand everything that is going on so don't hesitate to critic my code.

public static void init() throws NamingException {

    // Get references to directory contexts for each environnement
    ctxMap = new HashMap<String, DirContext>();
    ctxMap.put( LDAP1.get(DOMAIN), new InitialDirContext( getEnv( LDAP1 ) ) );
    ctxMap.put( LDAP2.get(DOMAIN), new InitialDirContext( getEnv( LDAP2 ) ) );
    ctxMap.put( LDAP3.get(DOMAIN), new InitialDirContext( getEnv( LDAP3 ) ) );
    ctxMap.put( LDAP4.get(DOMAIN), new InitialDirContext( getEnv( LDAP4 ) ) );

}
public static void close() throws NamingException{

 // Close references to directory contexts for each environnement
    ctxMap.get( LDAP1.get(DOMAIN) ).close();
    ctxMap.get( LDAP2.get(DOMAIN) ).close();
    ctxMap.get( LDAP3.get(DOMAIN) ).close();
    ctxMap.get( LDAP4.get(DOMAIN) ).close();
}

public static Hashtable<String, String> getEnv( Map<String, String> configMap ) {

    // Hashtable for environmental information
    Hashtable<String, String> env = new Hashtable<String, String>();

    // Specify which class to use for our JNDI Provider
    env.put( Context.INITIAL_CONTEXT_FACTORY, INITCTX );

    // Specify the host and port to use for directory service
    env.put( Context.PROVIDER_URL, configMap.get( HOST ) );

    // Security Information
    env.put( Context.SECURITY_AUTHENTICATION, "simple" );
    env.put( Context.SECURITY_PRINCIPAL, configMap.get( MGR_DN ) );
    env.put( Context.SECURITY_CREDENTIALS, configMap.get( MGR_PW ) );
    env.put( "java.naming.ldap.attributes.binary", "objectSID" );

    return env;
}


    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {

    try {
        Util.init();
       } catch ( Exception e ) {
        e.printStackTrace();
       }

    // Get the parameters from the vue, call the form functions, set parameters for the vue
        Operations 
        .
        .
        .


    try {
        Util.close();
    } catch ( NamingException e ) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 // Show JSP page
    this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
}

Aucun commentaire:

Enregistrer un commentaire