protected void initJobState() throws Exception {
    List<String> groupNames = _persistedScheduler.getJobGroupNames();

    for (String groupName : groupNames) {
      Set<JobKey> jobkeys = _persistedScheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName));

      for (JobKey jobKey : jobkeys) {
        Trigger trigger =
            _persistedScheduler.getTrigger(new TriggerKey(jobKey.getName(), jobKey.getGroup()));

        if (trigger != null) {
          continue;
        }

        JobDetail jobDetail = _persistedScheduler.getJobDetail(jobKey);

        JobDataMap jobDataMap = jobDetail.getJobDataMap();

        Message message = getMessage(jobDataMap);

        message.put(SchedulerEngine.JOB_NAME, jobKey.getName());
        message.put(SchedulerEngine.GROUP_NAME, jobKey.getGroup());

        if (_schedulerEngineHelper != null) {
          _schedulerEngineHelper.auditSchedulerJobs(message, TriggerState.EXPIRED);
        }

        _persistedScheduler.deleteJob(jobKey);
      }
    }
  }
示例#2
0
  public List<RunTimeJobDetail> searchJobs(String groupExp, String nameExp)
      throws SchedulerException {
    Pattern groupPattern = Pattern.compile(groupExp);
    Pattern namePattern = Pattern.compile(nameExp);

    List<RunTimeJobDetail> jobs = new ArrayList<RunTimeJobDetail>();
    for (String groupName : quartzScheduler.getJobGroupNames()) {
      if (groupPattern.matcher(groupName).matches()) {
        for (JobKey jobKey : quartzScheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
          if (namePattern.matcher(jobKey.getName()).matches()) {
            jobs.add(getJobDetails(groupName, jobKey.getName()));
          }
        }
      }
    }
    return jobs;
  }
示例#3
0
 public void execute(JobExecutionContext jobContext) throws JobExecutionException {
   SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
   JobKey jobKey = jobContext.getJobDetail().getKey();
   Long id = Long.parseLong(jobKey.getName());
   Event event = eventDAO.findById(id);
   MailService.sendMail(event, getRecipients(event));
   CronScheduler.removeJob(jobKey);
 }
示例#4
0
 public List<RunTimeJobDetail> listAllJobs() throws SchedulerException {
   List<RunTimeJobDetail> jobs = new ArrayList<RunTimeJobDetail>();
   for (String groupName : quartzScheduler.getJobGroupNames()) {
     for (JobKey jobKey : quartzScheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
       jobs.add(getJobDetails(groupName, jobKey.getName()));
     }
   }
   return jobs;
 }
  protected SchedulerResponse getScheduledJob(Scheduler scheduler, JobKey jobKey) throws Exception {

    JobDetail jobDetail = scheduler.getJobDetail(jobKey);

    if (jobDetail == null) {
      return null;
    }

    SchedulerResponse schedulerResponse = new SchedulerResponse();

    JobDataMap jobDataMap = jobDetail.getJobDataMap();

    String description = jobDataMap.getString(SchedulerEngine.DESCRIPTION);

    schedulerResponse.setDescription(description);

    String destinationName = jobDataMap.getString(SchedulerEngine.DESTINATION_NAME);

    schedulerResponse.setDestinationName(destinationName);

    Message message = getMessage(jobDataMap);

    JobState jobState = getJobState(jobDataMap);

    message.put(SchedulerEngine.JOB_STATE, jobState);

    schedulerResponse.setMessage(message);

    StorageType storageType =
        StorageType.valueOf(jobDataMap.getString(SchedulerEngine.STORAGE_TYPE));

    schedulerResponse.setStorageType(storageType);

    String jobName = jobKey.getName();
    String groupName = jobKey.getGroup();

    TriggerKey triggerKey = new TriggerKey(jobName, groupName);

    Trigger trigger = scheduler.getTrigger(triggerKey);

    if (trigger == null) {
      schedulerResponse.setGroupName(groupName);
      schedulerResponse.setJobName(jobName);

      return schedulerResponse;
    }

    message.put(SchedulerEngine.END_TIME, trigger.getEndTime());
    message.put(SchedulerEngine.FINAL_FIRE_TIME, trigger.getFinalFireTime());
    message.put(SchedulerEngine.NEXT_FIRE_TIME, trigger.getNextFireTime());
    message.put(SchedulerEngine.PREVIOUS_FIRE_TIME, trigger.getPreviousFireTime());
    message.put(SchedulerEngine.START_TIME, trigger.getStartTime());

    schedulerResponse.setTrigger(new QuartzTrigger(trigger));

    return schedulerResponse;
  }
 /**
  * Delete all {@link Job}s of the group <code>persistModelName</code>
  *
  * @throws SchedulerException if there is an internal Scheduler error.
  */
 private void removeTimers(String persistModelName) {
   try {
     Set<JobKey> jobKeys = scheduler.getJobKeys(GroupMatcher.jobGroupEquals(persistModelName));
     for (JobKey jobKey : jobKeys) {
       try {
         boolean success = scheduler.deleteJob(jobKey);
         if (success) {
           logger.debug("Removed scheduled cron job for strategy '{}'", jobKey.toString());
         } else {
           logger.warn("Failed to delete cron jobs '{}'", jobKey.getName());
         }
       } catch (SchedulerException e) {
         logger.warn("Failed to delete cron jobs '{}'", jobKey.getName());
       }
     }
   } catch (SchedulerException e) {
     logger.warn("Failed to delete cron jobs of group '{}'", persistModelName);
   }
 }
