public class TestJob implements Job { public TestJob(){} public void execute(JobExecutionContext arg0) throws JobExecutionException { String name = context.getJobDetail().getJobDataMap().getString("name"); System.out.println("job executing..."+name); } } public class QuartzTest { public static void main(String[] args) { QuartzTest test = new QuartzTest(); try { test.startSchedule(); } catch (Exception e) { e.printStackTrace(); } } public void startSchedule() throws Exception { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = new JobDetail("testJob", Scheduler.DEFAULT_GROUP, TestJob.class); //结束时间 long end = System.currentTimeMillis() + 9000L; //执行10次,每3秒执行一次,到9秒后结束 SimpleTrigger trigger = new SimpleTrigger("test",null,new Date(),new Date(end),10,3000L); scheduler.scheduleJob(jobDetail, trigger); scheduler.start(); } }
在web.xml中添加QuartzInitializerServlet,Quartz为能够在web应用中使用,提供了一个QuartzInitializerServlet和一个QuartzInitializerListener,用于在加载web应用时,对quartz进行初始化。我在使用servlet时加载成功,在使用listener时不成功,不知道怎么回事?
<servlet> <servlet-name>QuartzInitializer</servlet-name> <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class> <init-param> <param-name>shutdown-on-unload</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>config-file</param-name> <param-value>quartz.properties</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
#============================================================================ # Configure Main Scheduler Properties #============================================================================ org.quartz.scheduler.instanceName = org.quartz.scheduler.instanceId = AUTO #============================================================================ # Configure ThreadPool #============================================================================ org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 3 org.quartz.threadPool.threadPriority = 5 #============================================================================ # Configure Plugins #============================================================================ org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin org.quartz.plugin.jobInitializer.fileName = /scheduler/quartz_jobs.xml org.quartz.plugin.jobInitializer.overWriteExistingJobs = true org.quartz.plugin.jobInitializer.failOnFileNotFound = true org.quartz.plugin.jobInitializer.scanInterval = 10
<?xml version='1.0' encoding='utf-8'?> <quartz> <job> <job-detail> <name>test</name> <group>DEFAULT</group> <description>testJobhere</description> <job-class>TestJob</job-class> <job-data-map allows-transient-data="true"> <entry> <key>name</key> <value>test</value> </entry> </job-data-map> </job-detail> <trigger> <cron> <name>testCron</name> <group>DEFAULT</group> <job-name>test</job-name> <job-group>DEFALUT</job-group> <cron-expression>0/3 * * * * ?</cron-expression> </cron> </trigger> </job> </quartz>
public class TestJob implements Job { public TestJob(){} public void execute(JobExecutionContext context) throws JobExecutionException { String name = context.getJobDetail().getJobDataMap().getString("name"); System.out.println("job executing..."+name); } }