API Code to scheduleJob / Delete Trigger / Update Job for CRON Triggers


  1. package com.oimacademy.scheduler;
  2. import java.io.Serializable;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.Date;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. import com.oimacademy.connection.DataSource;
  12. import com.oimacademy.connection.OIMConnection;
  13. import oracle.iam.scheduler.api.SchedulerService;
  14. import oracle.iam.scheduler.vo.CronTrigger;
  15. import oracle.iam.scheduler.vo.JobDetails;
  16. import oracle.iam.scheduler.vo.JobParameter;
  17. import oracle.iam.scheduler.vo.Trigger;
  18. public class UpdateScheduledJobCron {
  19.  public static void main(String[] args){
  20.   new UpdateScheduledJobCron().updateScheduledJobParameter();  
  21.  }
  22.  public HashMap getCronTriggerJobs(){
  23.   HashMap cronJobMap = new HashMap();
  24.   Connection con = DataSource.getConnection();
  25.   String cronQuery = "select T.TRIGGER_NAME,CT.CRON_EXPRESSION from QRTZ92_CRON_TRIGGERS CT,QRTZ92_TRIGGERS T where T.TRIGGER_NAME=CT.TRIGGER_NAME AND T.TRIGGER_TYPE='CRON'";
  26.   try {
  27.     PreparedStatement prepStmt = con.prepareStatement(cronQuery);
  28.       ResultSet rs = prepStmt.executeQuery();   
  29.       while(rs.next()) {
  30.        cronJobMap.put(rs.getString("TRIGGER_NAME"),rs.getString("CRON_EXPRESSION"));
  31.       }
  32.       rs.close();
  33.                prepStmt.close();   
  34.      } catch (SQLException ex) {
  35.       ex.printStackTrace();
  36.      } finally {
  37.       DataSource.closeConnection();
  38.          }
  39.   return  cronJobMap;  
  40.  }
  41.  public void updateScheduledJobParameter() {    
  42.      SchedulerService schedulerService = null;
  43.      HashMap<String,String> cronMap = getCronTriggerJobs();     
  44.      for (Map.Entry<String,String> entry : cronMap.entrySet()) {
  45.      JobParameter jbParam = new JobParameter();
  46.      String jobName = entry.getKey();
  47.      String cronExpression =  entry.getValue();
  48.      try{
  49.          //get scheduler service
  50.          schedulerService = OIMConnection.getConnection().getService(SchedulerService.class); 
  51.          JobDetails jobDetail = schedulerService.getJobDetail(jobName);
  52.          
  53.          HashMap jobAttributes = jobDetail.getAttributes();
  54.          System.out.println("JobDetails Object Getting Params ");
  55.    HashMap<String, JobParameter> jobParameters = jobDetail.getParams();   
  56.    if(null!=jobParameters && jobParameters.size()>0){
  57.     Iterator itrJobParams = jobParameters.keySet().iterator();
  58.     while (itrJobParams.hasNext()) {
  59.      String hashKey=itrJobParams.next().toString();
  60.      Object obj = jobParameters.get(hashKey);
  61.      JobParameter jp = null;
  62.      if (null != obj && obj instanceof JobParameter) {
  63.       jp = (JobParameter) obj;
  64.       Serializable val = jp.getValue();
  65.        jbParam.setName(jp.getName());
  66.           jbParam.setValue(jp.getValue());
  67.           jbParam.setParameterKey(jp.getParameterKey());
  68.           jbParam.setDataType(jp.getDataType());
  69.           jobAttributes.put(jp.getName(), jbParam);
  70.      }
  71.     }
  72.    }else{
  73.     System.out.println("  jobParameters Null in the task Name -> "+jobDetail.getTaskName());
  74.    }
  75.          System.out.println( "Updated job attributes - " + jobAttributes);   
  76.         Trigger[] triggers= schedulerService.getTriggersOfJob(jobName);
  77.         for(Trigger trigg : triggers ){
  78.          schedulerService.deleteTrigger(trigg.getName());         
  79.         }
  80.          JobDetails jb = new JobDetails();
  81.    jb.setName(jobDetail.getName());
  82.    jb.setJobScheduleType(jobDetail.getJobScheduleType());
  83.    jb.setCronScheduleType(jobDetail.getCronScheduleType());
  84.    jb.setRetrycount(jobDetail.getRetrycount());
  85.    jb.setAttributes(jobAttributes);   
  86.    //Update Job with Job Details.
  87.    schedulerService.updateJob(jobDetail);
  88.     System.out.println("Job updated successfully");
  89.    //Schedule Trigger
  90.         schedulerService.scheduleJob(jobDetail.getName(), new CronTrigger(jobDetail.getName(),new Date(),null, cronExpression));        
  91.         System.out.println("Job scheduled successfully");    
  92.         }catch(Exception e){
  93.       System.out.println( "Exception occured while updating scheduled job parameters - " + e);
  94.      }
  95.      }
  96.      System.out.println("END");
  97.  }
  98. }

No comments:

Post a Comment