定时任务写法

SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        try {
            Scheduler scheduler = schedulerFactory.getScheduler();
            JobDataMap jobDataMap = new JobDataMap();
            jobDataMap.put("warning", this);
            JobDetail onlineJobDetail = JobBuilder.newJob(OnlineUserWarningJobDetail.class)
                    .usingJobData(jobDataMap)
                    .withIdentity("onlineJob", "onelineJobGroup")
                    .build();
            Trigger oldtrigger = scheduler.getTrigger(TriggerKey.triggerKey("onlineTrigger", "onlineTriggerGroup"));
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("onlineTrigger", "onlineTriggerGroup")
                    .startNow()
                    .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInMinutes(time).repeatForever())
                    .build();
            if(null != oldtrigger) {
                scheduler.rescheduleJob(TriggerKey.triggerKey("onlineTrigger", "onlineTriggerGroup"), trigger);
            }else {
                scheduler.scheduleJob(onlineJobDetail, trigger);
                //启动定时任务
                scheduler.start();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

 

posted @ 2021-04-29 15:28  oc-long  阅读(126)  评论(0编辑  收藏  举报