API Code to Update Scheduled Job Parameter & Schedule Job Using Cron trigger


  1. package com.oimacademy.scheduler;
  2. import java.io.Serializable;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Date;
  6. import com.oimacademy.connection.Platform;
  7. import oracle.iam.scheduler.api.SchedulerService;
  8. import oracle.iam.scheduler.vo.CronTrigger;
  9. import oracle.iam.scheduler.vo.JobDetails;
  10. import oracle.iam.scheduler.vo.JobParameter;
  11. import oracle.iam.scheduler.vo.Trigger;
  12. public class UpdateScheduledJobParameter {
  13.  public static void main(String[] args){
  14.   new UpdateScheduledJobParameter().updateScheduledJobParameter("Retry Task1", "Recurring Interval", "MONTHLY");  
  15.  }
  16.  public void updateScheduledJobParameter(String jobName, String paramName, String paramValue) {
  17.      final String logp = " :: updateScheduledJobParameter - ";
  18.      System.out.println(logp + "START");
  19.      SchedulerService schedulerService = null;
  20.      try{
  21.          //get scheduler service
  22.          schedulerService = Platform.getService(SchedulerService.class); 
  23.          JobDetails jobDetail = schedulerService.getJobDetail(jobName);         
  24.          HashMap jobAttributes = jobDetail.getAttributes();
  25.          System.out.println("JobDetails Object Getting Params ");
  26.    HashMap<String, JobParameter> jobParameters = jobDetail.getParams();   
  27.    System.out.println( "  name               : " + jobDetail.getName());
  28.    System.out.println( "  class name         : " + jobDetail.getTaskClassName());
  29.    System.out.println( "  task key           : " + jobDetail.getTaskKey());
  30.    System.out.println( "  task name          : " + jobDetail.getTaskName());
  31.    System.out.println( "  method             : " + jobDetail.getMethod());
  32.    System.out.println( "  job schedule type  : " + jobDetail.getJobScheduleType());
  33.    System.out.println( "  job listener       : " + jobDetail.getJobListener());
  34.    System.out.println( "  cron schedule type : " + jobDetail.getCronScheduleType());
  35.    System.out.println( "  retry count        : " + jobDetail.getRetrycount());
  36.    if(null!=jobParameters && jobParameters.size()>0){
  37.     Iterator itrJobParams = jobParameters.keySet().iterator();
  38.     while (itrJobParams.hasNext()) {
  39.      String hashKey=itrJobParams.next().toString();
  40.      Object obj = jobParameters.get(hashKey);
  41.      JobParameter jp = null;
  42.      if (null != obj && obj instanceof JobParameter) {
  43.       jp = (JobParameter) obj;
  44.       System.out.println( "      data type     : " + jp.getDataType());
  45.       System.out.println( "      name          : " + jp.getName());
  46.       System.out.println( "      parameter key : " + jp.getParameterKey());
  47.       Serializable val = jp.getValue();
  48.       System.out.println( "      value         : " + val);       
  49.       System.out.println();
  50.      }
  51.     }
  52.    }else{
  53.     System.out.println("  jobParameters Null in the task Name -> "+jobDetail.getTaskName());
  54.    }   
  55.          JobParameter jbParam = new JobParameter();
  56.          jbParam.setName(paramName);
  57.          jbParam.setValue(paramValue);
  58.          jobAttributes.put(paramName, jbParam);
  59.          jobDetail.setAttributes(jobAttributes);
  60.          System.out.println(logp + "Updated job attributes - " + jobAttributes);
  61.          //    
  62.         Trigger[] triggers= schedulerService.getTriggersOfJob("Retry Task1");
  63.         for(Trigger trigg : triggers ){
  64.          schedulerService.deleteTrigger(trigg.getName());         
  65.         }
  66.          JobDetails jb = new JobDetails();
  67.    jb.setName(jobDetail.getName());
  68.    jb.setJobScheduleType(jobDetail.getJobScheduleType());
  69.    jb.setCronScheduleType(jobDetail.getCronScheduleType());
  70.    jb.setRetrycount(jobDetail.getRetrycount());   
  71.    schedulerService.updateJob(jobDetail);   
  72.    schedulerService.scheduleJob(jobDetail.getName(), new CronTrigger(jobDetail.getName(),new Date(),null, "8 46 15 ? * 3,4,5,6,7,1 *"));      
  73.          System.out.println(logp + "Job updated successfully");
  74.      }catch(Exception e){
  75.       System.out.println(logp + "Exception occured while updating scheduled job parameters - " + e);
  76.      }     
  77.      System.out.println(logp + "END");
  78.  } 
  79. }

No comments:

Post a Comment