4 Replies Latest reply on Oct 13, 2004 10:06 PM by adrian.brock

    JMX MBean looup returns null

    beena

      Hi,

      I am using JBoss 3.2.3. I created a JMX MBean to integrate 'Apache Quartz Scheduler' with JBoss and deployed the .sar file under 'default/deploy' dir. The MBean service starts fine while starting JBoss. I checked
      http://localhost:8080/jmx-console and I am able to see the MBean service under 'DefaultDomain'. Also I checked 'service=JNDIView' list, the MBean service is listed under 'Global JNDI Namespace'. But when I try to access the MBean from a client program using jndi lookup, the object returned is null.
      For eg:
      InitialContext ctx = new InitialContext();
      Scheduler std = (Scheduler) ctx.lookup("Quartz");
      The Scheduler instance, std is null. No exceptions are thrown. Any idea why this is happening?

      Thanks,
      Beena




        • 1. Re: JMX MBean looup returns null
          dimitris

          Do you actually bind the scheduler to JNDI in your MBean code? MBeans are not bound automatically to JNDI and from your description it is not very clear what you want to access, the scheduler or the MBean

          • 2. Re: JMX MBean lookup returns null
            beena

            Well...I am not very familiar with JMX and MBean. Basically I am trying to access the Scheduler instance.

            Here's the QuartzServiceMBean Interface
            -----------------------------------
            package org.quartz.ee.jmx.jboss;

            import org.jboss.system.ServiceMBean;

            /**
            * @author Andrew Collins
            */
            public interface QuartzServiceMBean extends ServiceMBean {

            public void setJndiName(String jndiName) throws Exception;

            public String getJndiName();

            public void setProperties(String properties);

            public void setPropertiesFile(String propertiesFile);

            }

            --------------------------------------------------------
            Heres' the QuartzService MBean code:
            ---------------------------------------------------------
            package org.quartz.ee.jmx.jboss;

            import java.io.ByteArrayInputStream;
            import java.io.ByteArrayOutputStream;
            import java.io.IOException;
            import java.io.PrintWriter;
            import java.io.StringWriter;
            import java.util.Properties;

            import javax.naming.CompositeName;
            import javax.naming.Context;
            import javax.naming.InitialContext;
            import javax.naming.Name;
            import javax.naming.NamingException;

            import org.quartz.Scheduler;
            import org.quartz.SchedulerConfigException;
            import org.quartz.impl.StdSchedulerFactory;

            import org.jboss.naming.NonSerializableFactory;
            import org.jboss.system.ServiceMBeanSupport;

            /**
            * @author Andrew Collins
            */
            public class QuartzService extends ServiceMBeanSupport implements
            QuartzServiceMBean {

            /*
            * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            *
            * Data members.
            *
            * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            */

            private Properties properties;

            private StdSchedulerFactory schedulerFactory;

            private String jndiName;

            private String propertiesFile;

            private boolean error;

            private boolean useProperties;

            private boolean usePropertiesFile;

            /*
            * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            *
            * Constructors.
            *
            * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            */

            public QuartzService() {
            // flag initialization errors
            error = false;

            // use PropertiesFile attribute
            usePropertiesFile = false;
            propertiesFile = "";

            // use Properties attribute
            useProperties = false;
            properties = new Properties();

            // default JNDI name for Scheduler
            jndiName = "Quartz";
            }

            /*
            * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            *
            * Methods.
            *
            * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            */

            public void setJndiName(String jndiName) throws Exception {
            String oldName = this.jndiName;
            this.jndiName = jndiName;

            if (super.getState() == STARTED) {
            try {
            unbind(oldName);
            } catch (NamingException ne) {
            log.error(captureStackTrace(ne));

            throw new SchedulerConfigException(
            "Failed to unbind Scheduler - ", ne);
            }

            try {
            rebind();
            } catch (NamingException ne) {
            log.error(captureStackTrace(ne));

            throw new SchedulerConfigException(
            "Failed to rebind Scheduler - ", ne);
            }
            }
            }

            public String getJndiName() {
            return jndiName;
            }

            public String getName() {
            return "QuartzService(" + jndiName + ")";
            }

            public void setProperties(String properties) {
            if (usePropertiesFile) {
            log
            .error("Must specify only one of 'Properties' or 'PropertiesFile'");

            error = true;

            return;
            }

            useProperties = true;

            try {
            ByteArrayInputStream bais = new ByteArrayInputStream(properties
            .getBytes());
            this.properties = new Properties();
            this.properties.load(bais);
            } catch (IOException ioe) {
            // should not happen
            }
            }

            public String getProperties() {
            try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            properties.store(baos, "");

            return new String(baos.toByteArray());
            } catch (IOException ioe) {
            // should not happen
            return "";
            }
            }

            public void setPropertiesFile(String propertiesFile) {
            if (useProperties) {
            log
            .error("Must specify only one of 'Properties' or 'PropertiesFile'");

            error = true;

            return;
            }

            usePropertiesFile = true;

            this.propertiesFile = propertiesFile;
            }

            public String getPropertiesFile() {
            return propertiesFile;
            }

            public void createService() throws Exception {
            log.info("Create QuartzService(" + jndiName + ")...");

            if (error) {
            log
            .error("Must specify only one of 'Properties' or 'PropertiesFile'");

            throw new Exception(
            "Must specify only one of 'Properties' or 'PropertiesFile'");
            }

            schedulerFactory = new StdSchedulerFactory();

            try {
            if (useProperties) {
            schedulerFactory.initialize(properties);
            }

            if (usePropertiesFile) {
            schedulerFactory.initialize(propertiesFile);
            }
            } catch (Exception e) {
            log.error(captureStackTrace(e));

            throw new SchedulerConfigException(
            "Failed to initialize Scheduler - ", e);
            }

            log.info("QuartzService(" + jndiName + ") created.");
            }

            public void destroyService() throws Exception {
            log.info("Destroy QuartzService(" + jndiName + ")...");

            schedulerFactory = null;

            log.info("QuartzService(" + jndiName + ") destroyed.");
            }

            public void startService() throws Exception {
            log.info("Start QuartzService(" + jndiName + ")...");

            try {
            rebind();
            } catch (NamingException ne) {
            log.error(captureStackTrace(ne));

            throw new SchedulerConfigException("Failed to rebind Scheduler - ",
            ne);
            }

            try {
            Scheduler scheduler = schedulerFactory.getScheduler();

            scheduler.start();
            } catch (Exception e) {
            log.error(captureStackTrace(e));

            throw new SchedulerConfigException("Failed to start Scheduler - ",
            e);
            }

            log.info("QuartzService(" + jndiName + ") started.");
            }

            public void stopService() throws Exception {
            log.info("Stop QuartzService(" + jndiName + ")...");

            try {
            Scheduler scheduler = schedulerFactory.getScheduler();

            scheduler.shutdown();
            } catch (Exception e) {
            log.error(captureStackTrace(e));

            throw new SchedulerConfigException(
            "Failed to shutdown Scheduler - ");
            }

            try {
            unbind(jndiName);
            } catch (NamingException ne) {
            log.error(captureStackTrace(ne));

            throw new SchedulerConfigException("Failed to unbind Scheduler - ");
            }

            log.info("QuartzService(" + jndiName + ") stopped.");
            }

            private String captureStackTrace(Throwable throwable) {
            StringWriter sw = new StringWriter();
            throwable.printStackTrace(new PrintWriter(sw, true));

            return sw.toString();
            }

            private static Context createContext(Context rootCtx, Name name)
            throws NamingException {
            Context subctx = rootCtx;

            for (int n = 0; n < name.size(); n++) {
            String atom = name.get(n);

            try {
            Object obj = subctx.lookup(atom);
            subctx = (Context) obj;
            } catch (NamingException e) {
            // No binding exists, create a subcontext
            subctx = subctx.createSubcontext(atom);
            }
            }

            return subctx;
            }

            private void rebind() throws Exception {
            InitialContext rootCtx = new InitialContext();

            // Get the parent context into which we are to bind
            Name fullName = rootCtx.getNameParser("").parse(jndiName);

            Name parentName = fullName;

            if (fullName.size() > 1) {
            parentName = fullName.getPrefix(fullName.size() - 1);
            } else {
            parentName = new CompositeName();
            }

            Context parentCtx = createContext(rootCtx, parentName);
            Name atomName = fullName.getSuffix(fullName.size() - 1);
            String atom = atomName.get(0);

            Scheduler scheduler = schedulerFactory.getScheduler();

            NonSerializableFactory.rebind(parentCtx, atom, scheduler);
            }

            private void unbind(String jndiName) throws NamingException {
            Context rootCtx = new InitialContext();

            Name fullName = rootCtx.getNameParser("").parse(jndiName);
            Name atomName = fullName.getSuffix(fullName.size() - 1);
            String atom = atomName.get(0);

            rootCtx.unbind(jndiName);
            NonSerializableFactory.unbind(atom);
            }

            }
            ------------------------------------------------------------

            Thanks,
            Beena

            • 3. Re: JMX MBean looup returns null

              Keep it in the user forums.

              This question/miscomprehension has been asked so may times.
              I believe it is also in the old FAQ forum (because I added it).
              But you will be swamped by a tonne of useless posts (like this one).

              • 4. Re: JMX MBean looup returns null

                And now it is in the new FAQ:

                http://www.jboss.org/wiki/Wiki.jsp?page=HowDoIGetRemoteAccessToMyMBean

                But that won't stop people continually posting this question. :-(