怎样用quartz 调度Job
1.所需要的第三方包:
quartz-1.5.2.jar
commons-beanutils.jar
commons-digester-1.7.jar
commons-logging.jar
commons-logging-api.jar
log4j-1.2.11.jar
2.定义自己的job:
public class ScanJob implements Job { public void SanJob(){} public void execute(JobExecutionContext arg0) throws JobExecutionException { Logger logger = null; logger = Logger.getLogger(this.getClass()); logger.info("test job is running"); System.out.println("======================"); System.out.println("this is my 11111111111111111 job"); System.out.println("this is my 22222222222222222 job"); Date abc=new Date(); System.out.println("-------------"+ abc.getSeconds()); } }
3.在 web.xml加入:
<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>
4.在classes的目录下建立quartz.properties
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = QuartzScheduler
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 1000
#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 = quartz_jobs.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
#org.quartz.plugin.jobInitializer.failOnFileNotFound = true
5.在classes的目录下建立quartz_jobs.xml
<?xml version='1.0' encoding='utf-8'?>
<quartz>
<job>
<job-detail>
<name>test</name>
<group>DEFAULT</group>
<description>testJobhere</description>
<job-class>www.ngc.com.ScanJob</job-class>
</job-detail>
<trigger>
<cron>
<name>testTrigger</name>
<group>DEFAULT</group>
<job-name>test</job-name>
<job-group>DEFALUT</job-group>
<cron-expression>0/5 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
</quartz>
6.用main方法测试:
public class QuartzMain { public static void main(String[] args) { QuartzMain server = new QuartzMain(); try { server.startScheduler(); } catch (SchedulerException e) { e.printStackTrace(); } } protected void startScheduler() throws SchedulerException { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = new JobDetail("testJob", Scheduler.DEFAULT_GROUP, ScanJob.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(); } }
7.在应用服务器中发布,就可以在console看到日志.
======================
this is my 11111111111111111 job
this is my 22222222222222222 job
-------------35