skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

Cron Format value Check

Posted by Admin at 8:11 AM – 0 comments
 

this is java class file which has the function cheskCron() with return type ad boolean this function will check the CRON value with the SYSTEM value and returns true or false


import java.io.*;
import java.util.Calendar;
import java.util.StringTokenizer;



/**
 *
 * This class is to check the CRON value with the system value and return boolean 
 * which is used to scheduling of jobs.
 */
public class Cron {

 public static void main(String[] args) {
  Cron cron=new Cron();
  String cron_min = "*";
  String cron_hr = "*";
  String cron_day="#";
  String cron_mon = "*";
  String cron_week="*";
  String job[][]=new String[1000][5];
  /*try
  {
    cron_min=args[0];
      cron_hr=args[1];
    cron_day=args[2];
    cron_mon=args[3];
    cron_week=args[4];
  
  }catch(Exception e)
  {
   e.printStackTrace();
  }*/
  String cron_time = null;//= cron_min+" "+cron_hr+" "+cron_day+" "+cron_mon+" "+ cron_week;
 //reading the file job_scheduling.ini
     String record = null;
     int recCount = 0; 

    try { 

    FileReader freader     = new FileReader("job_scheduling.ini");
   BufferedReader breader = new BufferedReader(freader);

    record = new String();
   for(int li=0;li<100;li++)
   System.out.print("=");
   System.out.println("");
    while ((record = breader.readLine()) != null) {
    recCount++;
    StringTokenizer filetoken=new StringTokenizer(record,"!");
    job[recCount][0]=filetoken.nextToken(); 
    job[recCount][1]=filetoken.nextToken();
    job[recCount][2]=filetoken.nextToken();
    job[recCount][3]=filetoken.nextToken();
    job[recCount][4]="stoped";
    System.out.println(job[recCount][0]+"-----"+job[recCount][3]);
    } 
   for(int li=0;li<100;li++)
   System.out.print("=");
   System.out.println("");
    } catch (IOException e) { 
    // catch possible io errors from readLine()
   // System.out.println("Uh oh, got an IOException error!");
    e.printStackTrace();
    }



  while(true)
  {
   for(int totrec=1;totrec<=recCount;totrec++)
   {
   if(cron.checkCron(job[totrec][1]))
   {
    job[totrec][4]="started";
    System.out.println(job[totrec][0]+job[totrec][4]+"....");
   }
   if(cron.checkCron(job[totrec][2]))
   {
     job[totrec][4]="Stopped";
     System.out.println(job[totrec][0]+job[totrec][4]+"....");
   }
   }
   try {
    Thread.sleep(55000);
   } catch (InterruptedException ine) {
    
    ine.printStackTrace();
   }
  }
 }


 public boolean checkCron(String cron_time)
 {
  Calendar calendar = Calendar.getInstance();
  String cron_value[]=new String[6];
  int cron_index=0;
  StringTokenizer cron_token=new StringTokenizer(cron_time);
  String cron_min,sys_min;
  String cron_hr,sys_hr;
  String cron_day,sys_day;
  String cron_mon,sys_mon;
  String cron_week,sys_week;
  
  boolean flag_min,flag_hr,flag_day,flag_mon,flag_week;
  boolean flag_all_week,flag_all_mon;
  
 //getting the CRON time
  while(cron_token.hasMoreTokens())
  {
   cron_index++;
   cron_value[cron_index]=cron_token.nextToken();
  }
  
  //check for correct format
  if(cron_index!=5)
  {
   System.out.println("Invalud Cron Format");
  }
  
  cron_min=cron_value[1];
  if(cron_min.equals(null)) 
  {
   System.out.println("Cron Minute was empty");
   return false;
  }
  cron_hr=cron_value[2];
  if(cron_hr.equals(null)) 
  {
   System.out.println("Cron Hour was empty");
   return false;
  }
  cron_day=cron_value[3];
  if(cron_day.equals(null)) 
  {
   System.out.println("Cron Day was empty");
   return false;
  }
  cron_mon=cron_value[4];
  if(cron_mon.equals(null)) 
  {
   System.out.println("Cron Month was empty");
   return false;
  }
  cron_week=cron_value[5];
  if(cron_week.equals(null)) 
  {
   System.out.println("Cron Weekday was empty");
   return false;
  }
  
 //getting the system time
 sys_week=String.valueOf(calendar.get(Calendar.DAY_OF_WEEK)-1).toString();
 sys_mon=String.valueOf(calendar.get(Calendar.MONTH)).toString();
 sys_day=String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)).toString();
 sys_hr=String.valueOf(calendar.get(Calendar.HOUR)).toString();
 sys_min=String.valueOf(calendar.get(Calendar.MINUTE)).toString();
 //checking mins
 if(cron_min.equals("*"))
 {
  flag_min=true;
  
 }else if(check_is_avail(cron_min,sys_min))
 {
  flag_min=true;
 }
 else
 {
  flag_min=false;
 }
 //checking HRS
 if(cron_hr.equals("*"))
 {
  flag_hr=true;
  
 }else if(check_is_avail(cron_hr,sys_hr))
 {
  flag_hr=true;
 }
 else
 {
  flag_hr=false;
 }
 
 //checking day
 if(cron_day.equals("*"))
 {
  flag_day=true;
  
 }else if(check_is_avail(cron_day,sys_day))
 {
  flag_day=true;
 }
 else
 {
  flag_day=false;
 }
 //checking month
 if(cron_mon.equals("*"))
 {
  flag_mon=true;
  
 }else if(check_is_avail(cron_mon,sys_mon))
 {
  flag_mon=true;
 }
 else
 {
  flag_mon=false;
 }
 //checking week
 if(cron_week.equals("*"))
 {
  flag_week=true;
  
 }else if(check_is_avail(cron_week,sys_week))
 {
  flag_week=true;
 }
 else
 {
  flag_week=false;
 }
 if(flag_min && flag_hr && flag_day && flag_mon)
 {
  return true;
 }else if(flag_min && flag_hr && flag_week && flag_mon)
 {
  return true;
 }
 else
 {
  return false;
 }

 }
 
 private boolean check_is_avail(String cron_time,String sys_time)
 {
  String cron_token_time;
  StringTokenizer cron_token=new StringTokenizer(cron_time,",");
  while(cron_token.hasMoreTokens())
  {
     cron_token_time=cron_token.nextToken();
     if(cron_token_time.equals(sys_time))
     {
      return true;
     }
  }
  return false;
 }
 
}


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger