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

package com.scheduler;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.connection.DataSource;
import com.connection.OIMConnection;

import oracle.iam.scheduler.api.SchedulerService;
import oracle.iam.scheduler.vo.CronTrigger;
import oracle.iam.scheduler.vo.JobDetails;
import oracle.iam.scheduler.vo.JobParameter;
import oracle.iam.scheduler.vo.Trigger;


public class UpdateScheduledJobCron {

 public static void main(String[] args){
  new UpdateScheduledJobCron().updateScheduledJobParameter();  
 }
 public HashMap getCronTriggerJobs(){
  HashMap cronJobMap = new HashMap();
  Connection con = DataSource.getConnection();
  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'";
  try {
    PreparedStatement prepStmt = con.prepareStatement(cronQuery);
      ResultSet rs = prepStmt.executeQuery();   
      while(rs.next()) {
       cronJobMap.put(rs.getString("TRIGGER_NAME"),rs.getString("CRON_EXPRESSION"));
      }
      rs.close();
               prepStmt.close();   
     } catch (SQLException ex) {
      ex.printStackTrace();
     } finally {
      DataSource.closeConnection();
         }
  return  cronJobMap;
  
 }
 public void updateScheduledJobParameter() {    
     SchedulerService schedulerService = null;
     HashMap<String,String> cronMap = getCronTriggerJobs();
     
     for (Map.Entry<String,String> entry : cronMap.entrySet()) {
     JobParameter jbParam = new JobParameter();
     String jobName = entry.getKey();
     String cronExpression =  entry.getValue();
     try{
         //get scheduler service
         schedulerService = OIMConnection.getConnection().getService(SchedulerService.class); 
         JobDetails jobDetail = schedulerService.getJobDetail(jobName);
         
         HashMap jobAttributes = jobDetail.getAttributes();
         System.out.println("JobDetails Object Getting Params ");
   HashMap<String, JobParameter> jobParameters = jobDetail.getParams();   
   
   if(null!=jobParameters && jobParameters.size()>0){
    Iterator itrJobParams = jobParameters.keySet().iterator();
    while (itrJobParams.hasNext()) {
     String hashKey=itrJobParams.next().toString();
     Object obj = jobParameters.get(hashKey);
     JobParameter jp = null;

     if (null != obj && obj instanceof JobParameter) {
      jp = (JobParameter) obj;
      Serializable val = jp.getValue();
       jbParam.setName(jp.getName());
          jbParam.setValue(jp.getValue());
          jbParam.setParameterKey(jp.getParameterKey());
          jbParam.setDataType(jp.getDataType());
          jobAttributes.put(jp.getName(), jbParam);
     }
    }
   }else{
    System.out.println("  jobParameters Null in the task Name -> "+jobDetail.getTaskName());
   }
         System.out.println( "Updated job attributes - " + jobAttributes);
   
        Trigger[] triggers= schedulerService.getTriggersOfJob(jobName);
        for(Trigger trigg : triggers ){
         schedulerService.deleteTrigger(trigg.getName());
         
        }
         JobDetails jb = new JobDetails();
   jb.setName(jobDetail.getName());
   jb.setJobScheduleType(jobDetail.getJobScheduleType());
   jb.setCronScheduleType(jobDetail.getCronScheduleType());
   jb.setRetrycount(jobDetail.getRetrycount());
   jb.setAttributes(jobAttributes);
   
   //Update Job with Job Details.
   schedulerService.updateJob(jobDetail);
    System.out.println("Job updated successfully");
   //Schedule Trigger
        schedulerService.scheduleJob(jobDetail.getName(), new CronTrigger(jobDetail.getName(),new Date(),null, cronExpression));        
        System.out.println("Job scheduled successfully");
        

     }catch(Exception e){
      System.out.println( "Exception occured while updating scheduled job parameters - " + e);
     }
     }
     System.out.println("END");
 }
}

No comments:

Post a Comment

About OIM

Oracle Identity Management enables organizations to effectively manage the end - to - end life - cycle of user ide...

Popular Posts