10 Replies Latest reply on Dec 6, 2002 6:59 PM by sieroka

    Reset client InitialContext after server restart

    sieroka

      We have a Java client application running against JBoss. In order to avoid the situation we are in now, we retry lookup failures and reload the InitialContext if that fails. This used to work with another AppServer, but with JBoss, anything looks up with the new InitialContext is null, and therefore you get a NullPointerException on create. Below is some sample code. We are trying to look up a remote session bean after the server is shutdown and restarted. We have to restart the client application for the connections to fucntion properly again.

      Hashtable lHashtable = new Hashtable();
      lHashtable.put("java.naming.factory.initial", initial);
      lHashtable.put("java.naming.factory.url.pkgs", naming);
      lHashtable.put("java.naming.provider.url", url);


      lInitialContext = new InitialContext( lHashtable );

      lHomeObject = lInitialContext.lookup( pLookupName );

      Object obj = ResourceLoader.lookup( pHashtable, "AuditLogCRUD" );
      auditLogCRUDHome = (AuditLogCRUDHome)PortableRemoteObject.narrow( obj, AuditLogCRUDHome.class );
      auditLogCRUD = auditLogCRUDHome.create();

      The last line throws a NPE on create...

        • 1. Re: Reset client InitialContext after server restart
          mikefinn

          So, the client lives through a cycle of JBoss, and anything looked with a InitialContext fails, even if the InitialContext is new?

          In your source, it looks like you do 2 JNDI lookups - one of pLookupName, and one (I suspect) buried in your ResourceLoader class of AuditLogCRUD. Are you saying that neither works? Is the object from the lookup call actually always null?

          Also - a version of JBoss would be nice... :-)

          Mike

          • 2. Re: Reset client InitialContext after server restart
            sieroka

            It's the latest, 3.0.4. We use this class for all lookups. We cache any lookups and InitialContexts. If the use of the cached ejb reference fails, we try to look it up with the existing InitialContext. If that fails, we call the following code to load the InitialContext again, and then the ejb again. This does return a different InitialContext object (I verfied this with some temporary log statements, ie. different hashcode), but then when we look up any ejb, we get the behaviour describes in the previous posting (NPE every time).

            One interesting thing. I was connecting over a VPN, and disconnected the VPN connection, reconnected, and everything worked as expected. ie. got a valid new InitialConext. It seems that only when the JBoss server is restarted that this occurs.

            try
            {
            lInitialContext = new InitialContext( pHashtable );

            lOldInitialContext = (InitialContext)sInitialContextHashMap.put( pHashtable, lInitialContext );
            if ( lOldInitialContext != null ) {
            try
            {
            lOldInitialContext.close();
            }
            catch( Exception e )
            {
            // do nothing
            }
            }
            }
            catch( Throwable e )
            {
            sLog.error( "Unable to load InitialContext" );

            • 3. Re: Reset client InitialContext after server restart
              sieroka

              Any thoughts here? Is there any way I can clear the InitialContextFactory's cache? Get it to reset somehow? Any pointers of where to look in the JBoss source code would be helpful as well, maybe I could hack it up for our use... Thanks.

              • 4. Re: Reset client InitialContext after server restart
                sgturner

                Is it a matter of time? I mean, when Jboss restarts, you got to give it some time before trying to get the new Initialcontext. I have some similar code as you. In my code I configure 2 variables. 1-the number of milli seconds to wait until client tries again and 2-the max number od tries. Seems to work OK.

                • 5. Re: Reset client InitialContext after server restart
                  sieroka

                  I've waited quite a long time. What properties are you using with your InitialContext? Are you using 3.0.4? Any other ideas?

                  • 6. Re: Reset client InitialContext after server restart
                    sgturner

                    I don't know if I tested against 3.0.4.

                    My wait interval is 3000 milliseconds with a max of 10 intervals.

                    Your post makes me think I need to do a more formal test of my code to make sure. I'll do that and post results here.

                    • 7. Re: Reset client InitialContext after server restart
                      sgturner

                      I did not do a formal test, but here is what I did. Changed my wait interval to 5000 milliseconds and change the max number of intervals to 12.

                      Then I ran my client program and it did what it was suppose to do - try and get InitialContext every 5000 milliseconds.

                      While that was happening, I started up JBoss without my Ejbs being deployed. After JBoss was up, I deployed my Ejb jar file.

                      After the Ejb jar got deployed, the client side was able to continue successfully.

                      This was using JBoss 3.2

                      • 8. Re: Reset client InitialContext after server restart
                        sieroka

                        These are the properties I use to connect. Am I missing anything?

                        Looking up InitialContext: {java.naming.provider.url=192.168.50.99, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}

                        • 9. Re: Reset client InitialContext after server restart
                          sgturner

                          Here is what I do:

                          Properties props = new Properties();
                          props.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
                          props.put("java.naming.provider.url","jnp://<your_id_address>:1099");
                          props.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
                          Context ctx = new InitialContext (props);

                          • 10. Re: Reset client InitialContext after server restart
                            sieroka

                            Has anyone tested something like this with 3.0.4? I noticed you used jnp://ip:port, where I only had ip. I tried this, but had the same results. Our client application must be restarted after the server is restarted.