标签:
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.4</version> </dependency>
1、
package cn.sniper.spider.utils; import java.text.ParseException; import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.impl.StdSchedulerFactory; import cn.sniper.spider.main.UrlJob; /** * 参考文章 * http://blog.csdn.net/czplplp_900725/article/details/39995295 * @author sniper * */ public class SchedulerUtil { /** * 启动一个定时任务 * @param job * @param cronExpression */ public static void run(Class<? extends Job> clazz, String cronExpression) { try { Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.start(); String jobName = clazz.getSimpleName(); JobDetail jobDetail = new JobDetail(jobName, Scheduler.DEFAULT_GROUP, clazz); CronTrigger trigger = new CronTrigger(jobName, Scheduler.DEFAULT_GROUP, cronExpression); scheduler.scheduleJob(jobDetail, trigger); } catch (SchedulerException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } public static void main(String[] args) { //每隔两秒执行一次 SchedulerUtil.run(UrlJob.class, CronExpressionUtil.getSecondCronExpression(2)); } }
2、
package cn.sniper.spider.main; import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 往队列初始化根链接 * @author sniper * */ public class UrlJob implements Job { private static Logger logger = LoggerFactory.getLogger(UrlJob.class); public void execute(JobExecutionContext context) throws JobExecutionException { /*Spider spider = Spider.getInstance(); UrlJob.initRootUrl(spider);*/ System.err.println(System.currentTimeMillis()); logger.info("job执行成功。。。"); } }
标签:
原文地址:http://my.oschina.net/sniperLi/blog/530697