首页 新闻 会员 周边 捐助

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

0
悬赏园豆:20 [已关闭问题] 关闭于 2019-10-23 20:51

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

似水阳光的主页 似水阳光 | 初学一级 | 园豆:109
提问于:2019-06-16 16:17
< >
分享
所有回答(1)
0

这个问题我也遇到了,有没有解决方案?

chjsd1511 | 园豆:202 (菜鸟二级) | 2019-10-25 19:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册