quartz 暂停后启动问题

J2EE 码拜 9年前 (2015-04-15) 1873次浏览 0个评论
 

近日由于项目需要用到quartz组件实现调度管理功能,想要实现调度的启动和暂停以及恢复功能,但是暂停遇到问题
暂停后重新启动,会连续多次调用job中的execute方法。如果当前工作的处理时间过长必然会导致问题。代码如下,急求帮助

import java.util.Date;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;

public class TestTwo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		try {
			SchedulerFactory sf = new StdSchedulerFactory(); 
			Scheduler sched = sf.getScheduler();

			 JobDetail job = new JobDetail();
			 job.setJobClass(HelloJob.class);
			 job.setGroup("jobgourp");
			 job.setName("jobname");

			 SimpleTrigger striger=new SimpleTrigger();
			 striger.setName("strigername");
			 striger.setStartTime(new Date());
			 striger.setRepeatInterval(1000);
			 striger.setRepeatCount(-1);
			 
			 striger.setJobName("jobname");
			 striger.setJobGroup("jobgourp");
			 
			 sched.scheduleJob(job, striger);
			 sched.start();
			 //-------------------------------------------------------
			 Thread.sleep(4000);
			 System.out.println("暂停");
			 
			 sched.pauseJobGroup("jobgourp");
			 sched.pauseTriggerGroup("jobgourp");
			 Thread.sleep(5000);
			 System.out.println("暂停结束");
			 
			 sched.resumeJobGroup("jobgourp");
			 sched.resumeTriggerGroup("jobgourp");
			 sched.start();
			 
			 Thread.sleep(4800);
			 sched.shutdown(true);
		} catch (SchedulerException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
	}

}

job 类代码如下

package com.scy.quartz.test;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class HelloJob implements Job {

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
			System.out.println("HelloJob 执行了"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));

	}

}

HelloJob 执行了2012-08-11 12:57:16
HelloJob 执行了2012-08-11 12:57:17
HelloJob 执行了2012-08-11 12:57:18
HelloJob 执行了2012-08-11 12:57:19
暂停
暂停结束
———-????———–
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
HelloJob 执行了2012-08-11 12:57:25
———-????———–
HelloJob 执行了2012-08-11 12:57:26
HelloJob 执行了2012-08-11 12:57:27
HelloJob 执行了2012-08-11 12:57:28
HelloJob 执行了2012-08-11 12:57:29

quartz 暂停后启动问题
40分
重复发帖。。。浪费分数,让讨论焦点分散,不可取。。。

resumeJobGroup的API解释说了(错过的触发,将在恢复时重新执行):
If any of the Job””sTrigger s missed one or more fire-times, then the Trigger””s misfire instruction will be applied.

所以我想你应该用standby()会更合适(但这个影响是全局性的):
When start() is called (to bring the scheduler out of stand-by mode), trigger misfire instructions will NOT be applied during the execution of the start() method – any misfires will be detected immediately afterward (by the JobStore””s normal process).

或者用unscheduleJob()将这个任务撤掉。

顺带说一句:在你这个例子里面,pauseJobGroup() 跟 pauseTriggerGroup() 重复,因为pauseJobGroup实际上也是暂停它的triggers:
Pause all of the JobDetails in the given group – by pausing all of their Triggers.

quartz 暂停后启动问题
ldh911 standby()和unscheduleJob() 也都试过了也是不行啊 pauseJobGroup() 跟 pauseTriggerGroup() 是我一个不行就两个都加上了 呵呵。兄弟还有什么其他办法吗
quartz 暂停后启动问题
只要是为了避免重启后调度多此执行 带来负面影响。倒是可以通过其他方式来解决 
1.在job里加过锁如果处理这那其他的就不能再处理了 但是也不是好的解决方式啊 
2.利用JobExecutionContext传递参数 但是因为我写好封装 其他人只需要提供job实现类就可以了,通过xml配置让其运行,如果采用这样的方式其他人就有点麻烦了。
quartz 暂停后启动问题
HelloJob 改成实现StatefulJob试试:

HelloJob implements StatefulJob

quartz 暂停后启动问题
真NM坑爹啊,之前用unscheduleJob不好使,是因为 striger.setStartTime(new Date());
的问题,结果改为 
striger.setStartTime(new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”).paere(new Date()));
竟然好了
quartz 暂停后启动问题
最后怎么搞定的呀?  我也遇见这问题啦  求思路
quartz 暂停后启动问题
我这边用的是Quartz.Net
在配置Quartz的时候有这样一句, 
 <add key=”quartz.jobStore.misfireThreshold” value=”60000″/>
注释掉这个就不会出现多次调用了。
上代码

 <quartz>
        <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
        <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
        <add key="quartz.threadPool.threadCount" value="1"/>
        <add key="quartz.threadPool.threadPriority" value="2"/>
        <!--<add key="quartz.jobStore.misfireThreshold" value="60000"/>-->
        <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
        <!--******************************Plugin配置********************************************* -->
        <!--<add key="quartz.plugin.xml.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz" />
        <add key="quartz.plugin.xml.fileNames" value=""/>-->
    </quartz>
    public class HelloJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.mmm"));
        }
    }
 
class Program
    {
        static void Main(string[] args)
        {
            var jobkey = JobKey.Create("job1", "job-group1");
            IJobDetail addJob = JobBuilder.Create<HelloJob>().WithIdentity(jobkey).Build();

            ITrigger trigger = TriggerBuilder.Create().WithIdentity("trigger1", "trigger-group1")
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()).Build();

            var factory = new StdSchedulerFactory();
            var scheduler = factory.GetScheduler();

            scheduler.ScheduleJob(addJob, trigger);

            scheduler.Start();

            Thread.Sleep(TimeSpan.FromSeconds(10));
            scheduler.DeleteJob(jobkey);

            Console.WriteLine("--------------------------------");

            Thread.Sleep(TimeSpan.FromSeconds(2));
            scheduler.ScheduleJob(addJob, trigger);

            Thread.Sleep(TimeSpan.FromSeconds(10));
            scheduler.Shutdown();

            Console.ReadKey();
        }
    }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明quartz 暂停后启动问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!