示例#7
0
  @Override
  protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
    JobKey jobKey = context.getTrigger().getJobKey();

    XxlJobInfo jobInfo =
        DynamicSchedulerUtil.xxlJobInfoDao.load(jobKey.getGroup(), jobKey.getName());
    // save log
    XxlJobLog jobLog = new XxlJobLog();
    jobLog.setJobGroup(jobInfo.getJobGroup());
    jobLog.setJobName(jobInfo.getJobName());
    jobLog.setJobCron(jobInfo.getJobCron());
    jobLog.setJobDesc(jobInfo.getJobDesc());
    jobLog.setJobClass(jobInfo.getJobClass());
    DynamicSchedulerUtil.xxlJobLogDao.save(jobLog);
    logger.info(">>>>>>>>>>> xxl-job trigger start, jobId:{}", jobLog.getId());

    // trigger request
    HashMap<String, String> params = new HashMap<String, String>();
    params.put(HandlerParamEnum.TIMESTAMP.name(), String.valueOf(System.currentTimeMillis()));
    params.put(HandlerParamEnum.ACTION.name(), ActionEnum.RUN.name());

    params.put(
        HandlerParamEnum.LOG_ADDRESS.name(), XxlJobLogCallbackServer.getTrigger_log_address());
    params.put(HandlerParamEnum.LOG_ID.name(), String.valueOf(jobLog.getId()));

    params.put(HandlerParamEnum.EXECUTOR_HANDLER.name(), jobInfo.getExecutorHandler());
    params.put(HandlerParamEnum.EXECUTOR_PARAMS.name(), jobInfo.getExecutorParam());

    params.put(HandlerParamEnum.GLUE_SWITCH.name(), String.valueOf(jobInfo.getGlueSwitch()));
    params.put(HandlerParamEnum.JOB_GROUP.name(), jobInfo.getJobGroup());
    params.put(HandlerParamEnum.JOB_NAME.name(), jobInfo.getJobName());

    // failover trigger
    RemoteCallBack callback = failoverTrigger(jobInfo.getExecutorAddress(), params, jobLog);
    jobLog.setExecutorHandler(
        jobInfo.getGlueSwitch() == 0 ? jobInfo.getExecutorHandler() : "GLUE任务");
    jobLog.setExecutorParam(jobInfo.getExecutorParam());
    logger.info(
        ">>>>>>>>>>> xxl-job failoverTrigger response, jobId:{}, callback:{}",
        jobLog.getId(),
        callback);

    // update trigger info
    jobLog.setTriggerTime(new Date());
    jobLog.setTriggerStatus(callback.getStatus());
    jobLog.setTriggerMsg(callback.getMsg());
    DynamicSchedulerUtil.xxlJobLogDao.updateTriggerInfo(jobLog);

    // monitor triger
    JobMonitorHelper.monitor(jobLog.getId());

    logger.info(">>>>>>>>>>> xxl-job trigger end, jobId:{}", jobLog.getId());
  }
 private boolean fetchJob(Connection conn, JobKey jobKey)
     throws IOException, SQLException, SchedulerException {
   PreparedStatement ps = null;
   ResultSet rs = null;
   try {
     ps = conn.prepareStatement(rtp(FETCH_JOB_DETAIL_TRACK, scheduler.getSchedulerName()));
     ps.setString(1, jobKey.getGroup());
     ps.setString(2, jobKey.getName());
     rs = ps.executeQuery();
     return rs.next();
   } finally {
     closeStatement(ps);
     cleanupConnection(conn);
   }
 }
