任务调度—Quartz—Cron表达式+参数传递

只需要修改触发规则

public class Test01 {
    public static void main(String[] args) throws SchedulerException {
        JobBuilder jobBuilder = JobBuilder.newJob(PlanJob.class);
        //*产生使用时的job
        JobDetail jobDetail = jobBuilder.withIdentity("working", "group01").build();

//        产生触发器时间规则, 依赖两个对象 (TriggerBuilder , Scheduled)
        TriggerBuilder<Trigger> triggerBuilder = TriggerBuilder.newTrigger();
        triggerBuilder = triggerBuilder.withIdentity("working trigger", "group01");
        triggerBuilder.startNow();//满足条件运行
//------------------------------------------------------------修改部分-------------
//        重复规则 第5秒,10秒,20秒都会触发
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("5,10,20 * * * * ? *");    
//        产生触发器
        CronTrigger cronTrigger = triggerBuilder.withSchedule(cronScheduleBuilder).build();
//        产生调度器
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        Date date = scheduler.scheduleJob(jobDetail, cronTrigger);
//-------------------------------------------------------------------------
        scheduler.start();
        System.out.println(date);
        
        scheduler.shutdown(true);
    }
}

核心对象 JobDataMap

//Test01中 准备数据
JobDataMap jobDataMap = jobDetail.getJobDataMap();
List<String> names = Arrays.asList(new String[]{"zs","le"});
jobDataMap.put("names",names);

//Jobservice中获取数据 --------------------------------------------------------------------
 List<String> names =  (List<String>)jobExecutionContext.getJobDetail().getJobDataMap().get("name");
// 获取 名 + 分组信息 
TriggerKey triggerKey = jobExecutionContext.getTrigger().getKey();
JobKey jobKey = jobExecutionContext.getJobDetail().getKey();

你可能感兴趣的:(Quartz)