⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quartzservice.java

📁 定时器开源项目, 相对于 jcrontab, Quartz 算是更完整的一个项目, 随著开发的版本上来, 他已经脱离只是写在程序里面的计时器, 在指定的时间或区间, 处理所指定的事件. 也加入了 se
💻 JAVA
字号:
/* * Copyright James House (c) 2001-2004 *  * All rights reserved. *  * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: 1. * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. *  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *   */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;/** *  * See org/quartz/ee/jmx/jboss/doc-files/quartz-service.xml for an example * service mbean deployment descriptor. *  * @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";    }    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Interface.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    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);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -