现在开始用远景执行quartz计划,24小时间隔Quartz在后台运行任务。计划定义如下:publicclassJobBackground:IJob{publicvoidExecute(IJobExecutionContextcontext){for(intj=1;j<=920;j++){NewsRepositorynewsRepository=newNewsRepository();GoyaAgentGoyaagent=newGoyaAgent();任务<列表>lst=Goyaagent.parsing("http://www.gooyait.com/page/"+j);列表enresult=lst.Result;foreach(NewsContentnewsContentinenresult){Newsnewnews=newNews();newnews.Subject=newsContent.Title;newnews.NewsDate=DateTime.Now;newnews.NewsBrief=newsContent.abs;newnews.NewsText=newsContent.Content;newnews.ShowOnSlide="是";newnews.GroupId=1049;newnews.NewsImageSmall=newsContent.Img;newnews.NewsImageBig=newsContent.Img;newnews.Reference=newsContent.Url;newnews.UserId="3";newnews.Visible="是";newnews.ViewCounter=0;newsRepository.Add(newnews);如果(newsRepository.FindBy(i=>i.Reference==newsContent.Url).Count()==0)newsRepository.Save();}}}}解析函数:publicasyncTaskparsing(stringnewsArchive){ListlstResult=newList();}尝试{HttpClienthttp=newHttpClient();varresponse=awaithttp.GetByteArrayAsync(newsArchive);Stringsource=Encoding.GetEncoding("utf-8").GetString(response,0,response.Length-1);来源=WebUtility.HtmlDecode(来源);HtmlDocumentresultat=newHtmlDocument();resultat.LoadHtml(来源);列出toftitle=resultat.DocumentNode.Descendants().Where(x=>(x.Name=="div"&&x.Attributes["class"]!=null&&x.Attributes["class"].Value.Contains("主列")).ToList();varli=toftitle[0].Descendants().Where(x=>(x.Name=="div"&&x.Attributes["class"]!=null&&x.Attributes["class"].Value.Contains("base-boxblog-post")).ToList();foreach(variteminli){NewsContentnewsContent=newNewsContent();newsContent.Url=item.Descendants("a").ToList()[0].GetAttributeValue("href",null);newsContent.Img=item.Descendants("img").ToList()[0].GetAttributeValue("src",null);newsContent.Title=item.Descendants("h2").ToList()[0].InnerText;newsContent.abs=item.Descendants("p").ToList()[0].InnerText;//查找主要新闻内容varresponse1=awaithttp.GetByteArrayAsync(newsContent.Url);Stringsource1=Encoding.GetEncoding("utf-8").GetString(response1,0,response1.Length-1);source1=WebUtility.HtmlDecode(source1);HtmlDocumentresultat1=newHtmlDocument();resultat1.LoadHtml(source1);HtmlNodedoc=resultat1.DocumentNode.SelectSingleNode("//div[@class='entry-content']");HtmlNodenode=doc.SelectSingleNode("//div[@class='yasr-visitor-votes']");如果(节点!=null)node.ParentNode.RemoveChild(节点);HtmlNodenode1=doc.SelectSingleNode("//div[@class='post-tags']");如果(node1!=null)node1.ParentNode.RemoveChild(node1);HtmlNodenode2=doc.SelectSingleNode("//div[@class='mom-social-sharess-horizo??ntalborder-box']");如果(node2!=null)node2.ParentNode.RemoveChild(node2);HtmlNodenode3=doc.SelectSingleNode("//脚本|//样式");如果(node3!=null)node3.ParentNode.RemoveChild(node3);newsContent.Content=doc.InnerHtml;lstResult.Add(新闻内容);}}catch(Exceptione){}返回lstResult;}调度以这段代码开始:publicclassJobScheduler{publicstaticvoidStart(){ISchedulerscheduler=StdSchedulerFactory.GetDefaultScheduler();调度器.Start();IJobDetailjob=JobBuilder.Create().Build();ITrigger触发器=TriggerBuilder.Create().StartNow().WithDailyTimeIntervalSchedule(s=>s.WithIntervalInHours(24).OnEveryDay().StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0,0))).Build();scheduler.ScheduleJob(作业,触发器);有几个问题:当我运行这段代码时,代码永远不会执行,因为ITriggertrigger=TriggerBuilder.Create().StartNow()所以我必须等待24小时才能看到结果,为什么?第二个问题,当我将代码上传到网络服务器时,它不起作用为什么?尝试将您的方法更新为如下所示:调度器.Start();IJobDetailjob=JobBuilder.Create().Build();ITriggertrigger=TriggerBuilder.Create().WithIdentity("trigger1","group1").StartNow().WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(0,0).WithMisfireHandlingInstructionFireAndProceed()//MISFIRE_INSTRUCTION_FIREAndProceed()//MISFIRE_INSTRUCTION_FIRE_NOW(GT时间"))//(GMT+02:00)//https://alexandrebrisebois.wordpress.com/2013/01/20/using-quartz-net-to-schedule-jobs-in-windows-azure-worker-roles/).Build();scheduler.ScheduleJob(job,trigger);}}关于第二个问题,问题出在IIS而不是调度程序Quartz.NET、Hangfire等。另一方面,有很多解决方法发布在web上,但只有其中一些可以正常工作。在我看来,无需应用大量配置设置。只需在发布应用程序的服务器上安装IIS6.0/7.5的KeepAliveService即可。然后,在应用程序池回收、IIS/应用程序重启等之后,您发布的应用程序将处于活动状态更新:这是我在IIS上使用了几个月没有任何问题的完整工作代码。另一方面,对于基于IIS的触发问题,请查看我对Quartz.netSchedulerdoesn'tfirejobs/triggersoncedeployed的回答。Global.asax:protectedvoidApplication_Start(){JobScheduler.Start();}EmailJob.cs:使用Quartz;publicclassEmailJob:IJob{publicvoidExecute(IJobExecutionContextcontext){SendEmail();}}JobScheduler.cs:以上是C#学习教程:现在开始执行Quartz计划有远见,每隔24小时分享所有内容。如果对大家有用,需要详细了解C#学习教程,希望大家多多关注——usingQuartz;使用Quartz.Impl;公共类JobScheduler{publicstaticvoidStart(){ISchedulerscheduler=StdSchedulerFactory.GetDefaultScheduler();调度器.Start();IJobDetailjob=JobBuilder.Create().Build();ITrigger触发器=TriggerBuilder.Create().WithIdentity("trigger1","group1")//.StartAt(newDateTime(2015,12,21,17,19,0,0)).StartNow().WithSchedule(CronScheduleBuilder.WeeklyOnDayAndHourAndMinute(DayOfWeek.Monday,10,00)//.WithMisfireHandlingInstructionDoNothing()//如果未触发则不触发TimeZoneById("GTB标准时间"))//(GMT+02:00)).Build();scheduler.ScheduleJob(作业,触发器);}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
