quartz - sample JobScheduler

import java.util.Date;

import org.apache.log4j.Logger;

import org.quartz.JobDataMap;

import org.quartz.JobDetail;

import org.quartz.Scheduler;

import org.quartz.SchedulerException;

import org.quartz.SchedulerFactory;

import org.quartz.SimpleTrigger;

import org.quartz.Trigger;

 

public class OrderCancelJobScheduler

{

      SchedulerFactory schedFact;

      Scheduler sched;

      OrderManager orderManager;

     

      public void init() throws SchedulerException

      {

            logger.info(">>> Entering init()..");

            if(schedFact == null) schedFact = new org.quartz.impl.StdSchedulerFactory();

            if(sched == null) sched = schedFact.getScheduler();

            sched.start();

            logger.info("<<< Leaving init().");

      }

 

      public void cancelOrder30SecondsLater(String orderId) throws SchedulerException

      {

            logger.info(">>> Entering cancelOrder30SecondsLater( " + orderId + ")...");

            JobDetail jobDetail = new JobDetail("CancelOrder" + System.currentTimeMillis(), null, OrderCancelJob.class);

            JobDataMap jobDataMap = new JobDataMap();

            jobDataMap.put("orderId", orderId);

            jobDataMap.put("orderManager", orderManager);

            jobDetail.setJobDataMap(jobDataMap);

            Trigger trigger = getOneTimeTrigger();

            sched.scheduleJob(jobDetail, trigger);

            logger.info("<<< Leaving cancelOrder30SecondsLater(" + orderId + ").");

      }

 

      private Trigger getOneTimeTrigger()

      {

            long startTime = System.currentTimeMillis() + 30000L;

            SimpleTrigger trigger = new SimpleTrigger("myTrigger", null, new Date(startTime), null, 0, 0L);

            return trigger;

      }

 

      public void setOrderManager(OrderManager orderManager)

      {

            this.orderManager = orderManager;

      }

 

}

 

你可能感兴趣的:(apache,log4j,quartz)