首页新闻找找看学习计划

asp.net core 依赖注入使用Quartz.Net 不能执行多个任务

0
悬赏园豆:20 [待解决问题]

async static Task Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<DemoJob>();
serviceCollection.AddScoped<DemoJob2>();
serviceCollection.AddScoped<IDemoService, DemoService>();
var serviceProvider = serviceCollection.BuildServiceProvider();

        await ScheduleJob(serviceProvider);
        Console.ReadLine();
    }

    private static async Task ScheduleJob(IServiceProvider serviceProvider)
    {
        var props = new NameValueCollection
        {
            { "quartz.serializer.type", "binary" }
        };
        var factory = new StdSchedulerFactory(props);
        var sched = await factory.GetScheduler();

        await sched.Start();
        sched.JobFactory = new DemoJobFactory(serviceProvider);
        var job = JobBuilder.Create<DemoJob>()
            .WithIdentity("myJob", "group1")
            .Build();
        var trigger = TriggerBuilder.Create()
            .WithIdentity("myTrigger", "group1")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithIntervalInSeconds(5)
                .RepeatForever())
        .Build();
        await sched.ScheduleJob(job, trigger);


        sched.JobFactory = new DemoJob2Factory(serviceProvider);
        var job2 = JobBuilder.Create<DemoJob2>()
            .WithIdentity("myJob2", "group2")
            .Build();
        var trigger2 = TriggerBuilder.Create()
            .WithIdentity("myTrigger2", "group2")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithIntervalInSeconds(5)
                .RepeatForever())
        .Build();
        await sched.ScheduleJob(job2, trigger2);
    }

DemoJob2 会执行两遍,DemoJob不会执行

问题补充:

参考例子, https://github.com/Wkalmar/QuartzDI.Demo

似水阳光的主页 似水阳光 | 初学一级 | 园豆:94
提问于:2019-06-16 16:17
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册