示例#9
0
  @Async
  public void runJob(JobDetails jobDetails, Scheduler scheduler)
      throws JobExecutionAlreadyRunningException, JobRestartException,
          JobInstanceAlreadyCompleteException, JobParametersInvalidException,
          SpartanPersistenceException, SchedulerException, ClassNotFoundException {
    Map<String, JobParameter> jobMapParams = new HashMap<String, JobParameter>();
    JobParameter paramMonth = new JobParameter(1l);
    jobMapParams.put("salForMonth", paramMonth);

    JobParameter paramDateTime = new JobParameter(new Date(System.currentTimeMillis()));
    jobMapParams.put("myCurrentTime", paramDateTime);

    JobParameters defaultParams = new JobParameters(jobMapParams);

    JobDataMap newJobData = new JobDataMap(jobMapParams);

    // Creating Job and link to our Job class
    Class jobClass = Class.forName(jobDetails.getBeanName());
    List<String> listOfJob = scheduler.getJobGroupNames();
    if (listOfJob.size() == 0) {
      schdeuleJob(jobClass, scheduler, jobDetails, newJobData);
    } else {
      for (String groupName : scheduler.getJobGroupNames()) {

        for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {

          String jobName = jobKey.getName();
          String jobGroup = jobKey.getGroup();

          // get job's trigger
          List<Trigger> triggers = (List<Trigger>) scheduler.getTriggersOfJob(jobKey);
          CronTrigger nextFireTime = (CronTrigger) triggers.get(0);
          if (jobDetails.getJobName().equalsIgnoreCase(jobName)
              && !jobDetails
                  .getScheduler()
                  .getSchedulerExpression()
                  .equalsIgnoreCase(nextFireTime.getCronExpression())) {
            scheduler.deleteJob(jobKey);
            schdeuleJob(jobClass, scheduler, jobDetails, newJobData);
          } else if (listOfJob.contains(jobDetails.getJobName())) {

          } else {
            schdeuleJob(jobClass, scheduler, jobDetails, newJobData);
          }
        }
      }
    }
  }
 private Object getJobExecParams(Connection conn, JobKey jobKey, String restart)
     throws IOException, SQLException, SchedulerException, ClassNotFoundException {
   PreparedStatement ps = null;
   ResultSet rs = null;
   try {
     ps = conn.prepareStatement(rtp(FETCH_JOB_DETAIL_EXEC_TRACK, scheduler.getSchedulerName()));
     ps.setString(1, jobKey.getGroup());
     ps.setString(2, jobKey.getName());
     ps.setString(3, restart);
     rs = ps.executeQuery();
     rs.next();
     return getJobDataFromBlob(rs, "JOB_DATA");
   } finally {
     closeStatement(ps);
     cleanupConnection(conn);
   }
 }
  protected void unschedule(Scheduler scheduler, JobKey jobKey) throws Exception {

    JobDetail jobDetail = scheduler.getJobDetail(jobKey);

    TriggerKey triggerKey = new TriggerKey(jobKey.getName(), jobKey.getGroup());

    if (jobDetail == null) {
      return;
    }

    unregisterMessageListener(scheduler, jobKey);

    JobDataMap jobDataMap = jobDetail.getJobDataMap();

    JobState jobState = getJobState(jobDataMap);

    Trigger trigger = scheduler.getTrigger(triggerKey);

    if (trigger == null) {
      return;
    }

    jobState.setTriggerDate(SchedulerEngine.END_TIME, new Date());
    jobState.setTriggerDate(SchedulerEngine.FINAL_FIRE_TIME, trigger.getPreviousFireTime());
    jobState.setTriggerDate(SchedulerEngine.NEXT_FIRE_TIME, null);
    jobState.setTriggerDate(SchedulerEngine.PREVIOUS_FIRE_TIME, trigger.getPreviousFireTime());
    jobState.setTriggerDate(SchedulerEngine.START_TIME, trigger.getStartTime());

    jobState.setTriggerState(TriggerState.UNSCHEDULED);

    jobState.clearExceptions();

    jobDataMap.put(SchedulerEngine.JOB_STATE, JobStateSerializeUtil.serialize(jobState));

    scheduler.unscheduleJob(triggerKey);

    scheduler.addJob(jobDetail, true);
  }
