<div class="article_title"> <span class="ico ico_type_Original"></span> <h1> <span class="link_title"><a href="/a67474506/article/details/38317119"> Quartz入门实例4-通过JobDataMap实现数据的传输 </a></span> </h1> </div> <div class="article_manage clearfix"> <div class="article_l"> <span class="link_categories"> 标签: <a href="http://www.csdn.net/tag/Quartz%e5%85%a5%e9%97%a8" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">Quartz入门</a><a href="http://www.csdn.net/tag/%e5%ae%9e%e4%be%8b" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">实例</a><a href="http://www.csdn.net/tag/JobDataMap" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">JobDataMap</a> </span> </div> <div class="article_r"> <span class="link_postdate">2014-07-31 14:47</span> <span class="link_view" title="阅读次数">3214人阅读</span> <span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(3)</span> <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('Quartz%e5%85%a5%e9%97%a8%e5%ae%9e%e4%be%8b4-%e9%80%9a%e8%bf%87JobDataMap%e5%ae%9e%e7%8e%b0%e6%95%b0%e6%8d%ae%e7%9a%84%e4%bc%a0%e8%be%93','38317119');return false;" title="收藏" target="_blank">收藏</a></span> <span class="link_report"> <a href="#report" onclick="javascript:report(38317119,2);return false;" title="举报">举报</a></span> </div> </div> <div class="embody" style="display:none" id="embody"> <span class="embody_t">本文章已收录于:</span> <div class="embody_c" id="lib" value="{"err":0,"msg":"ok","data":[]}"></div> </div> <style type="text/css"> .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ededed; } .embody_b{ margin:0 ; padding:10px 0; } .embody .embody_t,.embody .embody_c{ display: inline-block; margin-right:10px; } .embody_t{ font-size: 12px; color:#999; } .embody_c{ font-size: 12px; } .embody_c img,.embody_c em{ display: inline-block; vertical-align: middle; } .embody_c img{ width:30px; height:30px; } .embody_c em{ margin: 0 20px 0 10px; color:#333; font-style: normal; } </style> <script type="text/javascript"> $(function () { try { var lib = eval("("+$("#lib").attr("value")+")"); var html = ""; if (lib.err == 0) { $.each(lib.data, function (i) { var obj = lib.data[i]; //html += '<img src="' + obj.logo + '"/>' + obj.name + " "; html += ' <a href="' + obj.url + '" target="_blank">'; html += ' <img src="' + obj.logo + '">'; html += ' <em><b>' + obj.name + '</b></em>'; html += ' </a>'; }); if (html != "") { setTimeout(function () { $("#lib").html(html); $("#embody").show(); }, 100); } } } catch (err) { } }); </script> <div class="category clearfix"> <div class="category_l"> <img src="http://static.blog.csdn.net/images/category_icon.jpg"> <span>分类:</span> </div> <div class="category_r"> <label onclick="GetCategoryArticles('2438035','a67474506','top','38317119');"> <span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">quartz<em>(13)</em></span> <img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;"> <img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;"> <div class="subItem"> <div class="subItem_t"><a href="http://blog.csdn.net/a67474506/article/category/2438035" target="_blank">作者同类文章</a><i class="J_close">X</i></div> <ul class="subItem_l" id="top_2438035"> </ul> </div> </label> </div> </div> <div class="bog_copyright"> <p class="copyright_p">版权声明:本文为博主原创文章,博客地址:http://blog.csdn.net/a67474506?viewmode=contents</p> </div> <div id="article_content" class="article_content tracking-ad" data-mod="popu_307" data-dsm="post"> <div style="top:0px"></div> <p>JobDataMap 通过它的超类 <span style="color:#800080">org.quartz.util.DirtyFlagMap</span> 实现了<span style="color:#800080"><a href="http://lib.csdn.net/base/java" class="replace_word" title="Java 知识库" target="_blank" style="color:#df3434; font-weight:bold;">Java</a>.util.Map</span> 接口,你可以向 <span style="color:#800080">JobDataMap</span> 中存入键/值对,那些数据对可在你的 Job 类中传递和进行访问。这是一个向你的 Job 传送配置的信息便捷方法。</p> <p><br> </p> <p>ColorJob.java</p> <div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 462px; top: 687px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_1" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=18&height=18" wmode="transparent" width="18" align="middle" height="18"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">package</span><span> cn.zto.job; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> java.text.SimpleDateFormat; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.util.Date; </span></span></li><li class="alt"><span> </span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.DisallowConcurrentExecution; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.Job; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.JobDataMap; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.JobDetail; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.JobExecutionContext; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.JobExecutionException; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.JobKey; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.PersistJobDataAfterExecution; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.slf4j.Logger; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.slf4j.LoggerFactory; </span></span></li><li class=""><span><span class="comment">//对jobDateMap实现持久化 将上次处理过得值存入jobDateMap</span><span> </span></span></li><li class="alt"><span><span class="annotation">@PersistJobDataAfterExecution</span><span> </span></span></li><li class=""><span><span class="annotation">@DisallowConcurrentExecution</span><span> </span></span></li><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> ColorJob </span></span></li><li class=""><span> <span class="keyword">implements</span><span> Job </span></span></li><li class="alt"><span>{ </span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String FAVORITE_COLOR = </span><span class="string">"favorite color"</span><span>; </span></span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> String EXECUTION_COUNT = </span><span class="string">"count"</span><span>; </span></span></li><li class=""><span> <span class="keyword">private</span><span> </span><span class="keyword">int</span><span> _counter = </span><span class="number">1</span><span>; </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> execute(JobExecutionContext context) </span></span></li><li class="alt"><span> <span class="keyword">throws</span><span> JobExecutionException </span></span></li><li class=""><span> { </span></li><li class="alt"><span> SimpleDateFormat dateFormat = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy年MM月dd日 HH时mm分ss秒"</span><span>); </span></span></li><li class=""><span> JobKey jobKey = context.getJobDetail().getKey(); </span></li><li class="alt"><span> </span></li><li class=""><span> JobDataMap data = context.getJobDetail().getJobDataMap(); </span></li><li class="alt"><span> String favoriteColor = data.getString(<span class="string">"color"</span><span>); </span></span></li><li class=""><span> <span class="keyword">int</span><span> count = data.getInt(</span><span class="string">"count"</span><span>); </span></span></li><li class="alt"><span> System.out.println((<span class="string">"ColorJob: 在 "</span><span> + dateFormat.format(</span><span class="keyword">new</span><span> Date()) + </span><span class="string">"执行 "</span><span>+ jobKey +</span><span class="string">"\n"</span><span> </span></span></li><li class=""><span> + <span class="string">" color : "</span><span> + favoriteColor + </span><span class="string">"\n"</span><span> </span></span></li><li class="alt"><span> + <span class="string">" 第 "</span><span> + count + </span><span class="string">"次 执行\n"</span><span> </span></span></li><li class=""><span> + <span class="string">" 成员变量_counter是第 "</span><span> + </span><span class="keyword">this</span><span>._counter+ </span><span class="string">"次 执行"</span><span>)); </span></span></li><li class="alt"><span> </span></li><li class=""><span> ++count; </span></li><li class="alt"><span> data.put(<span class="string">"count"</span><span>, count); </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">this</span><span>._counter += </span><span class="number">1</span><span>; </span></span></li><li class=""><span> } </span></li><li class="alt"><span>} </span></li></ol></div><pre class="java" name="code" style="display: none;">package cn.zto.job; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.DisallowConcurrentExecution; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.JobKey; import org.quartz.PersistJobDataAfterExecution; import org.slf4j.Logger; import org.slf4j.LoggerFactory; //对jobDateMap实现持久化 将上次处理过得值存入jobDateMap @PersistJobDataAfterExecution @DisallowConcurrentExecution public class ColorJob implements Job { public static final String FAVORITE_COLOR = "favorite color"; public static final String EXECUTION_COUNT = "count"; private int _counter = 1; public void execute(JobExecutionContext context) throws JobExecutionException { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); JobKey jobKey = context.getJobDetail().getKey(); JobDataMap data = context.getJobDetail().getJobDataMap(); String favoriteColor = data.getString("color"); int count = data.getInt("count"); System.out.println(("ColorJob: 在 " + dateFormat.format(new Date()) + "执行 "+ jobKey +"\n" + " color : " + favoriteColor + "\n" + " 第 " + count + "次 执行\n" + " 成员变量_counter是第 " + this._counter+ "次 执行")); ++count; data.put("count", count); this._counter += 1; } }</pre> <p><br> </p> <p>JobStateExample.java</p> <p></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 462px; top: 1636px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_2" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=18&height=18" wmode="transparent" width="18" align="middle" height="18"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">package</span><span> cn.zto.app; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> java.text.SimpleDateFormat; </span></span></li><li class=""><span><span class="keyword">import</span><span> java.util.Date; </span></span></li><li class="alt"><span> </span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.DateBuilder; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.JobBuilder; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.JobDetail; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.Scheduler; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.SchedulerFactory; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.SchedulerMetaData; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.SimpleScheduleBuilder; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.SimpleTrigger; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.quartz.TriggerBuilder; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.quartz.impl.StdSchedulerFactory; </span></span></li><li class=""><span><span class="keyword">import</span><span> org.slf4j.Logger; </span></span></li><li class="alt"><span><span class="keyword">import</span><span> org.slf4j.LoggerFactory; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> cn.zto.job.ColorJob; </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> JobStateExample </span></span></li><li class=""><span>{ </span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> run() </span></span></li><li class=""><span> <span class="keyword">throws</span><span> Exception </span></span></li><li class="alt"><span> { </span></li><li class=""><span> System.out.println(<span class="string">"------- 初始化 -------------------"</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> SchedulerFactory sf = <span class="keyword">new</span><span> StdSchedulerFactory(); </span></span></li><li class="alt"><span> Scheduler sched = sf.getScheduler(); </span></li><li class=""><span> </span></li><li class="alt"><span> System.out.println(<span class="string">"------- 初始化完成 --------"</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span> System.out.println(<span class="string">"------- 向Scheduler加入Job ----------------"</span><span>); </span></span></li><li class=""><span> SimpleDateFormat dateFormat = <span class="keyword">new</span><span> SimpleDateFormat(</span><span class="string">"yyyy年MM月dd日 HH时mm分ss秒"</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> Date startTime = DateBuilder.nextGivenSecondDate(<span class="keyword">null</span><span>, </span><span class="number">10</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> JobDetail job1 = JobBuilder.newJob(ColorJob.<span class="keyword">class</span><span>).withIdentity(</span><span class="string">"job1"</span><span>, </span><span class="string">"group1"</span><span>).build(); </span></span></li><li class="alt"><span> </span></li><li class=""><span> SimpleTrigger trigger1 = (SimpleTrigger)TriggerBuilder.newTrigger() </span></li><li class="alt"><span> .withIdentity(<span class="string">"trigger1"</span><span>, </span><span class="string">"group1"</span><span>) </span></span></li><li class=""><span> .startAt(startTime) </span></li><li class="alt"><span> .withSchedule(SimpleScheduleBuilder.simpleSchedule() </span></li><li class=""><span> .withIntervalInSeconds(<span class="number">10</span><span>) </span></span></li><li class="alt"><span> .withRepeatCount(<span class="number">4</span><span>)) </span></span></li><li class=""><span> .build(); </span></li><li class="alt"><span> </span></li><li class=""><span> job1.getJobDataMap().put(<span class="string">"color"</span><span>, </span><span class="string">"Green"</span><span>); </span></span></li><li class="alt"><span> job1.getJobDataMap().put(<span class="string">"count"</span><span>, </span><span class="number">1</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span> Date scheduleTime1 = sched.scheduleJob(job1, trigger1); </span></li><li class=""><span> System.out.println(job1.getKey() + <span class="string">" 将在: "</span><span> + dateFormat.format(scheduleTime1) + </span><span class="string">" 运行,重复 "</span><span> + trigger1.getRepeatCount() + </span><span class="string">" 次,每 "</span><span> + trigger1.getRepeatInterval() / 1000L + </span><span class="string">" 秒执行一次"</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> JobDetail job2 = JobBuilder.newJob(ColorJob.<span class="keyword">class</span><span>).withIdentity(</span><span class="string">"job2"</span><span>, </span><span class="string">"group1"</span><span>).build(); </span></span></li><li class="alt"><span> </span></li><li class=""><span> SimpleTrigger trigger2 = (SimpleTrigger)TriggerBuilder.newTrigger() </span></li><li class="alt"><span> .withIdentity(<span class="string">"trigger2"</span><span>, </span><span class="string">"group1"</span><span>) </span></span></li><li class=""><span> .startAt(startTime) </span></li><li class="alt"><span> .withSchedule(SimpleScheduleBuilder.simpleSchedule() </span></li><li class=""><span> .withIntervalInSeconds(<span class="number">10</span><span>) </span></span></li><li class="alt"><span> .withRepeatCount(<span class="number">4</span><span>)) </span></span></li><li class=""><span> .build(); </span></li><li class="alt"><span> </span></li><li class=""><span> job2.getJobDataMap().put(<span class="string">"color"</span><span>, </span><span class="string">"Red"</span><span>); </span></span></li><li class="alt"><span> job2.getJobDataMap().put(<span class="string">"count"</span><span>, </span><span class="number">1</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span> Date scheduleTime2 = sched.scheduleJob(job2, trigger2); </span></li><li class=""><span> System.out.println(job2.getKey().toString() + <span class="string">" 将在: "</span><span> + dateFormat.format(scheduleTime2) + </span><span class="string">" 运行,重复 "</span><span> + trigger2.getRepeatCount() + </span><span class="string">" 次,每 "</span><span> + trigger2.getRepeatInterval() / 1000L + </span><span class="string">" 秒执行一次"</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> System.out.println(<span class="string">"------- 开始Scheduler ----------------"</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> sched.start(); </span></li><li class="alt"><span> </span></li><li class=""><span> System.out.println(<span class="string">"------- Scheduler调用job结束 -----------------"</span><span>); </span></span></li><li class="alt"><span> </span></li><li class=""><span> System.out.println(<span class="string">"------- 等待60秒... -------------"</span><span>); </span></span></li><li class="alt"><span> <span class="keyword">try</span><span> </span></span></li><li class=""><span> { </span></li><li class="alt"><span> Thread.sleep(60000L); </span></li><li class=""><span> } </span></li><li class="alt"><span> <span class="keyword">catch</span><span> (Exception e) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> System.out.println(<span class="string">"------- 关闭Scheduler ---------------------"</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span> sched.shutdown(<span class="keyword">true</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span> System.out.println(<span class="string">"------- 关闭完成 -----------------"</span><span>); </span></span></li><li class=""><span> </span></li><li class="alt"><span> SchedulerMetaData metaData = sched.getMetaData(); </span></li><li class=""><span> System.out.println(<span class="string">"Executed "</span><span> + metaData.getNumberOfJobsExecuted() + </span><span class="string">" jobs."</span><span>); </span></span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> main(String[] args) </span></span></li><li class=""><span> <span class="keyword">throws</span><span> Exception </span></span></li><li class="alt"><span> { </span></li><li class=""><span> JobStateExample example = <span class="keyword">new</span><span> JobStateExample(); </span></span></li><li class="alt"><span> example.run(); </span></li><li class=""><span> } </span></li><li class="alt"><span>} </span></li></ol></div><pre class="java" name="code" style="display: none;">package cn.zto.app; import java.text.SimpleDateFormat; import java.util.Date; import org.quartz.DateBuilder; import org.quartz.JobBuilder; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerFactory; import org.quartz.SchedulerMetaData; import org.quartz.SimpleScheduleBuilder; import org.quartz.SimpleTrigger; import org.quartz.TriggerBuilder; import org.quartz.impl.StdSchedulerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import cn.zto.job.ColorJob; public class JobStateExample { public void run() throws Exception { System.out.println("------- 初始化 -------------------"); SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); System.out.println("------- 初始化完成 --------"); System.out.println("------- 向Scheduler加入Job ----------------"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒"); Date startTime = DateBuilder.nextGivenSecondDate(null, 10); JobDetail job1 = JobBuilder.newJob(ColorJob.class).withIdentity("job1", "group1").build(); SimpleTrigger trigger1 = (SimpleTrigger)TriggerBuilder.newTrigger() .withIdentity("trigger1", "group1") .startAt(startTime) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .withRepeatCount(4)) .build(); job1.getJobDataMap().put("color", "Green"); job1.getJobDataMap().put("count", 1); Date scheduleTime1 = sched.scheduleJob(job1, trigger1); System.out.println(job1.getKey() + " 将在: " + dateFormat.format(scheduleTime1) + " 运行,重复 " + trigger1.getRepeatCount() + " 次,每 " + trigger1.getRepeatInterval() / 1000L + " 秒执行一次"); JobDetail job2 = JobBuilder.newJob(ColorJob.class).withIdentity("job2", "group1").build(); SimpleTrigger trigger2 = (SimpleTrigger)TriggerBuilder.newTrigger() .withIdentity("trigger2", "group1") .startAt(startTime) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(10) .withRepeatCount(4)) .build(); job2.getJobDataMap().put("color", "Red"); job2.getJobDataMap().put("count", 1); Date scheduleTime2 = sched.scheduleJob(job2, trigger2); System.out.println(job2.getKey().toString() + " 将在: " + dateFormat.format(scheduleTime2) + " 运行,重复 " + trigger2.getRepeatCount() + " 次,每 " + trigger2.getRepeatInterval() / 1000L + " 秒执行一次"); System.out.println("------- 开始Scheduler ----------------"); sched.start(); System.out.println("------- Scheduler调用job结束 -----------------"); System.out.println("------- 等待60秒... -------------"); try { Thread.sleep(60000L); } catch (Exception e) { } System.out.println("------- 关闭Scheduler ---------------------"); sched.shutdown(true); System.out.println("------- 关闭完成 -----------------"); SchedulerMetaData metaData = sched.getMetaData(); System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } public static void main(String[] args) throws Exception { JobStateExample example = new JobStateExample(); example.run(); } }</pre><br> <p></p> <p></p> <p>运行结果如下:</p> <p><br> </p> <p></p><div class="dp-highlighter bg_plain"><div class="bar"><div class="tools"><b>[plain]</b> <a href="#" class="ViewSource" title="view plain" onclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" onclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 464px; top: 3664px; width: 18px; height: 18px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_3" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=18&height=18" wmode="transparent" width="18" align="middle" height="18"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" onclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" onclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1"><li class="alt"><span><span>------- 初始化 ------------------- </span></span></li><li class=""><span>SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". </span></li><li class="alt"><span>SLF4J: Defaulting to no-operation (NOP) logger implementation </span></li><li class=""><span>SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. </span></li><li class="alt"><span>------- 初始化完成 -------- </span></li><li class=""><span>------- 向Scheduler加入Job ---------------- </span></li><li class="alt"><span>group1.job1 将在: 2014年07月31日 15时20分40秒 运行,重复 4 次,每 10 秒执行一次 </span></li><li class=""><span>group1.job2 将在: 2014年07月31日 15时20分40秒 运行,重复 4 次,每 10 秒执行一次 </span></li><li class="alt"><span>------- 开始Scheduler ---------------- </span></li><li class=""><span>------- Scheduler调用job结束 ----------------- </span></li><li class="alt"><span>------- 等待60秒... ------------- </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时20分40秒执行 group1.job1 </span></li><li class="alt"><span> color : Green </span></li><li class=""><span> 第 1次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时20分40秒执行 group1.job2 </span></li><li class="alt"><span> color : Red </span></li><li class=""><span> 第 1次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时20分50秒执行 group1.job1 </span></li><li class="alt"><span> color : Green </span></li><li class=""><span> 第 2次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时20分50秒执行 group1.job2 </span></li><li class="alt"><span> color : Red </span></li><li class=""><span> 第 2次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时21分00秒执行 group1.job1 </span></li><li class="alt"><span> color : Green </span></li><li class=""><span> 第 3次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时21分00秒执行 group1.job2 </span></li><li class="alt"><span> color : Red </span></li><li class=""><span> 第 3次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时21分10秒执行 group1.job1 </span></li><li class="alt"><span> color : Green </span></li><li class=""><span> 第 4次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时21分10秒执行 group1.job2 </span></li><li class="alt"><span> color : Red </span></li><li class=""><span> 第 4次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时21分20秒执行 group1.job1 </span></li><li class="alt"><span> color : Green </span></li><li class=""><span> 第 5次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>ColorJob: 在 2014年07月31日 15时21分20秒执行 group1.job2 </span></li><li class="alt"><span> color : Red </span></li><li class=""><span> 第 5次 执行 </span></li><li class="alt"><span> 成员变量_counter是第 1次 执行 </span></li><li class=""><span>------- 关闭Scheduler --------------------- </span></li><li class="alt"><span>------- 关闭完成 ----------------- </span></li><li class=""><span>Executed 10 jobs. </span></li></ol></div><pre class="plain" name="code" style="display: none;">------- 初始化 ------------------- SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. ------- 初始化完成 -------- ------- 向Scheduler加入Job ---------------- group1.job1 将在: 2014年07月31日 15时20分40秒 运行,重复 4 次,每 10 秒执行一次 group1.job2 将在: 2014年07月31日 15时20分40秒 运行,重复 4 次,每 10 秒执行一次 ------- 开始Scheduler ---------------- ------- Scheduler调用job结束 ----------------- ------- 等待60秒... ------------- ColorJob: 在 2014年07月31日 15时20分40秒执行 group1.job1 color : Green 第 1次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时20分40秒执行 group1.job2 color : Red 第 1次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时20分50秒执行 group1.job1 color : Green 第 2次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时20分50秒执行 group1.job2 color : Red 第 2次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时21分00秒执行 group1.job1 color : Green 第 3次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时21分00秒执行 group1.job2 color : Red 第 3次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时21分10秒执行 group1.job1 color : Green 第 4次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时21分10秒执行 group1.job2 color : Red 第 4次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时21分20秒执行 group1.job1 color : Green 第 5次 执行 成员变量_counter是第 1次 执行 ColorJob: 在 2014年07月31日 15时21分20秒执行 group1.job2 color : Red 第 5次 执行 成员变量_counter是第 1次 执行 ------- 关闭Scheduler --------------------- ------- 关闭完成 ----------------- Executed 10 jobs. </pre><br> <br> <p></p> <p></p> <div style="top:248px"></div> </div> <!-- Baidu Button BEGIN --> <div class="bdsharebuttonbox tracking-ad bdshare-button-style0-16" style="float: right;" data-mod="popu_172" data-bd-bind="1497424645902"> <a href="#" class="bds_more" data-cmd="more" style="background-position:0 0 !important; background-image: url(http://bdimg.share.baidu.com/static/api/img/share/icons_0_16.png?v=d754dcc0.png) !important" target="_blank"></a> <a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间" style="background-position:0 -52px !important" target="_blank"></a> <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博" style="background-position:0 -104px !important" target="_blank"></a> <a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博" style="background-position:0 -260px !important" target="_blank"></a> <a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网" style="background-position:0 -208px !important" target="_blank"></a> <a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信" style="background-position:0 -1612px !important" target="_blank"></a> </div> <script>window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "1", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "16" }, "share": {} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];</script> <!-- Baidu Button END --> <!--172.16.140.13--> <!-- Baidu Button BEGIN --> <script type="text/javascript" id="bdshare_js" data="type=tools&uid=1536434" src="http://bdimg.share.baidu.com/static/js/bds_s_v2.js?cdnversion=415952"></script> <script type="text/javascript"> document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000) </script> <!-- Baidu Button END --> <div id="digg" articleid="38317119"> <dl id="btnDigg" class="digg digg_enable" onclick="btndigga();"> <dt>顶</dt> <dd>0</dd> </dl> <dl id="btnBury" class="digg digg_enable" onclick="btnburya();"> <dt>踩</dt> <dd>0</dd> </dl> </div> <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank"> </a> </div> <div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank"> </a></div> <script type="text/javascript"> function btndigga() { $(".tracking-ad[data-mod='popu_222'] a").click(); } function btnburya() { $(".tracking-ad[data-mod='popu_223'] a").click(); } </script> <ul class="article_next_prev"> <li class="prev_article"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian']);location.href='/a67474506/article/details/38316273';">上一篇</span><a href="/a67474506/article/details/38316273" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian'])">Quartz入门实例3-使用CronTrigger指定触发器执行job</a></li> <li class="next_article"><span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian']);location.href='/a67474506/article/details/38320167';">下一篇</span><a href="/a67474506/article/details/38320167" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian'])">Quartz入门实例5-处理因执行job超时而错过触发的job</a></li> </ul> <div style="clear:both; height:10px;"></div> <div class="similar_article"> <h4></h4> <div class="similar_c" style="margin:20px 0px 0px 0px"> <div class="similar_c_t"> 相关文章推荐 </div> <div class="similar_wrap tracking-ad" data-mod="popu_36" style="max-height:195px;"> <ul class="similar_list fl"> <li> <em>•</em> <a href="http://blog.csdn.net/xlxxcc/article/details/52104463" title="Quartz 2.2 的实现原理和运行过程" strategy="" target="_blank" style="width: 290px;">Quartz 2.2 的实现原理和运行过程</a> </li> <li> <em>•</em> <a href="http://blog.csdn.net/weinierbian/article/details/6242707" title="不支持类型 Quartz.JobDataMap,因为它实现 IDictionary。 " strategy="" target="_blank" style="width: 290px;">不支持类型 Quartz.JobDataMap,因为它实现 IDictionary。 </a> </li> <li> <em>•</em> <a href="http://blog.csdn.net/a67474506/article/details/38369059" title="Quartz入门实例13-spirng4和quartz2实现动态任务调用" strategy="" target="_blank" style="width: 290px;">Quartz入门实例13-spirng4和quartz2实现动态任务调用</a> </li> <li> <em>•</em> <a href="http://1127057102.iteye.com/blog/2108461" title="Quartz入门实例13-spirng4和quartz2实现动态任务调用" strategy="" target="_blank" style="width: 290px;">Quartz入门实例13-spirng4和quartz2实现动态任务调用</a> </li> <li> <em>•</em> <a href="http://blog.csdn.net/a67474506/article/details/38325721" title="Quartz入门实例9-实现Trigger监听器" strategy="" target="_blank" style="width: 290px;">Quartz入门实例9-实现Trigger监听器</a> </li> </ul> <ul class="similar_list fr"> <li> <em>•</em> <a href="http://blog.csdn.net/a67474506/article/details/38323811" title="Quartz入门实例8-实现一个Job监听器" strategy="" target="_blank" style="width: 290px;">Quartz入门实例8-实现一个Job监听器</a> </li> <li> <em>•</em> <a href="http://blog.csdn.net/caomiao2006/article/details/46417197" title="Quartz 有状态的JobDataMap" strategy="" target="_blank" style="width: 290px;">Quartz 有状态的JobDataMap</a> </li> <li> <em>•</em> <a href="http://blog.csdn.net/u010002184/article/details/72598766" title="Quartz执行多作业,JobDataMap传值" strategy="" target="_blank" style="width: 290px;">Quartz执行多作业,JobDataMap传值</a> </li> <li> <em>•</em> <a href="http://blog.csdn.net/a4307515/article/details/46989743" title="Quartz 2.2.1学习笔记 (二) Jobs、JobDetail、JobDataMap" strategy="" target="_blank" style="width: 290px;">Quartz 2.2.1学习笔记 (二) Jobs、JobDetail、JobDataMap</a> </li> <li> <em>•</em> <a href="http://blog.csdn.net/a67474506/article/details/38367755" title="Quartz入门实例12-Spring4和quartz2的集群" strategy="" target="_blank" style="width: 290px;">Quartz入门实例12-Spring4和quartz2的集群</a> </li> </ul> </div> </div> </div>