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


View the original post : 
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3851353#3851353

Reply to the post : 
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=3851353


-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl
_______________________________________________
JBoss-Development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to