JobDetail 记录可以重用吗?如何重用? [英] Can JobDetail records be re-used, and how?

查看:50
本文介绍了JobDetail 记录可以重用吗?如何重用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在数据库中但不在内存中的 JobDetail 记录上创建新触发器?具体用途:我安排了每天使用 cron 触发器运行的作业.有时需要使用相同的参数重新运行特定日期的作业.我想使用相同的 JobDetail 创建一个新的简单触发器,因为这是存储参数的地方.Reschedule() 不起作用,因为它删除了现有的触发器.这可能吗?

Is there any way to create a new trigger on a JobDetail record which is in the database, but not in memory? Specific use: I have jobs scheduled to run daily using a cron trigger. Sometimes a particular day's job needs to be re-run with the same parameters. I'd like to create a new simple trigger using the same JobDetail, since that's where the parameters are stored. Reschedule() doesn't work, since it deletes the existing trigger. Is that possible?

推荐答案

是的,你可以.

我要做的是从数据库中获取作业:

What I would do is the fetch the job from the database:

var myJob = Scheduler.GetJobDetail(jobName, groupName);

并使用该功能(您可能已经使用过)

and use the function (which probably you've already used)

Scheduler.ScheduleJob(JobDetail jobDetail, Trigger trigger);

传递您的新触发器.您不必做太多事情,因为运行时会在几秒钟后从数据库中获取新的触发器并运行它.

passing your new trigger. You don't have to do much cause the run-time will fetch the new trigger from the DB after a few seconds and will run it.

更新

使用 Quartz.net 添加触发器有两种方式

There are two ways to add a trigger with Quartz.net

1) 您可以添加作业,然后添加触发器:

1) you can add a job and then the trigger:

Scheduler.AddJob(jobToSchedule, true);
Scheduler.ScheduleJob(triggerToSchedule);

2) 您可以安排触发器同时添加作业:

2) you can schedule a trigger adding a job at the same time:

Scheduler.ScheduleJob(jobToSchedule, triggerToSchedule);

如果您尝试以这种方式添加作业和触发器:

If you try to add a job and a trigger this way:

Scheduler.AddJob(jobToSchedule, true);
Scheduler.ScheduleJob(jobToSchedule, triggerToSchedule);

您收到一个异常,警告您作业已经存在.

You get an exception which warns you the job already exists.

我建议您在进行任何测试之前清理数据库,因为您可能有一些待处理的工作.你可以在这里找到一个简单的程序来清理它:

I would suggest you to clean the DB before making any test cause you might have some pending jobs. You can find a simple routine to clean it here:

        // unschedule jobs
        string[] groups = Scheduler.TriggerGroupNames;
        for (int i = 0; i < groups.Length; i++)
        {
            string[] names = Scheduler.GetTriggerNames(groups[i]);
            for (int j = 0; j < names.Length; j++)
            {
                Scheduler.UnscheduleJob(names[j], groups[i]);
            }
        }

        // delete jobs
        groups = Scheduler.JobGroupNames;
        for (int i = 0; i < groups.Length; i++)
        {
            string[] names = Scheduler.GetJobNames(groups[i]);
            for (int j = 0; j < names.Length; j++)
            {
                Scheduler.DeleteJob(names[j], groups[i]);
            }
        }

这篇关于JobDetail 记录可以重用吗?如何重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