Quartz: running multiple jobs

In Quarts, you can use one trigger to schedule multiple tasks so that all tasks run in parallel. What is the best way to do this.

Example: every hour performs tasks j1, j2, ..., jn in parallel. Assuming there is no dependency between tasks.

+5
source share
3 answers

It is not possible to associate several tasks with the same trigger (a task can have several triggers, but not vice versa), but you can configure several identical triggers, one for each task.

, , Quartz . . .

+8

, . JobMap, (, , ).

+1

I ended up with the GetTrigger function

class Program
{
    static void Main(string[] args)
    {
        Common.Logging.LogManager.Adapter = new Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter { Level = Common.Logging.LogLevel.Info };            

        IJobDetail jobOne = JobBuilder.Create<JobOne>()
            .WithIdentity(typeof(JobOne).Name)
            .Build();

        IJobDetail jobTwo = JobBuilder.Create<JobTwo>()
            .WithIdentity(typeof(JobTwo).Name)
            .Build();

        var jobOneTrigger = GetTrigger(new TimeSpan(0, 0, 5), jobOne);
        var jobTwoTrigger = GetTrigger(new TimeSpan(0, 0, 5), jobTwo);

        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
        scheduler.ScheduleJob(jobOne, jobOneTrigger);
        scheduler.ScheduleJob(jobTwo, jobTwoTrigger);

        scheduler.Start();
    }

    private static ITrigger GetTrigger(TimeSpan executionTimeSpan, IJobDetail forJob)
    {            
        return TriggerBuilder.Create()
            .WithIdentity(forJob.JobType.Name+"Trigger")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithInterval(executionTimeSpan)
                .RepeatForever())  
            .ForJob(forJob.JobType.Name)
            .Build();
    }
}

public class JobOne : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Console.WriteLine("JobOne");
    }
}

public class JobTwo : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        Console.WriteLine("JobTwo");
    }
}
0
source

All Articles