Topshelf+quartz.net implementation of timing scheduling based on window service

Source: Internet
Author: User

Because the scheduled schedule for Quartz.net on the publishing site is not performed by IIS recycle, the workaround is to write the Windows service-case column "Delete domain names that expire 35 days 8 o'clock in the morning every day"

All 30% architectures: Download the Quart class Library and the Topshelf class library from NuGet into a program Management Pack

1. Create a scheduled Schedule Quartz class

1   classQuartzservicerunner2     {3         Private ReadOnlyIScheduler Scheduler;4       5          PublicQuartzservicerunner ()6         {7Scheduler =Stdschedulerfactory.getdefaultscheduler ();8         }9 Ten          Public voidStart () One         { A             //read the task start time from the configuration file -             stringcronexpr = configurationmanager.appsettings["cronexpr"]; -Ijobdetail job = jobbuilder.create<deletedomainjob> (). Withidentity ("Job1","group1"). Build (); the             //create a trigger for a task to run -Itrigger trigger =triggerbuilder.create () -. Withidentity ("Triggger1","group1") -. Withschedule (Cronschedulebuilder.cronschedule (Newcronexpression (cronexpr))) +                 . Build (); -             //Start a task + Scheduler. Schedulejob (Job, trigger); A Scheduler. Start (); at Scheduler. Start (); -              -         } -  -          Public voidStop () -         { in Scheduler. Clear (); -         } to  +          Public BOOLContinue (Hostcontrol hostcontrol) -         { the Scheduler. Resumeall (); *             return true; $         }Panax Notoginseng  -          Public BOOLPause (Hostcontrol hostcontrol) the         { + Scheduler. Pauseall (); A             return true; the         } +  -}

2. Create a dispatch class that executes a delete method from a database query that has an expired domain name to inherit the Ijob interface

classDeletedomainjob:ijob {ReadOnlyILog _log = Logmanager.getlogger (typeof(Deletedomainjob));  Public voidExecute (Ijobexecutioncontext context) {_log.infoformat ("Domain deletion dispatch starts ******"); DomainService Service=NewDomainService (); intOutday = Convert.ToInt16 (configurationmanager.appsettings["Outday"]); List<DeleteDoaminModel> list =service.            Querydeletedomain (Outday); StringBuilder SB=NewStringBuilder (); foreach(varList_domaininchlist) {                stringstr =""; BOOLFlag = Deletedomainservice.delete (list_domain. Domain,refstr); _log.infoformat ("the domain name to delete ="+ List_domain. Domain +"Expiry time ="+ List_domain. Enddateup +"Order number ="+ List_domain. Domainorderid +"Success Sign ="+ Flag +"Return information ="+str); if(flag) {sb. Append ("'"+ List_domain. Domain +"',"); }            }            stringStr_domain = sb. ToString (). TrimEnd (','); if(Str_domain. Length >0)            {                BOOLFlag =service.                Logic_delete (Str_domain); _log.infoformat ("database Delete domain name = ="+ Str_domain +"Delete flag = ="+flag); }        }    }

3. Main function entry

 classProgram { Public Static voidMain (string[] args) {            varLogcfg =NewFileInfo (AppDomain.CurrentDomain.BaseDirectory +"Log4net.config");                      Xmlconfigurator.configureandwatch (LOGCFG); Hostfactory.run (x={X.service<QuartzServiceRunner> (s ={s.constructusing (name=NewQuartzservicerunner ()); s.whenstarted (TC=TC.                    Start ()); s.whenstopped (TC=TC.                Stop ());                });                X.runaslocalsystem ();                X.startautomatically (); X.setdescription ("Creating cloud Scheduling Services"); X.setdisplayname ("Creating cloud Scheduling Services"); X.setservicename ("Creating cloud Scheduling Services");        }); }    }

4. Build a Windows Service

Build your project release to find the folder where the solution resides

Locate ***windowservice.exe; Copy the current path g:\topshelfdemo\topshelfdemo\bin\release\****. WindowService.exe

Run the command Prompt window as an administrator

Install complete Find Administrative Tools service program to see

Topshelf+quartz.net implementation of timing scheduling based on window service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.