Esempio n. 1
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);
          }
        }
      }
    }
  }
Esempio n. 2
0
 public void schdeuleJob(
     Class jobClass, Scheduler scheduler, JobDetails jobDetails, JobDataMap newJobData)
     throws SchedulerException {
   JobDetail jobdetail =
       JobBuilder.newJob(jobClass)
           .usingJobData(newJobData)
           .withIdentity(jobDetails.getJobName(), jobDetails.getJobName())
           .build();
   Trigger trigger =
       TriggerBuilder.newTrigger()
           .withIdentity(jobDetails.getJobName(), jobDetails.getJobName())
           .withSchedule(
               CronScheduleBuilder.cronSchedule(
                   "" + jobDetails.getScheduler().getSchedulerExpression() + ""))
           .build();
   scheduler.getListenerManager().addJobListener(joblistener);
   scheduler.start();
   scheduler.scheduleJob(jobdetail, trigger);
 }