4 Replies Latest reply on Oct 9, 2002 5:26 AM by Marco Rovati

    Connecting to MBean from a Client

    Marco Rovati Newbie

      Hi, I have deployed a simple MBean from "JBoss Administration and Development" and it is correctly running under JBoss (I can see under jmx-console).
      I'm trying to connect from a java client. This is the code of the client:

      package it.opentur.clients;

      import javax.naming.InitialContext;
      import javax.naming.Context;
      import javax.naming.NamingException;
      import javax.rmi.PortableRemoteObject;
      import java.rmi.RemoteException;

      public class Client_1 {
      public static void main(String [] args) {
      try {
      Context jndiContext = getInitialContext();
      Object ref = jndiContext.lookup("inmemory/map/ServiceDistributor");
      catch (javax.naming.NamingException ne){ne.printStackTrace();}

      public static Context getInitialContext() throws javax.naming.NamingException {
      return new InitialContext();

      But I have this error:

      java it.opentur.clients.Client_1
      javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
      at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)
      at javax.naming.InitialContext.lookup(InitialContext.java:347)
      at it.opentur.clients.Client_1.main(Client_1.java:13)

      Anyone have an idea?

      Thank you in advance.


        • 2. Re: Connecting to MBean from a Client
          Greg Turner Apprentice

          Here is my source code I have used to connect to an MBean from a client:

          package com.tes.rmiconnector;

          import java.util.*;
          import javax.management.*;
          import javax.naming.*;
          import org.jboss.jmx.adaptor.rmi.*;
          import org.jboss.jmx.connector.*;
          import org.jboss.jmx.connector.rmi.RMIConnectorImpl;

          public class Client {

          public static void main (String[] args) throws Exception {
          try {
          // Set up jndi properties
          Properties props = new Properties();
          props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
          props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
          props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
          InitialContext ctx = new InitialContext(props);
          System.out.println ("Got context");

          // Retrieve the RMI adaptor
          Object obj = ctx.lookup("jmx:my_machine_name:rmi");
          System.out.println ("Got RMI Adapter");

          // Wrap it in a connector
          RemoteMBeanServer server = new RMIConnectorImpl((RMIAdaptor)obj);

          // Print the EJB count
          System.out.println(server.queryMBeans(new ObjectName("jboss.j2ee:service=EJB,*"), null).size());
          catch (Exception ex) {

          • 3. Re: Connecting to MBean from a Client
            Marco Rovati Newbie

            Ok, I run this code and I have this error:

            javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory. Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory

            I need to do some CLASSPATH configuration?


            • 4. Re: Connecting to MBean from a Client
              Marco Rovati Newbie

              Ok, thank you.
              Your sample run correctly.
              Now I need to connect to my MBEAN (the jndiName is ServiceDistributor) and run a method and get a property.
              How can I do this?

              Thank you in advance