手动触发 Quartz 作业

发布于 2024-11-29 09:35:56 字数 280 浏览 0 评论 0原文

我们的应用程序中配置了几个 Quartz 作业。在开发过程中,我们将石英调度程序置于待机状态 - 然而,我们有时希望手动启动作业(出于开发目的)。如果我调用 fireTrigger,它会告诉我需要启动调度程序。但是,如果我启动调度程序,它也会立即调度所有其他作业,这不是我想要的(因为它们可能在我调试手动触发的作业时触发)。

当我启动调度程序时,我可以暂停所有触发器,但随后我必须处理失火指令等。

有没有一种简单的方法可以手动启动作业,而不必处理暂停和失火(即 fireTrigger,即使调度程序处于待机状态)?

We have several Quartz jobs configured in our application. During development, we leave the quartz scheduler in standby - however, we sometimes want to start a job manually (for development purposes). If I call fireTrigger, it tells me I need to start the scheduler. However, if I start the scheduler, it will also immediately schedule all the other jobs, which is not what I want (since they may trigger while I'm debugging the manually fired job).

I could pause all triggers when I start the scheduler, but then I have to deal with misfire instructions etc.

Is there a simple way to fire off a job manually without having to deal with pausing and misfires (i.e. a fireTrigger which works even if the scheduler is in standby)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め七分饶幸 2024-12-06 09:35:56

这是手动触发作业所需的循环:

SchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = stdSchedulerFactory.getScheduler();

// loop jobs by group
for (String groupName : scheduler.getJobGroupNames()) {
    // get jobkey
    for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
        String jobName = jobKey.getName();
        String jobGroup = jobKey.getGroup();
        scheduler.triggerJob(jobName, jobGroup);
    }
}

this is the loop you will require to fire the job manually:

SchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = stdSchedulerFactory.getScheduler();

// loop jobs by group
for (String groupName : scheduler.getJobGroupNames()) {
    // get jobkey
    for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
        String jobName = jobKey.getName();
        String jobGroup = jobKey.getGroup();
        scheduler.triggerJob(jobName, jobGroup);
    }
}
慵挽 2024-12-06 09:35:56

所有在 Quartz Scheduler 中注册的 Job 都由 JobKey 唯一标识,JobKey 由名称和组组成。您可以通过调用 Scheduler 实例的 triggerJob(JobKey jobKey) 立即触发具有给定 JobKey 的作业。

// Create a new Job 
JobKey jobKey = JobKey.jobKey("myNewJob", "myJobGroup");
JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity(jobKey).storeDurably().build();

// Register this job to the scheduler
scheduler.addJob(job, true);

// Immediately fire the Job MyJob.class
scheduler.triggerJob(jobKey);

