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

📄 stdschedulerfactory.java

📁 定时器开源项目, 相对于 jcrontab, Quartz 算是更完整的一个项目, 随著开发的版本上来, 他已经脱离只是写在程序里面的计时器, 在指定的时间或区间, 处理所指定的事件. 也加入了 se
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                    + jobListenerNames[i], true);                        String listenerClass = lp.getProperty(PROP_LISTENER_CLASS, null);            if (listenerClass == null) {                initException = new SchedulerException(                        "JobListener class not specified for listener '"                                + jobListenerNames[i] + "'",                        SchedulerException.ERR_BAD_CONFIGURATION);                throw initException;            }            JobListener listener = null;            try {               listener = (JobListener) loadClass(listenerClass).newInstance();            } catch (Exception e) {                initException = new SchedulerException(                        "JobListener class '" + listenerClass                                + "' could not be instantiated.", e);                initException                        .setErrorCode(SchedulerException.ERR_BAD_CONFIGURATION);                throw initException;            }            try {                Method nameSetter = listener.getClass().getMethod("setName", strArg);                if(nameSetter != null)                    nameSetter.invoke(listener, new Object[] {jobListenerNames[i] } );                setBeanProps(listener, lp);            } catch (Exception e) {                initException = new SchedulerException(                        "JobListener '" + listenerClass                                + "' props could not be configured.", e);                initException                        .setErrorCode(SchedulerException.ERR_BAD_CONFIGURATION);                throw initException;            }            jobListeners[i] = listener;        }                       // Set up any TriggerListeners        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        String[] triggerListenerNames = cfg.getPropertyGroups(PROP_TRIGGER_LISTENER_PREFIX);        TriggerListener[] triggerListeners = new TriggerListener[triggerListenerNames.length];        for (int i = 0; i < triggerListenerNames.length; i++) {            Properties lp = cfg.getPropertyGroup(PROP_TRIGGER_LISTENER_PREFIX + "."                    + triggerListenerNames[i], true);                        String listenerClass = lp.getProperty(PROP_LISTENER_CLASS, null);            if (listenerClass == null) {                initException = new SchedulerException(                        "TriggerListener class not specified for listener '"                                + triggerListenerNames[i] + "'",                        SchedulerException.ERR_BAD_CONFIGURATION);                throw initException;            }            TriggerListener listener = null;            try {               listener = (TriggerListener) loadClass(listenerClass).newInstance();            } catch (Exception e) {                initException = new SchedulerException(                        "TriggerListener class '" + listenerClass                                + "' could not be instantiated.", e);                initException                        .setErrorCode(SchedulerException.ERR_BAD_CONFIGURATION);                throw initException;            }            try {                Method nameSetter = listener.getClass().getMethod("setName", strArg);                if(nameSetter != null)                    nameSetter.invoke(listener, new Object[] {triggerListenerNames[i] } );                setBeanProps(listener, lp);            } catch (Exception e) {                initException = new SchedulerException(                        "TriggerListener '" + listenerClass                                + "' props could not be configured.", e);                initException                        .setErrorCode(SchedulerException.ERR_BAD_CONFIGURATION);                throw initException;            }            triggerListeners[i] = listener;        }                                // Fire everything up        // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        JobRunShellFactory jrsf = null; // Create correct run-shell factory...        UserTransactionHelper userTxHelper = null;        if (wrapJobInTx)                userTxHelper = new UserTransactionHelper(userTXLocation);        if (wrapJobInTx) jrsf = new JTAJobRunShellFactory(userTxHelper);        else            jrsf = new StdJobRunShellFactory();        if (autoId) {            try {                schedInstId = DEFAULT_INSTANCE_ID;                if (js instanceof org.quartz.impl.jdbcjobstore.JobStoreSupport) {                    if(((org.quartz.impl.jdbcjobstore.JobStoreSupport) js)                         .isClustered()) {                        schedInstId = InetAddress.getLocalHost().getHostName()                            + System.currentTimeMillis();                    }                }            } catch (Exception e) {                getLog().error("Couldn't get host name!", e);                throw new IllegalStateException(                        "Cannot run without an instance id.");            }        }        if (js instanceof JobStoreSupport) {            JobStoreSupport jjs = (JobStoreSupport) js;            jjs.setInstanceId(schedInstId);            jjs.setDbRetryInterval(dbFailureRetry);        }                QuartzSchedulerResources rsrcs = new QuartzSchedulerResources();        rsrcs.setName(schedName);        rsrcs.setThreadName(threadName);                rsrcs.setInstanceId(schedInstId);        rsrcs.setJobRunShellFactory(jrsf);        if (rmiExport) {            rsrcs.setRMIRegistryHost(rmiHost);            rsrcs.setRMIRegistryPort(rmiPort);            rsrcs.setRMICreateRegistryStrategy(rmiCreateRegistry);        }        rsrcs.setThreadPool(tp);        if(tp instanceof SimpleThreadPool)          ((SimpleThreadPool)tp).setThreadNamePrefix(schedName + "_Worker");        tp.initialize();                rsrcs.setJobStore(js);        ClassLoadHelper loadHelper = null;        try {            loadHelper = (ClassLoadHelper) loadClass(classLoadHelperClass)                    .newInstance();        } catch (Exception e) {            throw new SchedulerConfigException(                    "Unable to instantiate class load helper class: "                            + e.getMessage(), e);        }        loadHelper.initialize();        schedCtxt = new SchedulingContext();        schedCtxt.setInstanceId(rsrcs.getInstanceId());        qs = new QuartzScheduler(rsrcs, schedCtxt, idleWaitTime, dbFailureRetry);        //    if(usingJSCMT)        //      qs.setSignalOnSchedulingChange(false); // TODO: fixed? (don't need        // this any more?)        schedCtxt = new SchedulingContext();        schedCtxt.setInstanceId(rsrcs.getInstanceId());        Scheduler scheduler = new StdScheduler(qs, schedCtxt);        // add plugins        for (int i = 0; i < plugins.length; i++) {            plugins[i].initialize(pluginNames[i], scheduler);            qs.addSchedulerPlugin(plugins[i]);        }                // add listeners        for (int i = 0; i < jobListeners.length; i++) {            qs.addGlobalJobListener(jobListeners[i]);        }        for (int i = 0; i < triggerListeners.length; i++) {            qs.addGlobalTriggerListener(triggerListeners[i]);        }                // set scheduler context data...        Iterator itr = schedCtxtProps.keySet().iterator();        while(itr.hasNext()) {            String key = (String) itr.next();            String val = schedCtxtProps.getProperty(key);                        scheduler.getContext().put(key, val);        }                // fire up job store, and runshell factory        js.initialize(loadHelper, qs.getSchedulerSignaler());        jrsf.initialize(scheduler, schedCtxt);        getLog().info(                "Quartz scheduler '" + scheduler.getSchedulerName()                        + "' initialized from " + propSrc);        getLog().info("Quartz scheduler version: " + qs.getVersion());        // prevents the repository from being garbage collected        qs.addNoGCObject(schedRep);        // prevents the db manager from being garbage collected        if (dbMgr != null) qs.addNoGCObject(dbMgr);        schedRep.bind(scheduler);        return scheduler;    }    private void setBeanProps(Object obj, Properties props)            throws NoSuchMethodException, IllegalAccessException,            java.lang.reflect.InvocationTargetException,            IntrospectionException, SchedulerConfigException {        props.remove("class");        BeanInfo bi = Introspector.getBeanInfo(obj.getClass());        PropertyDescriptor[] propDescs = bi.getPropertyDescriptors();        PropertiesParser pp = new PropertiesParser(props);        java.util.Enumeration keys = props.keys();        while (keys.hasMoreElements()) {            String name = (String) keys.nextElement();            String c = name.substring(0, 1).toUpperCase();            String methName = "set" + c + name.substring(1);            java.lang.reflect.Method setMeth = getSetMethod(methName, propDescs);            try {                if (setMeth == null)                        throw new NoSuchMethodException(                                "No setter for property '" + name + "'");                Class[] params = setMeth.getParameterTypes();                if (params.length != 1)                        throw new NoSuchMethodException(                                "No 1-argument setter for property '" + name                                        + "'");                if (params[0].equals(int.class)) {                    setMeth.invoke(obj, new Object[]{new Integer(pp                            .getIntProperty(name))});                } else if (params[0].equals(long.class)) {                    setMeth.invoke(obj, new Object[]{new Long(pp                            .getLongProperty(name))});                } else if (params[0].equals(float.class)) {                    setMeth.invoke(obj, new Object[]{new Float(pp                            .getFloatProperty(name))});                } else if (params[0].equals(double.class)) {                    setMeth.invoke(obj, new Object[]{new Double(pp                            .getDoubleProperty(name))});                } else if (params[0].equals(boolean.class)) {                    setMeth.invoke(obj, new Object[]{new Boolean(pp                            .getBooleanProperty(name))});                } else if (params[0].equals(String.class)) {                    setMeth.invoke(obj,                            new Object[]{pp.getStringProperty(name)});                } else                    throw new NoSuchMethodException(                            "No primitive-type setter for property '" + name                                    + "'");            } catch (NumberFormatException nfe) {                throw new SchedulerConfigException("Could not parse property '"                        + name + "' into correct data type: " + nfe.toString());            }        }    }    private java.lang.reflect.Method getSetMethod(String name,            PropertyDescriptor[] props) {        for (int i = 0; i < props.length; i++) {            java.lang.reflect.Method wMeth = props[i].getWriteMethod();            if (wMeth != null && wMeth.getName().equals(name)) return wMeth;        }        return null;    }    private Class loadClass(String className) throws ClassNotFoundException {        try {            return Thread.currentThread().getContextClassLoader().loadClass(                    className);        } catch (ClassNotFoundException e) {            return getClass().getClassLoader().loadClass(className);        }    }    private String getSchedulerName() {        return cfg.getStringProperty(PROP_SCHED_INSTANCE_NAME,                "QuartzScheduler");    }    private String getSchedulerInstId() {        return cfg.getStringProperty(PROP_SCHED_INSTANCE_ID,                DEFAULT_INSTANCE_ID);    }    /**     * <p>     * Returns a handle to the Scheduler produced by this factory.     * </p>     *      * <p>     * If one of the <code>initialize</code> methods has not be previously     * called, then the default (no-arg) <code>initialize()</code> method     * will be called by this method.     * </p>     */    public Scheduler getScheduler() throws SchedulerException {        if (cfg == null) initialize();        SchedulerRepository schedRep = SchedulerRepository.getInstance();        Scheduler sched = schedRep.lookup(getSchedulerName());        if (sched != null) {            if (sched.isShutdown()) schedRep.remove(getSchedulerName());            else                return sched;        }        sched = instantiate();        return sched;    }    /**     * <p>     * Returns a handle to the default Scheduler, creating it if it does not     * yet exist.     * </p>     *      * @see #initialize()     */    public static Scheduler getDefaultScheduler() throws SchedulerException {        StdSchedulerFactory fact = new StdSchedulerFactory();        return fact.getScheduler();    }    /**     * <p>     * Returns a handle to the Scheduler with the given name, if it exists (if     * it has already been instantiated).     * </p>     */    public Scheduler getScheduler(String schedName) throws SchedulerException {        return SchedulerRepository.getInstance().lookup(schedName);    }    /**     * <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 + -