Setting classpath to be able to use Quartz libs from Application war
JBOSS_HOME\jboss-4.2.2.GA\server\default\lib\quartz-1.6.6.jar;
JBOSS_HOME\jboss-4.2.2.GA\server\default\lib\commons-dbcp-all-1.3-r699049.jar;
JBOSS_HOME\lib\custom\classes\Executor.class; ( Is discussed below )
JBOSS_HOME\jboss-4.2.2.GA\lib\custom\classes\QuartzScheduler.class; ( Is discussed below )
Note:
On completion of above steps, restart Jboss server and see that the Quartz service is deployed successfully
Working with Quartz
To simplify the pain in creating and manage the jobs within Quartz, I have created a class that manages all the processes like adding job, deleting job, rescheduling job, pause/resume job, metadata and start/stop scheduler.
Currently this class support only CRON TRIGGERS.
Add a new job to Quartz scheduler
Function :
public String addQrtzCronJob(HashMap jobHashMap)
{
try
{
//Getting the schedular context
InitialContext ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup(((String)jobHashMap.get("dsXName")).toString());
//checking schedular shutdown status
if(scheduler.isShutdown())
{
createLog("\n"+new Date()+" {status:'unsuccess',error:'Schedular for source "+((String)jobHashMap.get("dsXName")).toString()+" already shutdown.',msg:''} ");
return "{status:'unsuccess',error:'Schedular for source "+((String)jobHashMap.get("dsXName")).toString()+" already shutdown.',msg:''}";
}
// defines job and execution class
JobDetail job = new JobDetail(((String)jobHashMap.get("jobName")).toString(), ((String)jobHashMap.get("jobGroup")).toString(), Class.forName(((String)jobHashMap.get("exeClass")).toString()) );
//Passing params to exector class
job.getJobDataMap().put("FUNCTION",((String)jobHashMap.get("funcName")).toString());
job.getJobDataMap().put("PARAMS",((String)jobHashMap.get("funcParams")).toString());
job.getJobDataMap().put("LOGPATH",(String)getQuartzCfgPath());
//Defines cron trigger with cron expression
CronTrigger trigger = new CronTrigger(((String)jobHashMap.get("trgName")).toString(), ((String)jobHashMap.get("tgrGroup")).toString(), ((String)jobHashMap.get("jobName")).toString(),
((String)jobHashMap.get("jobGroup")).toString(), ((String)jobHashMap.get("cronExpr")).toString());
//Setting mis fire instrunction for trigger
trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW);
// Adds the job to schedular
scheduler.addJob(job, true);
// Schedules the job
Date ft = scheduler.scheduleJob(trigger);
createLog("\n"+new Date()+job.getFullName() + " has been scheduled to run at: " + ft+ " and repeat based on expression: "+ trigger.getCronExpression()+" ::getQuartzLogPath()="+getQuartzLogPath());
return "{status:'success',error:'',msg:'Job added successfully'}";
}
catch(Exception e)
{
createLog("\n"+new Date()+" {status:'unsuccess',error:'"+e.toString()+"',msg:''} ");
return "{status:'unsuccess',error:'"+e.toString()+"',msg:''}";
}
}
ExeClass : The class that is to be called on execution by default it should be “custom.classes.Executor”
dsXName : The Data source name
jobName : Job name for the given job
jobGroup : Job group associated with the job, if job group is null, its takes the default job group.
trgName : Trigger name for the given job
tgrGroup : Trigger group name
cronExpr : CRON Expression to specify the time/period for job execution.
FuncName : This is the name of function which is to be called when the job executes
funcParams : Params are the extra information passed to function
Delete a job
Function:
public String deleteQrtzCronJob(String dsXName,String jobName,String jobGroup)
{
try
{
createLog("\n"+new Date()+" deleteQrtzCronJob called dsXName="+dsXName+",jobName="+jobName+",jobGroup="+jobGroup);
InitialContext ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup(dsXName);
boolean deleteJob = scheduler.deleteJob(jobName, jobGroup);
if(deleteJob)
{
createLog("\n"+new Date()+" {status:'success',error:'',msg:'Job deleted successfully.'}");
return "{status:'success',error:'',msg:'Job deleted successfully.'}";
}
else
{
createLog("\n"+new Date()+" {status:'unsuccess',error:'Job cannot be deleted',msg:''}");
return "{status:'unsuccess',error:'Job cannot be deleted',msg:''}";
}
}
catch(Exception e)
{
createLog("\n"+new Date()+" {status:'success',error:'"+e.toString()+"',msg:''} ");
return "{status:'unsuccess',error:'"+e.toString()+"',msg:''}";
}
}
Deleting a job within Quartz requires the following details.
dsXName : The Data source name
jobName : Job name to be deleted
jobGroup : The job group associated with the job to be deleted
Reschedule a job
Function :
public String reScheduleQrtzCronJob(HashMap jobHashMap)
{
try
{
//Getting the schedular context
InitialContext ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup(((String)jobHashMap.get("dsXName")).toString());
//checking schedular shutdown status
if(scheduler.isShutdown())
{
createLog("\n"+new Date()+" {status:'unsuccess',error:'Schedular for source "+((String)jobHashMap.get("dsXName")).toString()+" already shutdown.',msg:''}");
return "{status:'unsuccess',error:'Schedular for source "+((String)jobHashMap.get("dsXName")).toString()+" already shutdown.',msg:''}";
}
//Defines cron trigger with cron expression
CronTrigger trigger = new CronTrigger(((String)jobHashMap.get("trgName")).toString(), ((String)jobHashMap.get("tgrGroup")).toString(), ((String)jobHashMap.get("jobName")).toString(),
((String)jobHashMap.get("jobGroup")).toString(), ((String)jobHashMap.get("cronExpr")).toString());
//Setting mis fire instrunction for trigger
trigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_FIRE_ONCE_NOW);
// Adds the job to schedular
Date ft = scheduler.rescheduleJob(((String)jobHashMap.get("trgName")).toString(), ((String)jobHashMap.get("tgrGroup")).toString(), trigger);
createLog("\n"+new Date()+" {status:'success',error:'',msg:'Job rescheduled successfully with expression'"+((String)jobHashMap.get("cronExpr")).toString()+"} ");
return "{status:'success',error:'',msg:'Job rescheduled successfully with expression'"+((String)jobHashMap.get("cronExpr")).toString()+"}";
}
catch(Exception e)
{
createLog("\n"+new Date()+" {status:'success',error:'"+e.toString()+"',msg:''} ");
return "{status:'unsuccess',error:'"+e.toString()+"',msg:''}";
}
}
Rescheduling a job within Quartz requires the following details.
ExeClass : The class that is to be called on execution by default it should be “custom.classes.Executor”
dsXName : The Data source name
jobName : Job name for the given job
jobGroup : Job group associated with the job, if job group is null, its takes the default job group.
trgName : Trigger name for the given job
tgrGroup : Trigger group name
cronExpr : CRON Expression to specify the time/period for job execution.
Pause a job
Function :
public String pauseQrtzCronTrigger(String dsXName,String trgName,String trgGroup)
{
try
{
createLog("\n"+new Date()+" pauseQrtzCronTrigger called dsXName="+dsXName+",trgName="+trgName+",trgGroup="+trgGroup);
InitialContext ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup(dsXName);
scheduler.pauseTrigger(trgName, trgGroup);
createLog("\n"+new Date()+" {status:'success',error:'',msg:'Trigger stop/paused successfully.'}");
return "{status:'success',error:'',msg:'Trigger stop/paused successfully.'}";
}
catch(Exception e)
{
createLog("\n"+new Date()+" pauseQrtzCronTrigger exception : {status:'unsuccess',error:'"+e.toString()+"',msg:''} ");
return "{status:'unsuccess',error:'"+e.toString()+"',msg:''}";
}
}
Pause a job within Quartz requires the following details.
dsXName : The Data source name
jobName : Job name to be paused
jobGroup : The job group associated with the job to be paused
Resume a job
Function :
public String resumeQrtzCronJob(String dsXName,String jobName,String jobGroup)
{
try
{
createLog("\n"+new Date()+" resumeQrtzCronJob called dsXName="+dsXName+",jobName="+jobName+",jobGroup="+jobGroup);
InitialContext ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup(dsXName);
scheduler.resumeJob(jobName, jobGroup);
createLog("\n"+new Date()+" {status:'success',error:'',msg:'Job resumed successfully.'}");
return "{status:'success',error:'',msg:'Job resumed successfully.'}";
}
catch(Exception e)
{
createLog("\n"+new Date()+" resumeQrtzCronJob exception : {status:'unsuccess',error:'"+e.toString()+"',msg:''} ");
return "{status:'unsuccess',error:'"+e.toString()+"',msg:''}";
}
}
Resume a job within Quartz requires the following details.
dsXName : The Data source name
jobName : Job name to be resumed
jobGroup : The job group associated with the job to be resumed
Start scheduler
Function :
public String startQrtzSchedular(String dsXName)
{
try
{
createLog("\n"+new Date()+" startQrtzSchedular called dsXName="+dsXName);
InitialContext ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup(dsXName);
if(!scheduler.isShutdown())
{
createLog("\n"+new Date()+"checking shutdown status {status:'unsuccess',error:'Schedular for source "+dsXName+" already running.',msg:''}");
return "{status:'unsuccess',error:'Schedular for source "+dsXName+" already running.',msg:''}";
}
scheduler.start();
return "{status:'success',error:'',msg:'Scheduler started successfully'}";
}
catch(Exception e)
{
createLog("\n"+new Date()+" {status:'success',error:'"+e.toString()+"',msg:''} ");
return "{status:'unsuccess',error:'"+e.toString()+"',msg:''}";
}
}
Start Scheduler with Quartz requires the following details.
dsXName : The Data source name
Stop scheduler
Function :
public String stopQrtzSchedular(String dsXName)
{
try
{
createLog("\n"+new Date()+"stopQrtzSchedular called with dsXName="+dsXName);
InitialContext ctx = new InitialContext();
Scheduler scheduler = (Scheduler) ctx.lookup(dsXName);
if(scheduler.isShutdown())
{
createLog("\n"+new Date()+" {status:'unsuccess',error:'Schedular for source "+dsXName+" already shutdown.',msg:''} ");
return "{status:'unsuccess',error:'Schedular for source "+dsXName+" already shutdown.',msg:''}";
}
scheduler.shutdown(true);
return "{status:'success',error:'',msg:'Scheduler shutdown successfully'}";
}
catch(Exception e)
{
createLog("\n"+new Date()+" execption in stopQrtzSchedular {status:'success',error:'"+e.toString()+"',msg:''} ");
return "{status:'unsuccess',error:'"+e.toString()+"',msg:''}";
}
}
Stop Scheduler with Quartz requires the following details.
1. dsXName : The Data source name