每分钟安排一次触发,如果作业仍在运行,则待机并等待下一个触发

发布于 2024-07-21 05:36:43 字数 78 浏览 5 评论 0原文

我需要安排一个触发器每分钟触发一次,如果作业仍在运行,则下一分钟触发器不应触发,应再等待一分钟进行检查,如果作业已完成,则触发器应触发 谢谢

I need to schedule a trigger to fire every minute, next minute if the job is still running the trigger should not fire and should wait another minute to check, if job has finished the trigger should fire
Thanks

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

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

发布评论

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

评论(5

掌心的温暖 2024-07-28 05:36:43

在 Quartz 2 中,您需要在作业类上使用 DisallowConcurrentExecution 属性。 然后确保您使用类似于 TriggerBuilder.Create().WithIdentity( "SomeTriggerKey" ) 的方式设置密钥,因为 DisallowConcurrentExecution 使用它来确定您的作业是否已启动跑步。

[DisallowConcurrentExecution]
public class MyJob : IJob
{
 ...
}

In Quartz 2, you'll want to use the DisallowConcurrentExecution attribute on your job class. Then make sure that you set up a key using something similar to TriggerBuilder.Create().WithIdentity( "SomeTriggerKey" ) as DisallowConcurrentExecution uses it to determine if your job is already running.

[DisallowConcurrentExecution]
public class MyJob : IJob
{
 ...
}
云巢 2024-07-28 05:36:43

我没有找到任何关于monitor.enter或类似的东西,无论如何谢谢
另一个答案是作业应该实现“StatefulJob”接口。 作为 StatefulJob,只要一个实例已经在运行,另一个实例就不会运行
再次感谢

I didnt find any thing about monitor.enter or something like that, thanks any way
the other answer is that the job should implement the 'StatefulJob' interface. As a StatefulJob, another instance will not run as long as one is already running
thanks again

国粹 2024-07-28 05:36:43

IStatefulJob 是这里的关键。 当您参与线程时,创建自己的锁定机制可能会导致调度程序出现问题。

IStatefulJob is the key here. Creating own locking mechanisms may cause problems with the scheduler as you are then taking part in the threading.

风筝有风,海豚有海 2024-07-28 05:36:43

如果您使用 Quartz.NET,您可以在 Execute 方法中执行类似的操作:

object execution_lock = new object();

public void Execute(JobExecutionContext context) {
    if (!Monitor.TryEnter(execution_lock, 1)) {
        return,
    }

    // do work

    Monitor.Exit(execution_lock);
}

我突然想到了这一点,也许有些名称是错误的,但这就是想法:在执行时锁定某个对象,如果执行时锁处于打开状态,则先前的作业仍在运行,您只需 return;

编辑:Monitor 类位于 System.Threading 命名空间中

If you're using Quartz.NET, you can do something like this in your Execute method:

object execution_lock = new object();

public void Execute(JobExecutionContext context) {
    if (!Monitor.TryEnter(execution_lock, 1)) {
        return,
    }

    // do work

    Monitor.Exit(execution_lock);
}

I pull this off the top of my head, maybe some names are wrong, but that's the idea: lock on some object while you're executing, and if upon execution the lock is on, then a previous job is still running and you simply return;

EDIT: the Monitor class is in the System.Threading namespace

旧人 2024-07-28 05:36:43

如果您使用 Spring Quartz 集成,则可以将 MethodInvokingJobDetailFactoryBean 中的“concurrent”属性指定为“false”

 <bean id="positionFeedFileProcessorJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="xxxx" />
        <property name="targetMethod" value="xxxx" />
        <property name="concurrent" value="false" /> <!-- This will not run the job if the previous method is not yet finished -->
    </bean>

If you are using spring quartz integration, you can specify the 'concurrent' property to 'false' from MethodInvokingJobDetailFactoryBean

 <bean id="positionFeedFileProcessorJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="xxxx" />
        <property name="targetMethod" value="xxxx" />
        <property name="concurrent" value="false" /> <!-- This will not run the job if the previous method is not yet finished -->
    </bean>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文