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

📄 cmsschedulemanager.java

📁 一个cms内容管理平台
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                try {
                    CmsScheduledJobInfo job = (CmsScheduledJobInfo)m_configuredJobs.get(i);
                    scheduleJob(cms, job);
                } catch (CmsSchedulerException e) {
                    // ignore this job, but keep scheduling the other jobs
                    // note: the log is has already been written
                }
            }
        }

        try {
            // start the scheduler
            m_scheduler.start();
        } catch (Exception e) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_CANNOT_START_SCHEDULER_0), e);
            // can not continue
            m_scheduler = null;
            return;
        }

        if (CmsLog.INIT.isInfoEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SCHEDULER_STARTED_0));
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SCHEDULER_CONFIG_FINISHED_0));
        }
    }

    /**
     * Adds a new job to the scheduler.<p>
     *  
     * @param cms an OpenCms context object that must have been initialized with "Admin" permissions
     * @param jobInfo the job info describing the job to schedule
     * 
     * @throws CmsRoleViolationException if the user has insufficient role permissions
     * @throws CmsSchedulerException if the job could not be scheduled for any reason
     */
    public synchronized void scheduleJob(CmsObject cms, CmsScheduledJobInfo jobInfo)
    throws CmsRoleViolationException, CmsSchedulerException {

        if (OpenCms.getRunLevel() > OpenCms.RUNLEVEL_1_CORE_OBJECT) {
            // simple unit tests will have runlevel 1 and no CmsObject
            cms.checkRole(CmsRole.SCHEDULER_MANAGER);
        }

        if ((jobInfo == null) || (jobInfo.getClassName() == null)) {
            // prevent NPE
            CmsMessageContainer message = Messages.get().container(Messages.ERR_INVALID_JOB_CONFIGURATION_0);
            LOG.error(message.key());
            // can not continue
            throw new CmsSchedulerException(message);
        }

        if (m_scheduler == null) {
            CmsMessageContainer message = Messages.get().container(Messages.ERR_NO_SCHEDULER_1, jobInfo.getJobName());
            LOG.error(message.key());
            // can not continue
            throw new CmsSchedulerException(message);
        }

        Class jobClass;
        try {
            jobClass = Class.forName(jobInfo.getClassName());
        } catch (ClassNotFoundException e) {
            // class does not exist
            CmsMessageContainer message = Messages.get().container(
                Messages.ERR_JOB_CLASS_NOT_FOUND_1,
                jobInfo.getClassName());
            LOG.error(message.key());
            throw new CmsSchedulerException(message);
        }
        if (!I_CmsScheduledJob.class.isAssignableFrom(jobClass)) {
            // class does not implement required interface
            CmsMessageContainer message = Messages.get().container(
                Messages.ERR_JOB_CLASS_BAD_INTERFACE_2,
                jobInfo.getClassName(),
                I_CmsScheduledJob.class.getName());
            LOG.error(message.key());
            throw new CmsSchedulerException(message);
        }

        String jobId = jobInfo.getId();
        boolean idCreated = false;
        if (jobId == null) {
            // generate a new job id
            CmsUUID jobUUID = new CmsUUID();
            jobId = "OpenCmsJob_".concat(jobUUID.toString());
            jobInfo.setId(jobId);
            idCreated = true;
        }

        // generate Quartz job trigger
        CronTrigger trigger = new CronTrigger(jobId, Scheduler.DEFAULT_GROUP);

        try {
            trigger.setCronExpression(jobInfo.getCronExpression());
        } catch (ParseException e) {
            if (idCreated) {
                jobInfo.setId(null);
            }
            CmsMessageContainer message = Messages.get().container(
                Messages.ERR_BAD_CRON_EXPRESSION_2,
                jobInfo.getJobName(),
                jobInfo.getCronExpression());
            LOG.error(message.key());
            // can not continue
            throw new CmsSchedulerException(message);
        }

        CmsScheduledJobInfo oldJob = null;
        if (!idCreated) {
            // this job is already scheduled, remove the currently scheduled instance and keep the id     
            // important: since the new job may have errors, it's required to make sure the old job is only unscheduled 
            // if the new job info is o.k.
            oldJob = unscheduleJob(cms, jobId);
            if (oldJob == null) {
                CmsMessageContainer message = Messages.get().container(Messages.ERR_JOB_WITH_ID_DOES_NOT_EXIST_1, jobId);
                LOG.warn(message.key());
                // can not continue
                throw new CmsSchedulerException(message);
            }
            // open the job configuration (in case it has been frozen)
            jobInfo.setFrozen(false);
        }

        // only schedule jobs when they are marked as active
        if (jobInfo.isActive()) {

            // generate Quartz job detail
            JobDetail jobDetail = new JobDetail(jobInfo.getId(), Scheduler.DEFAULT_GROUP, CmsScheduleManager.class);

            // add the trigger to the job info
            jobInfo.setTrigger(trigger);

            // now set the job data
            JobDataMap jobData = new JobDataMap();
            jobData.put(CmsScheduleManager.SCHEDULER_JOB_INFO, jobInfo);
            jobDetail.setJobDataMap(jobData);

            // finally add the job to the Quartz scheduler
            try {
                m_scheduler.scheduleJob(jobDetail, trigger);
            } catch (Exception e) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(e.getMessage(), e);
                }
                if (idCreated) {
                    jobInfo.setId(null);
                }
                CmsMessageContainer message = Messages.get().container(
                    Messages.ERR_COULD_NOT_SCHEDULE_JOB_2,
                    jobInfo.getJobName(),
                    jobInfo.getClassName());
                if (oldJob != null) {
                    // make sure an old job is re-scheduled 
                    jobDetail = new JobDetail(oldJob.getId(), Scheduler.DEFAULT_GROUP, CmsScheduleManager.class);
                    jobDetail.setJobDataMap(jobData);
                    try {
                        m_scheduler.scheduleJob(jobDetail, oldJob.getTrigger());
                        m_jobs.add(oldJob);
                    } catch (SchedulerException e2) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug(e2.getMessage(), e2);
                        }
                        // unable to re-schedule original job - not much we can do about this...
                        message = Messages.get().container(
                            Messages.ERR_COULD_NOT_RESCHEDULE_JOB_2,
                            jobInfo.getJobName(),
                            jobInfo.getClassName());
                    }
                }
                if (LOG.isWarnEnabled()) {
                    LOG.warn(message.key());
                }
                throw new CmsSchedulerException(message);
            }
        }

        // freeze the scheduled job configuration
        jobInfo.initConfiguration();

        // add the job to the list of configured jobs
        m_jobs.add(jobInfo);

        if (LOG.isInfoEnabled()) {
            LOG.info(Messages.get().getBundle().key(
                Messages.LOG_JOB_SCHEDULED_4,
                new Object[] {
                    new Integer(m_jobs.size()),
                    jobInfo.getJobName(),
                    jobInfo.getClassName(),
                    jobInfo.getContextInfo().getUserName()}));
            Date nextExecution = jobInfo.getExecutionTimeNext();
            if (nextExecution != null) {
                LOG.info(Messages.get().getBundle().key(
                    Messages.LOG_JOB_NEXT_EXECUTION_2,
                    jobInfo.getJobName(),
                    nextExecution));
            }
        }
    }

    /** 
     * Shuts down this instance of the OpenCms scheduler manager.<p>
     */
    public synchronized void shutDown() {

        m_adminCms = null;

        if (CmsLog.INIT.isInfoEnabled()) {
            CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SHUTDOWN_1, this.getClass().getName()));
        }

        if (m_scheduler != null) {
            try {
                m_scheduler.shutdown();
            } catch (SchedulerException e) {
                LOG.error(Messages.get().getBundle().key(Messages.LOG_SHUTDOWN_ERROR_0));
            }
        }

        m_scheduler = null;
    }

    /**
     * Removes a currently scheduled job from the scheduler.<p>
     *  
     * @param cms an OpenCms context object that must have been initialized with "Admin" permissions
     * @param jobId the id of the job to unschedule, obtained with <code>{@link CmsScheduledJobInfo#getId()}</code>
     * 
     * @return the <code>{@link CmsScheduledJobInfo}</code> of the sucessfully unscheduled job, 
     *      or <code>null</code> if the job could not be unscheduled
     * 
     * @throws CmsRoleViolationException if the user has insufficient role permissions
     */
    public synchronized CmsScheduledJobInfo unscheduleJob(CmsObject cms, String jobId) throws CmsRoleViolationException {

        if (OpenCms.getRunLevel() > OpenCms.RUNLEVEL_1_CORE_OBJECT) {
            // simple unit tests will have runlevel 1 and no CmsObject
            cms.checkRole(CmsRole.SCHEDULER_MANAGER);
        }

        CmsScheduledJobInfo jobInfo = null;
        if (m_jobs.size() > 0) {
            // try to remove the job from the OpenCms list of jobs
            for (int i = (m_jobs.size() - 1); i >= 0; i--) {
                CmsScheduledJobInfo job = (CmsScheduledJobInfo)m_jobs.get(i);
                if (jobId.equals(job.getId())) {
                    m_jobs.remove(i);
                    if (jobInfo != null) {
                        LOG.error(Messages.get().getBundle().key(Messages.LOG_MULTIPLE_JOBS_FOUND_1, jobId));
                    }
                    jobInfo = job;
                }
            }
        }

        if ((jobInfo != null) && jobInfo.isActive()) {
            // job currently active, remove it from the Quartz scheduler
            try {
                // try to remove the job from Quartz
                m_scheduler.unscheduleJob(jobId, Scheduler.DEFAULT_GROUP);
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_UNSCHEDULED_JOB_1, jobId));
                }
            } catch (SchedulerException e) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_UNSCHEDULING_ERROR_1, jobId));
                }
            }
        }

        return jobInfo;
    }
}

⌨️ 快捷键说明

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