package com.acme.structure.util;


import com.acme.structure.job.SchedulerJob;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzUtility
{
    public static void createQuartzScheduler(String jobName, String jndiName, String methodName, String triggerName, String cronExpression) throws Exception
    {
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        JobDetail jd = new JobDetail(jobName, Scheduler.DEFAULT_GROUP, SchedulerJob.class);
        jd.getJobDataMap().put("jndiName", jndiName);
        jd.getJobDataMap().put("methodName", methodName);
        jd.setDurability(false);

        CronTrigger ct = new CronTrigger(triggerName, Scheduler.DEFAULT_GROUP, cronExpression);
        ct.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);

        sched.scheduleJob(jd, ct);
        sched.start();
    }

    public static void stopQuartzScheduler(String triggerName) throws Exception
    {
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        sched.unscheduleJob(triggerName, Scheduler.DEFAULT_GROUP);
    }
}
