如何使用两个名为Autofac的Quartz Jobs

时间:2018-08-16 06:27:26

标签: c# asp.net-web-api quartz-scheduler autofac quartz

我在Web API应用程序上创建了两个解析器作业,该作业返回不同的值,但是我不知道如何使这些作业以不同的间隔同时工作。 我有两个解析器类 Html Agilyty解析器

public class HapParser : IParser
{

    public List<Post> Parse()
    {
        const string URL = @"http://s13.ru/";
        var web = new HtmlWeb();
        var htmlDoc = web.Load(URL);
        var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//div[@class='itemhead']");

        return htmlNodes.Select(node => new Post
            {
                Title = node.SelectSingleNode("./h3/a").Name,
                DateTime = DateTime.Now
            })
            .ToList();
    }
}

Rss类解析器

        public List<Post> Parse()
    {
        List<Post> dataList = new List<Post>();

        const string url = "https://news.tut.by/rss/index.rss";

        try
        {
            var xDoc = XDocument.Load(url);
            var items = (from x in xDoc.Descendants("item")select new{title = x.Element("title")?.Value});

            dataList.AddRange(items.Select(i => new Post
            {
                Title = i.title,
                DateTime = DateTime.Now,
            }));
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

        return dataList;
    }

我使用Autofac命名的服务来注册这两种情况

        public static IContainer Container;

    public static void Initialize()
    {
        var builder = new ContainerBuilder();
        var config = GlobalConfiguration.Configuration;

        builder.RegisterWebApiFilterProvider(config);
        builder.RegisterWebApiModelBinderProvider();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        builder.RegisterType<DataBaseContext>().AsSelf();


        builder.Register(x => new StdSchedulerFactory().GetScheduler().Result).As<IScheduler>();
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).Where(x => typeof(IJob).IsAssignableFrom(x));

        builder.RegisterType<HapParser>().Named<IParser>("HAP");
        builder.RegisterType<RssParser>().Named<IParser>("RSS");


        Container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(Container);
    }

但是怎么说programm一起使用两个解析器Jobs?

HapJobScheduler

      public async void Start()
    {
        IScheduler sched = AutofacConfig.Container.Resolve<IScheduler>();
        sched.JobFactory = new AutofacParserJobFactory(AutofacConfig.Container);
        await sched.Start();
        IJobDetail job = JobBuilder.Create<HapParserJob>()
            .WithIdentity("Hap", "admin")
            .Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("Hap", "admin")
            .WithSimpleSchedule(x => x
                .RepeatForever()
                .WithIntervalInMinutes(15)
            )
            .StartNow()
            .Build();

        await sched.ScheduleJob(job, trigger);
    }

RssJobScheduler

        public async void Start()
    {
        IScheduler sched = AutofacConfig.Container.Resolve<IScheduler>();
        sched.JobFactory = new AutofacParserJobFactory(AutofacConfig.Container);
        await sched.Start();
        IJobDetail job = JobBuilder.Create<RssParserJob>()
            .WithIdentity("RSS", "admin")
            .Build();

        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("RSS", "admin")
            .WithSimpleSchedule(x => x
                .RepeatForever()
                .WithIntervalInMinutes(5)
            )
            .StartNow()
            .Build();

        await sched.ScheduleJob(job, trigger);
    }

0 个答案:

没有答案