ilpostino
ilpostino

Reputation:

schedule a trigger every minute, if job still running then standby and wait for the next trigger

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

Upvotes: 5

Views: 9856

Answers (5)

ryan
ryan

Reputation: 1

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>

Upvotes: 0

Ian Davis
Ian Davis

Reputation: 3868

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
{
 ...
}

Upvotes: 17

Marko Lahma
Marko Lahma

Reputation: 6884

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

Upvotes: 2

DonkeyMaster
DonkeyMaster

Reputation: 1352

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

Upvotes: 1

user112435
user112435

Reputation:

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

Upvotes: 5

Related Questions