加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Quartz 多个触发器

发布时间:2020-12-15 03:17:42 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 生成静态html文件,7点到19点每5分钟生成一次,其他时间1小时生成一次。 import static org.quartz.JobBuilder.newJob;import static org.quartz.Trig

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

生成静态html文件,7点到19点每5分钟生成一次,其他时间1小时生成一次。
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.CronScheduleBuilder.cronSchedule;
 
import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
 
import org.nutz.ioc.Ioc;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
 
import com.f139.frame.pojo.factory.Template;
 
 
public class CreateJob {
 
    private static SchedulerFactory sf = new StdSchedulerFactory();
 
    public static void createTemplateJob(Map<Integer,Template> map,Ioc ioc) {
        Scheduler sched;
        try {
            sched = sf.getScheduler();
 
            // ioc参数,将ioc传递到job中
            Map<String,Object> params = new HashMap<String,Object>();
            params.put("ioc",ioc);
            // 获取所有模板
            Collection<Template> templates = map.values();
            for (Template template : templates) {
                if (template.getIntervalTime() > 0) {
                    // 将当前模板ID传入job中
                    params.put("templateID",template.getTemplateID());
                    // 创建作业
                    JobDetail jobDetail = newJob(TemplateJob.class).withIdentity(new JobKey("templateJob_" + template.getTemplateID(),"template")).usingJobData(
                            new JobDataMap(params)).build();
                    // 创建触发器,并将触发器加入到作业中
                    sched.scheduleJob(jobDetail,newTrigger().withIdentity(new TriggerKey("between7and19_" + template.getTemplateID(),"template")).withSchedule(
                            cronSchedule("0 0/1 7-19 * * ?")).forJob(jobDetail).build());
                    sched.scheduleJob(newTrigger().withIdentity(new TriggerKey("between0and7_" + template.getTemplateID(),"template")).withSchedule(
                            cronSchedule("0 0/5 0-7 * * ?")).forJob(jobDetail).build());
                    sched.scheduleJob(newTrigger().withIdentity(new TriggerKey("between19and23_" + template.getTemplateID(),"template")).withSchedule(
                            cronSchedule("0 0/5 19-23 * * ?")).forJob(jobDetail).build());
                }
            }
            sched.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
 
    }
}

job处理类????

import java.util.Map;
 
import org.nutz.dao.Dao;
import org.nutz.ioc.Ioc;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
 
import com.f139.frame.freemarker.FreemarkerUtile;
import com.f139.frame.pojo.factory.Log;
import com.f139.frame.pojo.factory.Template;
import com.f139.frame.system.LocalCache;
import com.f139.frame.util.DateUtil;
 
 
public class TemplateJob implements Job {
 
    private Dao dao = null;
    private Ioc ioc = null;
 
    @Override
    @SuppressWarnings("unchecked")
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Map<String,Object> params = null;
        Template template = null;
        FreemarkerUtile freemarkerUtile = null;
        try {
            // 获取参数
            params = context.getJobDetail().getJobDataMap();
            // 获取ioc
            ioc = (Ioc) params.get("ioc");
 
                    // 获取Dao
            dao = ioc.get(NutDao.class,"dao");
 
            // 获取当前模板
            template = LocalCache.selectTemplateByID.get(Integer.parseInt(params.get("templateID").toString()));
            // 获取FreemarkerUtile
            freemarkerUtile = ioc.get(FreemarkerUtile.class,"freemarkerUtile");
            // 创建文件
            freemarkerUtile.createHtml(template.getTemplateContent(),template.getFileUrl(),null);
 
        } catch (Exception e) {
            FailLog("模板" + template.getTemplateName() + "在" + DateUtil.getNowString() + "生成静态文件时发生异常!");
        }
 
    }
 
    public void FailLog(String message) {
        Log log = new Log();
        log.setUserName("admin");
        log.setLogClass("html");
        log.setLogLevel("9");
        log.setLogMessage(message);
        log.setUpdateTime(DateUtil.getNowString());
        dao.insert(log);
    }
 
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读