Search in sources :

Example 56 with TriggerKey

use of org.quartz.TriggerKey in project new-cloud by xie-summer.

the class TriggerController method resumeTrigger.

@RequestMapping(value = "/resume/{appId}/{type}/{host}/{port}")
public void resumeTrigger(@PathVariable long appId, @PathVariable int type, @PathVariable String host, @PathVariable int port) {
    Assert.isTrue(appId > 0);
    Assert.isTrue(type > 0);
    Assert.hasText(host);
    Assert.isTrue(port > 0);
    String triggerName = ObjectConvert.linkIpAndPort(host, port);
    String triggerGroup = "";
    if (type == ConstUtils.CACHE_TYPE_REDIS_CLUSTER) {
        triggerGroup = ConstUtils.REDIS_TRIGGER_GROUP + appId;
    }
    TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
    try {
        scheduler.resumeTrigger(triggerKey);
    } catch (SchedulerException e) {
        logger.error(e.getMessage(), e);
    }
    logger.info("trigger with name: {}, group: {} is resumed", port, host);
}
Also used : TriggerKey(org.quartz.TriggerKey) SchedulerException(org.quartz.SchedulerException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 57 with TriggerKey

use of org.quartz.TriggerKey in project new-cloud by xie-summer.

the class TriggerController method pauseTrigger.

@RequestMapping(value = "/pause/{appId}/{type}/{host}/{port}")
public void pauseTrigger(@PathVariable long appId, @PathVariable int type, @PathVariable String host, @PathVariable int port) {
    Assert.isTrue(appId > 0);
    Assert.isTrue(type > 0);
    Assert.hasText(host);
    Assert.isTrue(port > 0);
    String triggerName = ObjectConvert.linkIpAndPort(host, port);
    String triggerGroup = "";
    if (type == ConstUtils.CACHE_TYPE_REDIS_CLUSTER) {
        triggerGroup = ConstUtils.REDIS_TRIGGER_GROUP + appId;
    }
    TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroup);
    try {
        scheduler.pauseTrigger(triggerKey);
    } catch (SchedulerException e) {
        logger.error(e.getMessage(), e);
    }
    logger.info("trigger with name: {}, group: {} is paused", port, host);
}
Also used : TriggerKey(org.quartz.TriggerKey) SchedulerException(org.quartz.SchedulerException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 58 with TriggerKey

use of org.quartz.TriggerKey in project new-cloud by xie-summer.

the class ScheduleTriggerServiceImpl method refreshTrigger.

/**
 * 定时刷新扫描配置表
 */
@Scheduled(fixedRate = 1000 * 30)
@Override
public void refreshTrigger() {
    try {
        List<ScheduleTrigger> jobList = scheduleTriggerMapper.selectAll();
        if (null != jobList && jobList.size() > 0) {
            for (ScheduleTrigger scheduleJob : jobList) {
                String status = scheduleJob.getStatus();
                TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
                CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
                // 说明本条任务还没有添加到quartz中
                if (null == trigger) {
                    if (STATUS.equals(status)) {
                        continue;
                    }
                    JobDetail jobDetail = null;
                    try {
                        // 创建JobDetail(数据库中job_name存的任务全路径,这里就可以动态的把任务注入到JobDetail中)
                        jobDetail = JobBuilder.newJob((Class<? extends Job>) Class.forName(scheduleJob.getJobName())).withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).build();
                        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCron());
                        trigger = TriggerBuilder.newTrigger().withIdentity(scheduleJob.getJobName(), scheduleJob.getJobGroup()).withSchedule(scheduleBuilder).build();
                        scheduler.scheduleJob(jobDetail, trigger);
                    } catch (ClassNotFoundException e) {
                    }
                } else {
                    // 如果是禁用,从quartz中删除这条任务
                    if (STATUS.equals(status)) {
                        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
                        scheduler.deleteJob(jobKey);
                        continue;
                    }
                    String searchCron = scheduleJob.getCron();
                    String currentCron = trigger.getCronExpression();
                    if (!searchCron.equals(currentCron)) {
                        // 表达式调度构建器
                        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(searchCron);
                        // 按新的cronExpression表达式重新构建trigger
                        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
                        // 按新的trigger重新设置job执行
                        scheduler.rescheduleJob(triggerKey, trigger);
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error("定时任务每日刷新触发器任务异常,在ScheduleTriggerService的方法refreshTrigger中,异常信息:", e);
    }
}
Also used : ScheduleTrigger(com.monitor.model.task.ScheduleTrigger) TriggerKey(org.quartz.TriggerKey) CronTrigger(org.quartz.CronTrigger) JobDetail(org.quartz.JobDetail) CronScheduleBuilder(org.quartz.CronScheduleBuilder) JobKey(org.quartz.JobKey) Job(org.quartz.Job) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 59 with TriggerKey

use of org.quartz.TriggerKey in project new-cloud by xie-summer.

the class MachineCenterImpl method unDeployMachineMonitor.

@Override
public boolean unDeployMachineMonitor(long hostId, String ip) {
    Assert.isTrue(hostId > 0);
    Assert.hasText(ip);
    TriggerKey monitorTriggerKey = TriggerKey.triggerKey(ip, ConstUtils.MACHINE_MONITOR_TRIGGER_GROUP + hostId);
    Trigger trigger = schedulerCenter.getTrigger(monitorTriggerKey);
    if (trigger == null) {
        return true;
    }
    return schedulerCenter.unscheduleJob(monitorTriggerKey);
}
Also used : TriggerKey(org.quartz.TriggerKey) Trigger(org.quartz.Trigger)

Example 60 with TriggerKey

use of org.quartz.TriggerKey in project new-cloud by xie-summer.

the class MachineCenterImpl method deployServerCollection.

@Override
public boolean deployServerCollection(long hostId, String ip) {
    Assert.hasText(ip);
    Map<String, Object> dataMap = new HashMap<String, Object>();
    dataMap.put(ConstUtils.HOST_KEY, ip);
    JobKey jobKey = JobKey.jobKey(ConstUtils.SERVER_JOB_NAME, ConstUtils.SERVER_JOB_GROUP);
    TriggerKey triggerKey = TriggerKey.triggerKey(ip, ConstUtils.SERVER_TRIGGER_GROUP + ip);
    boolean result = schedulerCenter.deployJobByCron(jobKey, triggerKey, dataMap, ScheduleUtil.getFiveMinuteCronByHostId(hostId), false);
    return result;
}
Also used : TriggerKey(org.quartz.TriggerKey) JobKey(org.quartz.JobKey) HashMap(java.util.HashMap)

Aggregations

TriggerKey (org.quartz.TriggerKey)112 Trigger (org.quartz.Trigger)49 JobKey (org.quartz.JobKey)30 SchedulerException (org.quartz.SchedulerException)30 CronTrigger (org.quartz.CronTrigger)25 JobDetail (org.quartz.JobDetail)22 Test (org.junit.Test)18 Scheduler (org.quartz.Scheduler)18 SimpleTrigger (org.quartz.SimpleTrigger)15 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)11 ArrayList (java.util.ArrayList)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 HashMap (java.util.HashMap)9 Date (java.util.Date)6 List (java.util.List)5 CronScheduleBuilder (org.quartz.CronScheduleBuilder)5 BigDecimal (java.math.BigDecimal)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 ParseException (java.text.ParseException)4