Women in Technology

Hear us Roar



Article:
  Job Scheduling in Java
Subject:   how to start the sheduler for a second time
Date:   2007-10-31 04:03:14
From:   william_dev
|---------------------------------|
|02 01-05 10 31 10 ? 2007|
|(01-05)not worked |
|(this is my class SchedulerStarter)it start for the first minute (01)and not start for(02\03\04\05)|
(please help me)
package scheduler;


import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.compiere.Compiere;
import org.compiere.model.MProcess;
import org.compiere.model.X_C_Job_Dependency;
import org.compiere.model.X_C_Scheduled_Unit;
import org.compiere.model.X_C_Trigger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;


import com.caciopee.lpn.scheduler.listner.CustomizedJobListner;
import com.caciopee.lpn.scheduler.listner.CustomizedTriggerListener;


/**
*
*
*/
public class SchedulerStarter {

//hash map des triggers, chaque entrée a la forme suivante : | c_trigger_id | x_c_trigger bean |
private HashMap triggerHashMap = new HashMap();


//hash map des scheduled units, chaque entrée a la forme suivante : | c_scheduled_unit_id | x_c_scheduled_unit bean |
private static HashMap scheduledUnitHashMap = new HashMap();

//hash map des dependencies, chaque entrée a la forme suivante : | ad_process_id | liste des process requis pour ce process |
private HashMap dependencyHashMap = new HashMap();

//hash map des process et leurs triggers evenementiels, chaque entrée a la forme suivante : | ad_process_id | liste des triggers evenementiels requis pour ce process |
private HashMap processTriggersHashMap = new HashMap();

//hash map des jobdetails indexée par ad_process_id : | ad_process_id | objet JobDetail pour ce process |
public static HashMap processJobDetailHashMap = new HashMap();

// hash map des jobListeners indexée par ad_process_id : | ad_process_id | objet JobListener |
public static HashMap jobListenerHashMap = new HashMap();

// hash map des triggerListeners indexée par id trigger : | ad_process_id+"_"+ c_trigger_id | objet TriggerListener |
public static HashMap triggerListenerHashMap = new HashMap();

// hash map des triggers quartz relatifs à chaque trigger dans triggerHashMap : | ad_process_id+"_"+ c_trigger_id | objet quartz trigger |
public static HashMap quartzTriggerHashMap = new HashMap();

//contexte de compiere
static Properties ctx = new Properties();


private Log log = LogFactory.getLog(SchedulerStarter.class);
public static int c_scheduled_unit_id;
/**
*
*
*/


public SchedulerStarter(){


}

private void fillTriggerHashMap(){

int[] triggers = X_C_Trigger.getAllIDs(X_C_Trigger.Table_Name," isactive='Y' ");
for(int i = 0 ; i
this.triggerHashMap.put(new Integer(triggers[i]),new X_C_Trigger(this.ctx,triggers[i]));

}

}

/**
*
*
*/
public static void fillScheduledUnitHashMap(){

int[] scheduledUnits = X_C_Scheduled_Unit.getAllIDs(X_C_Scheduled_Unit.Table_Name," isactive='Y' ");
for(int i = 0 ; i
scheduledUnitHashMap.put(new Integer(scheduledUnits[i]),new X_C_Scheduled_Unit(ctx,scheduledUnits[i]));
c_scheduled_unit_id=scheduledUnits[i];
System.out.println(c_scheduled_unit_id);
}

}

/**
*
*
*/
private void fillDependencyHashMap(){

int[] dependencies = X_C_Job_Dependency.getAllIDs(X_C_Job_Dependency.Table_Name," isactive='Y' ");
ArrayList requiredJobs = null;
X_C_Job_Dependency jobDependency = null;
int concernedProcess = -2;// id du process concerné par la job dependency courante
for(int i = 0 ; i
jobDependency = new X_C_Job_Dependency(this.ctx,dependencies[i]);
concernedProcess = ((X_C_Scheduled_Unit) this.scheduledUnitHashMap.get(new Integer(jobDependency.getC_Scheduled_Unit_ID()))).getAD_Process_ID();

if (this.dependencyHashMap.containsKey(new Integer(concernedProcess))){
requiredJobs = (ArrayList) this.dependencyHashMap.get(new Integer(concernedProcess));
requiredJobs.add(new Integer(jobDependency.getAD_Process_ID()));
}else{
requiredJobs = new ArrayList();
requiredJobs.add(new Integer(jobDependency.getAD_Process_ID()));
this.dependencyHashMap.put(new Integer(concernedProcess),requiredJobs);
}

}

}

/**
*
*
*/
private void fillProcessTriggersHashMap(){

Iterator scheduledUnitIterator = this.scheduledUnitHashMap.keySet().iterator();
Integer scheduledUnitId = null;
X_C_Scheduled_Unit scheduledUnit = null;
ArrayList requiredTriggers = null;
X_C_Trigger linkedTrigger = null;

while(scheduledUnitIterator.hasNext()){

scheduledUnitId = (Integer) scheduledUnitIterator.next();
scheduledUnit = (X_C_Scheduled_Unit) this.scheduledUnitHashMap.get(scheduledUnitId);
linkedTrigger = (X_C_Trigger) this.triggerHashMap.get(new Integer(scheduledUnit.getC_Trigger_ID()));

if (this.processTriggersHashMap.containsKey(new Integer(scheduledUnit.getAD_Process_ID())) ){
requiredTriggers = (ArrayList) this.processTriggersHashMap.get(new Integer(scheduledUnit.getAD_Process_ID()));
requiredTriggers.add(linkedTrigger);
}else{
requiredTriggers = new ArrayList();
requiredTriggers.add(linkedTrigger);
this.processTriggersHashMap.put(new Integer(scheduledUnit.getAD_Process_ID()),requiredTriggers);
}

}

}

/**
*
*
*/
private void fillProcessJobDetailHashMap(){
Integer adProcessId = null;
Iterator iter = this.processTriggersHashMap.keySet().iterator();
while(iter.hasNext()){
adProcessId = (Integer) iter.next();
this.processJobDetailHashMap.put(adProcessId,this.createJobDetail(adProcessId));
}
}

/**
*
*
*/
private void fillTriggerListenerHashMap(){
Integer adProcessId = null;
X_C_Trigger trigger = null;
Iterator iter = this.processTriggersHashMap.keySet().iterator();
ArrayList triggerArrayList = null;
while(iter.hasNext()){
adProcessId = (Integer) iter.next();
triggerArrayList = (ArrayList) this.processTriggersHashMap.get(adProcessId);
for(int i=0;i trigger = (X_C_Trigger) triggerArrayList.get(i);
this.triggerListenerHashMap.put(adProcessId.toString()+"_"+trigger.getC_Trigger_ID(),this.createTriggerListener(adProcessId,trigger));
}
}

}

/**
*
*
*/
private void fillJobListenerHashMap(){

Integer processId = null;
Iterator iter = this.processTriggersHashMap.keySet().iterator();
while(iter.hasNext()){
processId = (Integer) iter.next();
this.jobListenerHashMap.put(processId,this.createJobListener(processId));
}

}

/**
*
*
*/
private void fillQuartzTriggerHashMap(){

String triggerListenerId = null;
Iterator iter = this.triggerListenerHashMap.keySet().iterator();
while(iter.hasNext()){
triggerListenerId = (String) iter.next();
this.quartzTriggerHashMap.put(triggerListenerId,this.createQuartzTrigger(triggerListenerId));
}

}

/**
*
* @param triggerListenerId
* @return
*/
private CronTrigger createQuartzTrigger(String triggerListenerId){
CronTrigger cronTrigger = null;
String processId = null;
String triggerId = null;
StringTokenizer stokenizer = new StringTokenizer(triggerListenerId,"_");
if (stokenizer.hasMoreTokens()){
processId = stokenizer.nextToken();
}
if (stokenizer.hasMoreTokens()){
triggerId = stokenizer.nextToken();
}
X_C_Trigger trigger = (X_C_Trigger) this.triggerHashMap.get(Integer.valueOf(triggerId));
String cronExpression = trigger.getCT_Seconde()+" "+trigger.getCT_Minute()+" "+trigger.getCT_Houre()+
" "+trigger.getCT_Day_Of_Month()+" "+trigger.getCT_Month()+" "+trigger.getCT_Day_Of_Week()+
" "+trigger.getCT_Year();

System.out.println(trigger.getCT_Seconde()+" "+trigger.getCT_Minute()+" "+trigger.getCT_Houre()+
" "+trigger.getCT_Day_Of_Month()+" "+trigger.getCT_Month()+" "+trigger.getCT_Day_Of_Week()+
" "+trigger.getCT_Year());
try {
cronTrigger = new CronTrigger(triggerListenerId,"group1",cronExpression);
cronTrigger.addTriggerListener(((CustomizedTriggerListener)this.triggerListenerHashMap.get(triggerListenerId)).getName());
System.out.println(new CronTrigger(triggerListenerId,"group1",cronExpression));
System.out.println();

} catch (ParseException e) {
e.printStackTrace();
}

//triggerListenerId=null;
return cronTrigger;
}
/**
*
* @param processId
* @return
*/
private CustomizedJobListner createJobListener(Integer processId){
CustomizedJobListner customizedJobListner = new CustomizedJobListner();
customizedJobListner.setName("process_"+processId.intValue());
return customizedJobListner;
}

/**
*
* @param triggerId
* @return
*/
private CustomizedTriggerListener createTriggerListener(Integer processId,X_C_Trigger triggerId){
CustomizedTriggerListener customizedTriggerListener = new CustomizedTriggerListener();
customizedTriggerListener.setName("process_"+processId+"_trigger_"+triggerId.getC_Trigger_ID());
return customizedTriggerListener;
}

/**
*
* @param adProcessId
* @return
*/
private JobDetail createJobDetail(Integer adProcessId){
JobDetail jobDetail = null;
Class clazz = null;
MProcess process = new MProcess(this.ctx,adProcessId.intValue());

try {
clazz = Class.forName(process.getClassname()!=null ? process.getClassname() : Constants.PROCESS_PACKAGE+process.getProcedureName() );
jobDetail = new JobDetail("process_"+adProcessId.intValue(), "group1", clazz);
jobDetail.getJobDataMap().put(Constants.POST_JOBDETAIL_LIST,new ArrayList());
jobDetail.getJobDataMap().put(Constants.PRE_JOBDETAIL_HASHMAP,new HashMap());
jobDetail.getJobDataMap().put(Constants.JOB_EXECUTING,new Boolean(false));
jobDetail.getJobDataMap().put(Constants.PROCESS_ID,process.getAD_Process_ID());
jobDetail.getJobDataMap().put(Constants.PROCESS_NAME,process.getName());
jobDetail.getJobDataMap().put(Constants.COMPIERE_CONTEXT,this.ctx);
jobDetail.getJobDataMap().put(Constants.LAST_EXECUTION_DATE,null);
jobDetail.addJobListener(((CustomizedJobListner)this.jobListenerHashMap.get(adProcessId)).getName());
//test
System.out.println( jobDetail = new JobDetail("process_"+adProcessId.intValue(), "group1", clazz));

} catch (ClassNotFoundException e) {
e.printStackTrace();
}

return jobDetail;
}

private void updateJobDataMap(){
JobDetail jobDetailMaster = null;
JobDetail jobDetailSlave = null;
Integer processId = null;
ArrayList processSlaveList = null;
Iterator iter = this.dependencyHashMap.keySet().iterator();
while(iter.hasNext()){

processId = (Integer) iter.next();
processSlaveList = (ArrayList) this.dependencyHashMap.get(processId);
jobDetailSlave = (JobDetail) this.processJobDetailHashMap.get(processId);
for(int i=0;i jobDetailMaster = (JobDetail) this.processJobDetailHashMap.get((Integer)processSlaveList.get(i));
((HashMap)jobDetailSlave.getJobDataMap().get(Constants.PRE_JOBDETAIL_HASHMAP)).put(jobDetailMaster,new Boolean(false));
((ArrayList)jobDetailMaster.getJobDataMap().get(Constants.POST_JOBDETAIL_LIST)).add(jobDetailSlave);
}
}
}

private void setCompiereContext(){
// préparation du contexte ctx
long today = System.currentTimeMillis();
java.sql.Date sd = new java.sql.Date(today);


Calendar cal = GregorianCalendar.getInstance();
cal.setTimeInMillis(today);
cal.set(Calendar.DATE,1);
cal.set(Calendar.MONTH,0);


this.ctx.put("#AD_Client_ID","1000000");
this.ctx.put("#AD_Client_Name","LPN");
this.ctx.put("#AD_Language","fr_FR");
this.ctx.put("#AD_Org_ID","1000000");
this.ctx.put("#AD_Org_Name","LPN siège");
this.ctx.put("#AD_PrintColor_ID","100");
this.ctx.put("#AD_PrintFont_ID","130");
this.ctx.put("#AD_PrintPaper_ID","100");
this.ctx.put("#AD_PrintTableFormat_ID","100");
this.ctx.put("#AD_Role_ID","1000000");
this.ctx.put("#AD_Role_Name","Administrateur LPN");
this.ctx.put("#AD_Session_ID","");
this.ctx.put("#AD_User_ID","1000000");
this.ctx.put("#AD_User_Name","");
this.ctx.put("#Date",sd.toString());
this.ctx.put("#ShowAcct","N");
this.ctx.put("#ShowTrl","Y");
this.ctx.put("#StdPrecision","2");
this.ctx.put("#SysAdmin","Y");
this.ctx.put("#User_Level","CO");
this.ctx.put("#User_Org","0,1000000");
this.ctx.put("#YYYY","Y");
this.ctx.put("#yearbeginning",new java.sql.Date(cal.getTimeInMillis()).toString());
this.ctx.put("AutoCommit","Y");
this.ctx.put("$HasAlias","Y");


}


/**
*
*
*/
public void prepareData(){

this.setCompiereContext();
this.fillTriggerHashMap();
this.fillScheduledUnitHashMap();
this.fillDependencyHashMap();
this.fillProcessTriggersHashMap();
this.fillTriggerListenerHashMap();
this.fillJobListenerHashMap();
this.fillProcessJobDetailHashMap();
this.updateJobDataMap();
this.fillQuartzTriggerHashMap();

}

public static void scheduleJobs(Scheduler sched) throws NumberFormatException, SchedulerException{
String quartzTriggerId = null;
String processId = null;
String triggerId = null;
StringTokenizer stokenizer = null;
Iterator iter = quartzTriggerHashMap.keySet().iterator();
while(iter.hasNext()){

quartzTriggerId = (String) iter.next();// quartzTriggerId est de la forme suivante : ad_process_id+"_"+c_trigger_id
stokenizer = new StringTokenizer(quartzTriggerId,"_");
if (stokenizer.hasMoreTokens()){
processId = stokenizer.nextToken();
}
if (stokenizer.hasMoreTokens()){
triggerId = stokenizer.nextToken();
}

sched.scheduleJob((JobDetail)processJobDetailHashMap.get(Integer.valueOf(processId)),(CronTrigger)quartzTriggerHashMap.get(quartzTriggerId));
sched.addTriggerListener((CustomizedTriggerListener)triggerListenerHashMap.get(quartzTriggerId));
sched.addJobListener((CustomizedJobListner)jobListenerHashMap.get(Integer.valueOf(processId)));

}

}


public static void main(String[] args) throws SchedulerException, IOException{


if(!Compiere.startupServer()){
return;
}

SchedulerStarter schedulerStarter = new SchedulerStarter();
schedulerStarter.prepareData();
//schedulerStarter.fillJobListenerHashMap();
// First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();

//scheduling jobs
schedulerStarter.scheduleJobs(sched);
System.out.println();
// All of the jobs have been added to the scheduler, but none of the
// jobs will run until the scheduler has been started
job.
sched.start();

//an infinit loop
while(true){
// if ("exit".equalsIgnoreCase(new BufferedReader( new InputStreamReader(System.in)).readLine())){
// break;
// }
}

//shuting down the scheduler
//sched.shutdown(true);




}

}