欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Quartz

程序员文章站 2022-05-24 19:06:14
...
    private void scheduleTask(TagCharacterTask ulTask)
        throws ADException
    {
        Trigger taskTrigger = null;
        
        // 判断单次还循环;
        if (TaskTriggerType.Once_After.getValue().equalsIgnoreCase(ulTask.getTriggerType()))
        {   //单次指定时间执行;
            Date d = null;
            try
            {
                d = new SimpleDateFormat(DATEFORMAT).parse(ulTask.getPeriod());
            }
            catch (ParseException e)
            {   //周期格式转化错误;
                throw new ADException(ErrorCode.TAG_TASK_PERIOD_FORMAT_ERROR, 
                    ResourceCfg.getResourceMessage(String.valueOf(ErrorCode.TAG_TASK_PERIOD_FORMAT_ERROR)));
            }
            
            taskTrigger = new SimpleTrigger("tagTaskTrigger_" + ulTask.getId(), GROUP_NAME, d);
            
            log.debug("单次指定时间执行SimpleTrigger被创建!\n");
        }
        else if (TaskTriggerType.Once_Now.getValue().equalsIgnoreCase(ulTask.getTriggerType()))
        {
            //单次立即执行
            taskTrigger = new SimpleTrigger("tagTaskTrigger_" + ulTask.getId(), GROUP_NAME);
            log.debug("单次立即执行的SimpleTrigger被创建!\n");
        }
        else if (TaskTriggerType.Circle_After.getValue().equalsIgnoreCase(ulTask.getTriggerType()))
        {   //循环非立即执行;
            try
            {
                taskTrigger = new CronTrigger("tagTaskTrigger_" + ulTask.getId(), GROUP_NAME, ulTask.getPeriod());
                log.debug("循环执行的CronTrigger被创建!\n");
            }
            catch (ParseException e)
            {
                throw new ADException(ErrorCode.TAG_TASK_PERIOD_FORMAT_ERROR, 
                    ResourceCfg.getResourceMessage(String.valueOf(ErrorCode.TAG_TASK_PERIOD_FORMAT_ERROR)));
            }
        }
        /*
        else if(TaskTriggerType.Circle_Now.getValue().equalsIgnoreCase(ulTask.getTriggerType()))
        {
            //循环立即执行;
            // TODO Auto-generated method stub  
        }*/
        
        JobDetail jobDetail = new JobDetail("tagTaskTask_" + ulTask.getId(), GROUP_NAME, TagTaskJob.class);
        JobDataMap jMap = new JobDataMap();
        jMap.put("TagTaskJobData", ulTask);
        jobDetail.setJobDataMap(jMap);
        log.debug("Job " + "tagTaskTask_" + ulTask.getId() + " was set to schedule!");
        
        try
        {
            log.debug("Schedule tagCharacterTask, 加入一个任务,即将启动调度!");
            schedule.scheduleJob(jobDetail, taskTrigger);
            log.debug("Schedule tagCharacterTask, 加入一个任务,启动调度完成!");
            
            log.info("Schedule tagCharacterTask [" + ulTask.getId() + "] at "
                + DateUtil.date2StringByFormat(taskTrigger.getNextFireTime(), DateUtil.YYYY_MM_DD_HH_MM_SS));
        }
        catch (SchedulerException e)
        {
            log.error("schedule tagCharacterTask [" + ulTask.getId() + "] failed!", e);
            
            throw new ADException(ErrorCode.SYSTEM_ERROR, "schedule tagCharacterTask [" + ulTask.getId() + "] failed!");
        }
    } 
相关标签: quartz