问答详情
源自:1-1 初识Quartz

cron表达式

怎么和cron表达式结合一起使用?


提问者:慕村3188414 2019-05-27 11:02

个回答

  • zyang0419
    2019-06-04 13:45:23
    已采纳

    QuartzEntity quartz = new QuartzEntity();
    quartz.setJobName("monitor_addNesToES");
    quartz.setJobGroup("monitor");
    quartz.setDescription("定时向ES中更新数据");
    quartz.setJobClassName("com.jin.quartztask.job.AddNewsToEsJob");
    quartz.setCronExpression("0 0 8/1 * * ?");
    Class cls = Class.forName(quartz.getJobClassName()) ;
    cls.newInstance();
    //构建job信息
    JobDetail job = JobBuilder.newJob(cls).withIdentity(quartz.getJobName(),
          quartz.getJobGroup())
          .withDescription(quartz.getDescription()).build();
    //添加JobDataMap数据
    *//*job.getJobDataMap().put("itstyle", "欢迎你");
    job.getJobDataMap().put("blog", "https://www.xxx.com");
    job.getJobDataMap().put("data", new String[]{"张三","李四"});*//*
    // 触发时间点
    CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(quartz.getCronExpression());
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger"+quartz.getJobName(), quartz.getJobGroup())
          .startNow().withSchedule(cronScheduleBuilder).build();
    //交由Scheduler安排触发
    scheduler.scheduleJob(job, trigger);