public class StartupServlet extends HttpServlet { public void init(ServletConfig cfg) throws javax.servlet.ServletException { initScheduler(cfg); return; } PRotected void initScheduler(ServletConfig cfg){ logger.info("Quartz Init Servlet loaded, initializing Scheduler..."); // Start now try{ // Create an default instance of the Scheduler Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //將scheduler存入serlvet上下文。 cfg.getServletContext().setAttribute(Constants.SCHEDULER_KEY,scheduler); }catch(Exception e){ logger.error("Quartz Init Servlet failed"); } }}
3 程序配置一個(gè)schedule job
/** * @author tyrone * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */public class BatchEditAction extends Action implements PrivilegedAction { private static Logger logger = null; private Scheduler scheduler=null; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ ActionMessages errors=new ActionMessages(); logger = Logger.getLogger(this.getClass()); //獲得Servlet上下文 ServletContext ctx = request.getsession().getServletContext(); //獲得scheduler對(duì)象 scheduler=(Scheduler)ctx.getAttribute(Constants.SCHEDULER_KEY); //根據(jù)form屬性建立job createJob(form); try{ logger.info("Scheduler starting up..."); //啟動(dòng)scheduler。 scheduler.start(); }catch(Exception e){ logger.error("scheduler get error"); } return mapping.findForward("sUCcess"); } /** * create a job based on form info. * @param form * @return */ protected void createJob(ActionForm form) throws Exception{ BatchInfoForm batchinfo=(BatchInfoForm)form; String classname=batchinfo.getFile(); SimpleTrigger sTrigger=null; JobDetail jobDetail=null; Calendar cal=null; //假如是一天一次的job if (batchinfo.getFrequency().equalsIgnoreCase("onceDaily")){ logger.info("Batch run OnceDaily"); cal = new AnnualCalendar(); //Add Calendar to the Scheduler /* * Setup a trigger to start firing now, with a null end date/time, * repeat forever and have (hour*60+ minute)*60000 ms between each firing. */ //開(kāi)始時(shí)間:11:45 String[] time=batchinfo.getDailyStartTime().split(":"); java.util.Calendar rightNow = java.util.Calendar.getInstance(); rightNow.set(java.util.Calendar.HOUR_OF_DAY,new Integer(time[0]).intValue()); rightNow.set(java.util.Calendar.MINUTE,new Integer(time[1]).intValue()); //間隔24小時(shí) long repeatInterval=24*60*60000; sTrigger = new SimpleTrigger("Trigger", Scheduler.DEFAULT_GROUP, rightNow.getTime(), null, SimpleTrigger.REPEAT_INDEFINITELY, repeatInterval); } } // Trigger 關(guān)聯(lián)一個(gè)Calendar, batchinfo.getName()唯一表示一個(gè)Calendar sTrigger.setCalendarName(batchinfo.getName()); scheduler.addCalendar(batchinfo.getName(), cal, true, true); try{ //job類(lèi)名為com.nova.colimas.job.Test jobDetail = new JobDetail(classname, Scheduler.DEFAULT_GROUP, Class.forName(classname)); //job關(guān)聯(lián)一個(gè)Trigger,加入scheduler scheduler.scheduleJob(jobDetail, sTrigger); }catch(ClassNotFoundException ex){ logger.error(ex); throw new Exception(); } return ; }}
4 Job代碼,job必須繼續(xù)org.quartz.Job
package com.nova.colimas.job;import org.apache.log4j.Logger; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class Test implements Job { private static Logger logger = null; public void execute(JobExecutionContext arg0) throws JobExecutionException { // 定時(shí)運(yùn)行。 logger = Logger.getLogger(this.getClass()); logger.info("test job is running"); }}
5 運(yùn)行結(jié)果
[framework] 2005-08-23 11:45:29,440 - com.nova.colimas.job.Test -215700 [DefaultQuartzScheduler_Worker-0] INFO com.nova.colimas.job.Test - test job is running