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

📄 directschedulerfactory.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.impl;import java.util.Collection;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.quartz.Scheduler;import org.quartz.SchedulerException;import org.quartz.SchedulerFactory;import org.quartz.core.JobRunShellFactory;import org.quartz.core.QuartzScheduler;import org.quartz.core.QuartzSchedulerResources;import org.quartz.core.RemotableQuartzScheduler;import org.quartz.core.SchedulingContext;import org.quartz.simpl.CascadingClassLoadHelper;import org.quartz.simpl.RAMJobStore;import org.quartz.simpl.SimpleThreadPool;import org.quartz.spi.JobStore;import org.quartz.spi.ThreadPool;/** * <p> * A singleton implementation of <code>{@link org.quartz.SchedulerFactory}</code>. * </p> *  * <p> * Here are some examples of using this class: * </p> * <p> * To create a scheduler that does not write anything to the database (is not * persistent), you can call <code>createVolatileScheduler</code>: *  * <pre> *  DirectSchedulerFactory.getInstance().createVolatileScheduler(10); // 10 threads * // don't forget to start the scheduler: DirectSchedulerFactory.getInstance().getScheduler().start(); * </pre> *  *  * <p> * Several create methods are provided for convenience. All create methods * eventually end up calling the create method with all the parameters: * </p> *  * <pre> *  public void createScheduler(String schedulerName, String schedulerInstanceId, ThreadPool threadPool, JobStore jobStore, String rmiRegistryHost, int rmiRegistryPort) * </pre> *  *  * <p> * Here is an example of using this method: * </p> *  * *  * <pre>// create the thread pool SimpleThreadPool threadPool = new SimpleThreadPool(maxThreads, Thread.NORM_PRIORITY); threadPool.initialize(); * // create the job store JobStore jobStore = new RAMJobStore(); jobStore.initialize(); *  *  DirectSchedulerFactory.getInstance().createScheduler("My Quartz Scheduler", "My Instance", threadPool, jobStore, "localhost", 1099); * // don't forget to start the scheduler: DirectSchedulerFactory.getInstance().getScheduler("My Quartz Scheduler", "My Instance").start(); * </pre> *  *  * <p> * You can also use a JDBCJobStore instead of the RAMJobStore: * </p> *  * <pre> *  DBConnectionManager.getInstance().addConnectionProvider("someDatasource", new JNDIConnectionProvider("someDatasourceJNDIName")); *  *  JDBCJobStore jdbcJobStore = new JDBCJobStore(); jdbcJobStore.setDataSource("someDatasource"); jdbcJobStore.setPostgresStyleBlobs(true); jdbcJobStore.setTablePrefix("QRTZ_"); jdbcJobStore.setInstanceId("My Instance"); jdbcJobStore.initialize(); * </pre> *  * @author Mohammad Rezaei * @author James House *  * @see JobStore * @see ThreadPool */public class DirectSchedulerFactory implements SchedulerFactory {    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Constants.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    public static final String DEFAULT_INSTANCE_ID = "SIMPLE_NON_CLUSTERED";    public static final String DEFAULT_SCHEDULER_NAME = "SimpleQuartzScheduler";    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Data members.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    private SchedulerException initException = null;    private boolean initialized = false;    private static DirectSchedulerFactory instance = new DirectSchedulerFactory();    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Constructors.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    private Log getLog() {        return LogFactory.getLog(DirectSchedulerFactory.class);    }    /**     * Constructor     */    protected DirectSchedulerFactory() {    }    /*     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     *      * Interface.     *      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     */    public static DirectSchedulerFactory getInstance() {        return instance;    }    /**     * Creates an in memory job store (<code>{@link RAMJobStore}</code>)     * The thread priority is set to Thread.NORM_PRIORITY     *      * @param maxThreads     *          The number of threads in the thread pool     * @throws SchedulerException     *           if initialization failed.     */    public void createVolatileSchduler(int maxThreads)            throws SchedulerException {        SimpleThreadPool threadPool = new SimpleThreadPool(maxThreads,                Thread.NORM_PRIORITY);        threadPool.initialize();        JobStore jobStore = new RAMJobStore();        this.createScheduler(threadPool, jobStore);    }    /**     * Creates a proxy to a remote scheduler. This scheduler can be retrieved     * via {@link DirectSchedulerFactory#getScheduler()}     *      * @param rmiHost     *          The hostname for remote scheduler     * @param rmiPort     *          Port for the remote scheduler. The default RMI port is 1099.     * @throws SchedulerException     *           if the remote scheduler could not be reached.     */    public void createRemoteScheduler(String rmiHost, int rmiPort)            throws SchedulerException {        createRemoteScheduler(DEFAULT_SCHEDULER_NAME, DEFAULT_INSTANCE_ID,                rmiHost, rmiPort);        initialized = true;    }    /**     * Same as     * {@link DirectSchedulerFactory#createRemoteScheduler(String rmiHost, int rmiPort)},     * with the addition of specifying the scheduler name and instance ID. This     * scheduler can only be retrieved via     * {@link DirectSchedulerFactory#getScheduler(java.lang.String schedulerName, java.lang.String schedulerInstanceId)}     *      * @param schedulerName     *          The name for the scheduler.     * @param schedulerInstanceId     *          The instance ID for the scheduler.     * @param rmiHost     *          The hostname for remote scheduler     * @param rmiPort     *          Port for the remote scheduler. The default RMI port is 1099.     * @throws SchedulerException     *           if the remote scheduler could not be reached.     */    protected void createRemoteScheduler(String schedulerName,            String schedulerInstanceId, String rmiHost, int rmiPort)            throws SchedulerException {        SchedulingContext schedCtxt = null;        schedCtxt = new SchedulingContext();        schedCtxt.setInstanceId(schedulerInstanceId);        RemotableQuartzScheduler rqs = null;        String uid = QuartzSchedulerResources.getUniqueIdentifier(                schedulerName, schedulerInstanceId);        RemoteScheduler remoteScheduler = new RemoteScheduler(schedCtxt, uid,                rmiHost, rmiPort);        SchedulerRepository schedRep = SchedulerRepository.getInstance();        schedRep.bind(remoteScheduler);    }    /**     * Creates a scheduler using the specified thread pool and job store. This     * scheduler can be retrieved via     * {@link DirectSchedulerFactory#getScheduler()}     *      * @param threadPool     *          The thread pool for executing jobs     * @param jobStore     *          The type of job store     * @throws SchedulerException     *           if initialization failed     */    public void createScheduler(ThreadPool threadPool, JobStore jobStore)            throws SchedulerException {        createScheduler(DEFAULT_SCHEDULER_NAME, DEFAULT_INSTANCE_ID,                threadPool, jobStore);        initialized = true;    }    /**     * Same as     * {@link DirectSchedulerFactory#createScheduler(ThreadPool threadPool, JobStore jobStore)},     * with the addition of specifying the scheduler name and instance ID. This     * scheduler can only be retrieved via     * {@link DirectSchedulerFactory#getScheduler(java.lang.String schedulerName, java.lang.String schedulerInstanceId)}     *      * @param schedulerName     *          The name for the scheduler.     * @param schedulerInstanceId     *          The instance ID for the scheduler.     * @param threadPool     *          The thread pool for executing jobs     * @param jobStore     *          The type of job store     * @throws SchedulerException     *           if initialization failed     */    public void createScheduler(String schedulerName,            String schedulerInstanceId, ThreadPool threadPool, JobStore jobStore)            throws SchedulerException {        createScheduler(schedulerName, schedulerInstanceId, threadPool,                jobStore, null, 0, -1, -1);    }    /**     * Creates a scheduler using the specified thread pool and job store and     * binds it to RMI.     *      * @param schedulerName     *          The name for the scheduler.     * @param schedulerInstanceId     *          The instance ID for the scheduler.     * @param threadPool     *          The thread pool for executing jobs     * @param jobStore     *          The type of job store     * @param rmiRegistryHost     *          The hostname to register this scheduler with for RMI. Can use     *          "null" if no RMI is required.     * @param rmiRegistryPort     *          The port for RMI. Typically 1099.     * @param idleWaitTime     *          The idle wait time in milliseconds. You can specify "-1" for     *          the default value, which is currently 30000 ms.     * @throws SchedulerException     *           if initialization failed     */    public void createScheduler(String schedulerName,            String schedulerInstanceId, ThreadPool threadPool,            JobStore jobStore, String rmiRegistryHost, int rmiRegistryPort,            long idleWaitTime, long dbFailureRetryInterval)            throws SchedulerException {        // Currently only one run-shell factory is available...        JobRunShellFactory jrsf = new StdJobRunShellFactory();        // Fire everything up        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        SchedulingContext schedCtxt = new SchedulingContext();        schedCtxt.setInstanceId(schedulerInstanceId);        QuartzSchedulerResources qrs = new QuartzSchedulerResources();        qrs.setName(schedulerName);        qrs.setInstanceId(schedulerInstanceId);        qrs.setJobRunShellFactory(jrsf);        qrs.setThreadPool(threadPool);        qrs.setJobStore(jobStore);        qrs.setRMIRegistryHost(rmiRegistryHost);        qrs.setRMIRegistryPort(rmiRegistryPort);        QuartzScheduler qs = new QuartzScheduler(qrs, schedCtxt, idleWaitTime,                dbFailureRetryInterval);        CascadingClassLoadHelper cch = new CascadingClassLoadHelper();        cch.initialize();        jobStore.initialize(cch, qs.getSchedulerSignaler());        Scheduler scheduler = new StdScheduler(qs, schedCtxt);        jrsf.initialize(scheduler, schedCtxt);        getLog().info("Quartz scheduler '" + scheduler.getSchedulerName());        getLog().info("Quartz scheduler version: " + qs.getVersion());        SchedulerRepository schedRep = SchedulerRepository.getInstance();        qs.addNoGCObject(schedRep); // prevents the repository from being        // garbage collected        schedRep.bind(scheduler);    }    /*     * public void registerSchedulerForRmi(String schedulerName, String     * schedulerId, String registryHost, int registryPort) throws     * SchedulerException, RemoteException { QuartzScheduler scheduler =     * (QuartzScheduler) this.getScheduler(); scheduler.bind(registryHost,     * registryPort); }     */    /**     * <p>     * Returns a handle to the Scheduler produced by this factory.     * </p>     *      * <p>     * you must call createRemoteScheduler or createScheduler methods before     * calling getScheduler()     * </p>     */    public Scheduler getScheduler() throws SchedulerException {        if (!initialized) { throw new SchedulerException(                "you must call createRemoteScheduler or createScheduler methods before calling getScheduler()"); }        SchedulerRepository schedRep = SchedulerRepository.getInstance();        Scheduler sched = schedRep.lookup(DEFAULT_SCHEDULER_NAME);        return sched;    }    /**     * <p>     * Returns a handle to the Scheduler with the given name, if it exists.     * </p>     */    public Scheduler getScheduler(String schedName) throws SchedulerException {        SchedulerRepository schedRep = SchedulerRepository.getInstance();        Scheduler sched = schedRep.lookup(schedName);        return sched;    }    /**     * <p>     * Returns a handle to all known Schedulers (made by any     * StdSchedulerFactory instance.).     * </p>     */    public Collection getAllSchedulers() throws SchedulerException {        return SchedulerRepository.getInstance().lookupAll();    }}

⌨️ 快捷键说明

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