示例#12
0
 @Override
 public void execute(JobExecutionContext context) throws JobExecutionException {
   JobKey key = context.getJobDetail().getKey();
   logger.info("-----job key---" + key.getName());
   logger.info("----hello--" + new Date());
 }
示例#13
0
 private String getJobKeyAsString(final JobKey jobKey) {
   return jobKey.getName() + SchedulerServiceConstants.JOB_KEY_SEPERATOR + jobKey.getGroup();
 }
  protected SchedulerResponse getScheduledJob(Scheduler scheduler, JobKey jobKey) throws Exception {

    JobDetail jobDetail = scheduler.getJobDetail(jobKey);

    if (jobDetail == null) {
      return null;
    }

    SchedulerResponse schedulerResponse = new SchedulerResponse();

    JobDataMap jobDataMap = jobDetail.getJobDataMap();

    String description = jobDataMap.getString(SchedulerEngine.DESCRIPTION);

    schedulerResponse.setDescription(description);

    String destinationName = jobDataMap.getString(SchedulerEngine.DESTINATION_NAME);

    schedulerResponse.setDestinationName(destinationName);

    Message message = getMessage(jobDataMap);

    JobState jobState = getJobState(jobDataMap);

    message.put(SchedulerEngine.JOB_STATE, jobState);

    schedulerResponse.setMessage(message);

    StorageType storageType =
        StorageType.valueOf(jobDataMap.getString(SchedulerEngine.STORAGE_TYPE));

    schedulerResponse.setStorageType(storageType);

    String jobName = jobKey.getName();
    String groupName = jobKey.getGroup();

    TriggerKey triggerKey = new TriggerKey(jobName, groupName);

    Trigger trigger = scheduler.getTrigger(triggerKey);

    if (trigger == null) {
      schedulerResponse.setGroupName(groupName);
      schedulerResponse.setJobName(jobName);

      return schedulerResponse;
    }

    message.put(SchedulerEngine.END_TIME, trigger.getEndTime());
    message.put(SchedulerEngine.FINAL_FIRE_TIME, trigger.getFinalFireTime());
    message.put(SchedulerEngine.NEXT_FIRE_TIME, trigger.getNextFireTime());
    message.put(SchedulerEngine.PREVIOUS_FIRE_TIME, trigger.getPreviousFireTime());
    message.put(SchedulerEngine.START_TIME, trigger.getStartTime());

    if (trigger instanceof CalendarIntervalTrigger) {
      CalendarIntervalTrigger calendarIntervalTrigger = CalendarIntervalTrigger.class.cast(trigger);

      IntervalUnit intervalUnit = calendarIntervalTrigger.getRepeatIntervalUnit();

      schedulerResponse.setTrigger(
          new IntervalTrigger(
              jobName,
              groupName,
              calendarIntervalTrigger.getStartTime(),
              calendarIntervalTrigger.getEndTime(),
              calendarIntervalTrigger.getRepeatInterval(),
              TimeUnit.valueOf(intervalUnit.name())));
    } else if (trigger instanceof CronTrigger) {
      CronTrigger cronTrigger = CronTrigger.class.cast(trigger);

      schedulerResponse.setTrigger(
          new com.liferay.portal.kernel.scheduler.CronTrigger(
              jobName,
              groupName,
              cronTrigger.getStartTime(),
              cronTrigger.getEndTime(),
              cronTrigger.getCronExpression()));
    } else if (trigger instanceof SimpleTrigger) {
      SimpleTrigger simpleTrigger = SimpleTrigger.class.cast(trigger);

      schedulerResponse.setTrigger(
          new IntervalTrigger(
              jobName,
              groupName,
              simpleTrigger.getStartTime(),
              simpleTrigger.getEndTime(),
              (int) simpleTrigger.getRepeatInterval(),
              TimeUnit.MILLISECOND));
    }

    return schedulerResponse;
  }