Quartz:触发多个作业

发布于 2024-08-31 06:07:37 字数 106 浏览 6 评论 0原文

在Quarts中,我可以使用单个触发器来调度多个作业,以便所有作业并行执行。最好的方法是什么?

例如,每小时并行执行作业 j1, j2, ..., jn。假设作业之间不存在依赖关系。

In Quarts, can I use a single trigger to schedule multiple jobs, so that all the jobs gets executed in parallel. What is the best way to do this.

Example, every hour execute Jobs j1, j2, ..., jn in parallel. Assuming that there is no dependency between the jobs.

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

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

发布评论

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

评论(3

も让我眼熟你 2024-09-07 06:07:37

您无法将多个作业与同一触发器关联(一个给定作业可以有多个触发器,但反之则不然),但您可以设置多个相同的触发器,每个作业一个触发器。

为了让它们并行运行,您需要确保 Quartz 的线程池有足够的容量来执行此操作。请参阅此处了解线程池的配置选项。

You can't associate multiple jobs with the same trigger (a given job can have multiple triggers, but not vice versa), but you can set up multiple identical triggers, one for each job.

In order to get them running in parallel, you need to ensure that Quartz's thread pool has enough capacity to do so. See here for the config options for the thread pool.

过度放纵 2024-09-07 06:07:37

您可以构建一个触发其他作业的触发器作业。通过使用 JobMap 属性使其可配置,并且您可以重用该类来触发任意一组作业(并且可能自己执行第一个作业)。

You can build a trigger job that triggers the other jobs. Make it configurable by using the JobMap properties, and you can reuse the class for triggering an arbitrary set of jobs (and maybe executing the first for yourself).

风筝有风,海豚有海 2024-09-07 06:07:37

我最终制作了一个帮助函数 GetTrigger

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");
    }
}

I ended up making a help function GetTrigger

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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文