6 Replies Latest reply on May 20, 2005 7:07 AM by umeshs79

    How to bind an MBean to JNDI, then access from within applic

    monocongo

      I am creating my first MBean and I assume that I need to bind the MBean to a JNDI name in order to do lookups in my application code to access the MBean. My MBean extends ServiceMBeanSupport. Should I have some binding code within the startService() method which will bind the MBean to a JNDI name ? Then I can lookup and use the MBean from within my code, right ?

      If this is the wrong approach or if there are better ways to go about this then any advice would be appreciated. Thanks in advance.


      -James

        • 1. Re: How to bind an MBean to JNDI, then access from within ap
          monocongo

          This seems to work OK, I can bind the bean to a JNDI name, then access the bean from a servlet which looks up the MBean using the JNDI name.


          -James

          • 2. Re: How to bind an MBean to JNDI, then access from within ap
            maroni

            Hi James,
            Can you please tell me how you bound your MBean to JNDI and how you accessed it in your client application then?
            Can you please show me some code? I was trying to do the same, but it does not work.

            Thanks a lot,
            Marion

            • 3. Re: How to bind an MBean to JNDI, then access from within ap
              monocongo

              In my MBean class, which extends ServiceMBeanSupport, I do the following in the startService() method:

               protected void startService ()
               {
               // create a HashTable of environment properties for the InitialContext
               Hashtable environment = new Hashtable();
               environment.put(Context.INITIAL_CONTEXT_FACTORY,
               "org.jnp.interfaces.NamingContextFactory");
               environment.put(Context.PROVIDER_URL, "localhost:1099");
               environment.put("java.naming.factory.url.pkgs",
               "org.jboss.naming:org.jnp.interfaces");
              
               try
               {
               // bind to JNDI
               Context context = new InitialContext(environment);
               Name fullName = context.getNameParser("").parse(m_jndiName);
               NonSerializableFactory.rebind(fullName, this, true);
               }
               catch (NamingException e)
               {
               System.out.println("Unable to bind UserManager MBean");
               }
               }
              



              I have a variable m_jndiName which I set before this method is called (right now it is hard-coded, but later I will hopefully be able to pull it from a configuration file of some sort.

              In my EJB and servlet code I just do a lookup and cast of the object at that name:


               // lookup and cast the user manager MBean
               UserManager userManager;
               try
               {
               // get the InitialContext and lookup the home interface
               Context context = new InitialContext(environment);
               userManager = (UserManager) context.lookup("mbean/UserManager");
               }
               catch (NameNotFoundException e)
               {
               // throw an UnavailableException so this servlet can never be called
               throw new UnavailableException("Lookup of UserManager MBean failed: " + e);
               }
               catch (NamingException e)
               {
               // log the error
               //m_logger.error("Lookup of the IOIManagerHome interface failed", e);
              
               // throw an UnavailableException so this servlet can never be called
               throw new UnavailableException("Lookup of UserManager MBean failed: " + e);
               }
              



              I hope this is helpful for you.


              -James

              • 4. Re: How to bind an MBean to JNDI, then access from within ap
                maroni

                Hi James,
                That´s exactly what I did, and I can see my service bound to JNDI in the JMX console, my client can fetch the service from JNDI, but then...
                The lookup is successful, no exception is thrown, but how can you invoke now methods on your mbean?? Because in my code, userManager is null. When I use the object, I get a NullPointerException.
                Can you please tell me?
                Thanks a lot,
                Marion

                • 5. Re: How to bind an MBean to JNDI, then access from within ap
                  cbuckley

                  Most people use an adaptor to access their MBean. Just like the jmx-console is an html adaptor, jboss offers other adaptors as well, including a soap adaptor. you would use a soap call to get a reference to the jmx server then you would call "invoke(new Object[]{new ObjectName("your.domain:service=YourService"), new String("yourMethod"), new Object[]{"paramValue"}, new String[]{String.class.getName()}" the "String.class.getName()" part is the class name.

                  • 6. Re: How to bind an MBean to JNDI, then access from within ap

                    Hi All,

                    I also need to do the same is this the right approach or you had find the better approach.

                    Please reply.

                    Thanks
                    Umesh