Topshelf + Quartz2.5 Create a Windows-based service

Source: Internet
Author: User

1. Create a scheduled Schedule Quartz class

1 usingQuartz;2 usingQuartz.impl;3 usingSystem;4 usingSystem.Collections.Generic;5 usingSystem.Linq;6 usingSystem.Text;7 usingSystem.Threading.Tasks;8 usingtopshelf;9 Ten  One namespacetopshelfsolution A { -     -      Public Sealed classQuartzservicerunner:servicecontrol,servicesuspend the     { -         Private ReadOnlyIScheduler Scheduler; -  -         PublicQuartzservicerunner () +        { -Scheduler =Stdschedulerfactory.getdefaultscheduler (); +        } A  at         Public BOOLStart (Hostcontrol hostcontrol) -        { - Scheduler. Start (); -            return true; -        } -  in         Public BOOLStop (Hostcontrol hostcontrol) -        { toScheduler. Shutdown (false); +            return true; -        } the  *         Public BOOLContinue (Hostcontrol hostcontrol) $        {Panax Notoginseng Scheduler. Resumeall (); -            return true; the        } +  A         Public BOOLPause (Hostcontrol hostcontrol) the        { + Scheduler. Pauseall (); -            return true; $        } $  -     } -}
View Code

2. Create a class to handle business logic

1 usingQuartz;2 usingSystem;3 usingSystem.Collections.Generic;4 usingSystem.Linq;5 usingSystem.Text;6 usingSystem.Threading.Tasks;7 8 9 Ten namespaceTopshelfsolution.quartzjobs One { A      Public Sealed classQuartzjobfortest:ijob -     { -          Public voidExecute (ijobexecutioncontext context) the         { -            //working with business logic -Console.WriteLine (DateTime.Now.ToString ("yyyymmddhhmmssfff")); -            +         } -     } +}
View Code

3. Create a Quartz.config configuration file

# You can configure your schedulerincheither <quartz>configuration section # orinchQuartz Properties File # Configuration section have precedence quartz.scheduler.instanceName=Serverscheduler # Configure thread pool info quartz.threadPool.type=Quartz.Simpl.SimpleThreadPool, Quartz quartz.threadPool.threadCount=Tenquartz.threadPool.threadPriority=Normal # Job Initialization plugin handles our XML reading, without it defaults is used Quartz.plugin.xml.type /c4>=Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz quartz.plugin.xml.fileNames= ~/Quartz_jobs.xml # Export Thisserver to Remoting context Quartz.scheduler.exporter.type=Quartz.Simpl.RemotingSchedulerExporter, Quartz quartz.scheduler.exporter.port=555Quartz.scheduler.exporter.bindName=Quartzscheduler Quartz.scheduler.exporter.channelType=TCP Quartz.scheduler.exporter.channelName= Httpquartz
View Code

4. Create the configuration information XML file for the timed job

<?xml version="1.0"encoding="UTF-8"? ><job-scheduling-data xmlns="Http://quartznet.sourceforge.net/JobSchedulingData"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.0"> <processing-directives> <overwrite-existing-data>true</overwrite-existing-data> </processing-directives><schedule> <job> <name> quartzjobfortestjob</name> <group>TestJobGroup</group> <description>test_sample Job forQuartz</description> <job-type>topshelfsolution.quartzjobs.quartzjobfortest, TopShelfSolution</ Job-type> <durable>true</durable> <recover>false</recover> </job> <trigger> <cron> <name>QuartzJobForTestJobTrigger</name> <group>TestJobTriggerGroup</group> <job-name>QuartzJobForTestJob</job-name> <job- Group>testjobgroup</job-group> <!--starting from Start-time, 0 seconds, every 1/5 seconds ijob.execute--> &LT;START-TIME&G T2018- on-22t00:xx:xx+ ,:xx</start-time> <cron-expression>0/2* * * *?</cron-expression> </cron> </trigger> </schedule></job-scheduling-data>
View Code

5. Main function entry

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading;6 usingSystem.Threading.Tasks;7 usingsystem.timers;8 usingtopshelf;9 Ten namespacetopshelfsolution One { A     class Program -     { -         Static voidMain (string[] args) the         { -Hostfactory.run (U = -             { -U.service<quartzservicerunner>(); + U.runaslocalsystem (); -  +U.setdescription ("description of the Sample topshelf host service"); AU.setdisplayname ("Stuff Display Name"); atU.setservicename ("Stuff Service Name"); -  -                 //new Ttask (). Start (); -             }); -         } -     } in}
View Code

Note: Each time you finish modifying the Quartz_jobs.xml file, you need to rebuild the project and copy the Quartz_jobs.xml to the current project Bin\release directory.

Topshelf + Quartz2.5 Create a Windows-based service

Related Article

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.