注意:

  • Scheduler 是整个应用程序中使用的 Scheduler 实例。它的 start() 方法应该在创建后就已经被调用。
  • 该作业是持久作业,不能附加任何触发器或 cron。它只能通过调用“triggerJob(JobKey jobKey)”以编程方式触发。
  • All the Jobs registered in the Quartz Scheduler are uniquely identified by the JobKey which is composed of a name and group . You can fire the job which has a given JobKey immediately by calling triggerJob(JobKey jobKey) of your Scheduler instance.

    // Create a new Job 
    JobKey jobKey = JobKey.jobKey("myNewJob", "myJobGroup");
    JobDetail job = JobBuilder.newJob(MyJob.class).withIdentity(jobKey).storeDurably().build();
    
    // Register this job to the scheduler
    scheduler.addJob(job, true);
    
    // Immediately fire the Job MyJob.class
    scheduler.triggerJob(jobKey);
    

    Note:

  • scheduler is the Scheduler instance used throughout your application . Its start() method should be already called after it is created.
  • The job is the durable job which cannot attach any triggers or cron to it .It can only be fired programmatically by calling `triggerJob(JobKey jobKey)`.
  • 猫弦 2024-12-06 09:35:56

    你可以尝试在你的调度程序中添加一个触发过滤器,

    this.scheduler.addGlobalTriggerListener(new DebugExecutionFilter());
    

    调试执行过滤器会在执行不是易失性的(不安排立即运行)并且你处于调试模式时添加否决权。

    这是一个实现示例:

    private static class DebugExecutionFilter implements TriggerListener
    {
    
        public DebugExecutionFilter()
        {
        }
    
        @Override
        public String getName()
        {
            return "Task execution filter";
        }
    
        @Override
        public void triggerFired(Trigger trigger, JobExecutionContext context)
        {
            // Do nothing
        }
    
        /* (non-Javadoc)
         * 
         * @see org.quartz.TriggerListener#vetoJobExecution(org.quartz.Trigger, org.quartz.JobExecutionContext) */
        @Override
        @SuppressWarnings("unchecked")
        /**
         * A veto is added if :
         *  - For non volatile trigger if we are in debug mode
         */
        public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context)
        {
    
            try
            {
                // 
                if ( !trigger.isVolatile() && isDebugMode() )
                {
                    return true;
                }
    
                //task is run by scheduler.triggerJobWithVolatileTrigger() for immediate schedule
                //or task is schedule and we are not in debugMode
                return false;
        }
    
    
        @Override
        public void triggerMisfired(Trigger trigger)
        {
            // do nothing
        }
    
        @Override
        public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode)
        {
            // do nothing
        }
    
    }
    

    You can try to add a trigger filter in your scheduler

    this.scheduler.addGlobalTriggerListener(new DebugExecutionFilter());
    

    The debug execution filter will add a veto when the execution is not volatile (not scheduled to run immediately) and you are in debug mode .

    Here is an implementation example :

    private static class DebugExecutionFilter implements TriggerListener
    {
    
        public DebugExecutionFilter()
        {
        }
    
        @Override
        public String getName()
        {
            return "Task execution filter";
        }
    
        @Override
        public void triggerFired(Trigger trigger, JobExecutionContext context)
        {
            // Do nothing
        }
    
        /* (non-Javadoc)
         * 
         * @see org.quartz.TriggerListener#vetoJobExecution(org.quartz.Trigger, org.quartz.JobExecutionContext) */
        @Override
        @SuppressWarnings("unchecked")
        /**
         * A veto is added if :
         *  - For non volatile trigger if we are in debug mode
         */
        public boolean vetoJobExecution(Trigger trigger, JobExecutionContext context)
        {
    
            try
            {
                // 
                if ( !trigger.isVolatile() && isDebugMode() )
                {
                    return true;
                }
    
                //task is run by scheduler.triggerJobWithVolatileTrigger() for immediate schedule
                //or task is schedule and we are not in debugMode
                return false;
        }
    
    
        @Override
        public void triggerMisfired(Trigger trigger)
        {
            // do nothing
        }
    
        @Override
        public void triggerComplete(Trigger trigger, JobExecutionContext context, int triggerInstructionCode)
        {
            // do nothing
        }
    
    }
    
    给我一枪 2024-12-06 09:35:56

    不需要开始时间结束时间

    <trigger>
        <cron>
            <name>TestTrigger</name>
            <group>CronSampleTrigger</group>
            <description>CronSampleTrigger</description>
            <job-name>TestJob</job-name>
            <job-group>jobGroup1</job-group>
    
            <!--<start-time>1982-06-28T18:15:00.0Z</start-time>
            <end-time>2020-05-04T18:13:51.0Z</end-time>-->
    
            <cron-expression>0 0/1 * * * ?</cron-expression>
        </cron>
    </trigger>
    

    No need for start-time and end-time.

    <trigger>
        <cron>
            <name>TestTrigger</name>
            <group>CronSampleTrigger</group>
            <description>CronSampleTrigger</description>
            <job-name>TestJob</job-name>
            <job-group>jobGroup1</job-group>
    
            <!--<start-time>1982-06-28T18:15:00.0Z</start-time>
            <end-time>2020-05-04T18:13:51.0Z</end-time>-->
    
            <cron-expression>0 0/1 * * * ?</cron-expression>
        </cron>
    </trigger>
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文