8 Replies Latest reply on Apr 9, 2005 3:55 AM by roberto

    Shared JNDI between cluster nodes

      I have 2 nodes in a cluster (JBOSS_A and JBOSS_B) and a load balance machine using apache 2.0.x (APACHE).

      All works fine, but i have a little problem about "shared jndi".

      I try to explain it better.

      JBOSS_A and JBOSS_B deploy the same EAR (i try also the hot deploy feature).

      in my ear, i have a JSP (in the war) that bind a object to the JNDI.
      (i'm using JNDI insted JSP application scope because a ejb must retrieve it and i don't want use Stateful session bean)

      If the first user request the JSP the object is bind and if they request if is binded the response is "yes" (in the same session and so in the same node, for example in node JBOSS_A).

      Now if the same request id forward to the secondo node (JBOSS_B), JNDI
      reply that the object is not binded! instead this is just bind from the first request on node JBOSS_B.

      So seem that JNDI is not shared between JBOSS_A and JBOSS_B

      To verify it i have write a simple JSP.

      Maybe i haven't configure JBoss correctly. or is it not possible?

      Any idea?

      Thank You
      Roberto

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
      <%@ page import="java.io.*,javax.naming.*"%>
      <HTML>
      <BODY>
      <%
      System.out.println("**** hello! i'm executing on this server!");
      Object object = null;
      try
      {
      object = new InitialContext().lookup("myinformation");
      }
      catch(Exception vErr)
      {}
      if(object == null)
      {
      out.println("<br> object not bound! i bound it now");
      new InitialContext().bind("myinformation", "Hello. How are u?");
      }
      else
      out.println("<br> object found with value: " + object);
      %>
      </BODY>
      </HTML>
      



        • 1. Re: Shared JNDI between cluster nodes

          i forgot...
          i'm using JBoss AS 4.01sp1 on window XP environment

          • 2. Re: Shared JNDI between cluster nodes

             

            "roberto" wrote:

            ...
            try
            {
            object = new InitialContext().lookup("myinformation");
            }
            catch(Exception vErr)
            {}
            ...
            


            That piece of code is using the local JDNI context, not the global one. I think you want to use the global JNDI context. Assuming you're using the all configuration, you would do something like this:
            try
            {
            Properties properties = new Properties();
            properties.setProperty(javax.naming.Context.PROVIDER_URL, "localhost:1100");
            object = new InitialContext(properties).lookup("myinformation");
            }
            catch (Exception vErr)
            {}
            

            If you've changed the port number for the HAJNDI service, then adjust the localhost:xxxx property.

            • 3. Re: Shared JNDI between cluster nodes

              Thank You,
              i try this way, but in every case, how can i retrieve the port number at runtime?

              In all my project (web and ejb container) when i execute a lookup on the JNDI context, i initialize it only using new InitialContext() without using properties.

              It's possible to set as default PROVIDER_URL from configuration?
              The same application is used under JBoss, WebSphere, SAP WebAS and Weblogic.. so i can't specify a port.

              THank You

              • 4. Re: Shared JNDI between cluster nodes

                I try it and works fine.
                So the problem now is how use as defualt the HA-JNDI port when in my code (web container and ejb container.. and not in a client) i use new InitialContext() without setting properties directly.

                Maybe it is necessary change same configuration file.
                I think that the standard class InitialContext retrieve std properties value from some cfg file.. but which one?
                Thank You again.

                • 5. Re: Shared JNDI between cluster nodes

                  Try this: http://java.sun.com/products/jndi/tutorial/beyond/env/source.html

                  You can specify the provider url in jndi.properties. Make sure the file is in the client's classpath.

                  • 6. Re: Shared JNDI between cluster nodes

                    i haven't a client!
                    all my code is only a enterprise application composed by Web and EJB modules.

                    So i must configure jboss server environment to use the new provider url.

                    i try to change the jndi.properties in the server/all/conf dir (all is my server) but if i do it i cannot more deploy: many exception occurs.

                    So how can i configure jboss server to use this new provider url as stardard one when SERVER code (jsp and ejb) excute the "new InitialContext()" statement?

                    Thank You

                    • 7. Re: Shared JNDI between cluster nodes

                      Ok, so let me just make sure I understand what you're doing.

                      Yo have a JSP that binds some object to JNDI by doing something like:

                      ...
                      InitialContext context = new InitialContext();
                      context.rebind("myObject", myObject);
                      ...
                      

                      Then, in your EJB, you are looking up this object, with something like:
                      ...
                      InitialContext context = new InitialContext();
                      MyClass myObject = (MyClass)context.lookup("myObject");
                      ...
                      

                      Some things to try:
                      1) Pass myObject as an argument into the EJB's business method. This would work if the JSP is invoking a method directly on the EJB during the same request.
                      2) If instead you are calling a JSP to bind this object, then making a separate request to invoke the EJB that uses the bound object, then try storing your object in the HttpSession in the first JSP, look it up in the session from the second JSP, and then pass it to the EJB during the business method invocation.
                      3) Create a jndi.properties and put it in the root of your EAR. Your EJB should prefer that jndi.properties over the system jndi.properties.

                      It sounds like your using JNDI as a way to pass arguments from your JSP to your EJB. I'm sure you have a good reason for doing this, but if I were you, I would redesign the JSPs/EJBs to use a more conventional means of passing arguments (i.e. suggestion 1 or 2).

                      • 8. Re: Shared JNDI between cluster nodes

                        Ty Andy.

                        i will try using a jndi.properties in my ear.

                        I'm not able to pass a new argument in the business method, because i need to pass this to all methods.. and i have about 300 Entity and over 500 session bean... and so too much methods...

                        ty very much