The Windowservice of Quartz

Source: Internet
Author: User

(i) Create a service Quarzservice

Using System.ServiceProcess;
Using System.Text;

Using Quartz;
Using Quartz.impl;

Using WinNet.Log;
Namespace Quarzservice
{
public partial class Quartzservice:servicebase
{

Private IScheduler Scheduler;

Public Quartzservice ()
{
InitializeComponent ();

Ischedulerfactory schedulerfactory = new Stdschedulerfactory ();
Scheduler = Schedulerfactory.getscheduler ();


}

protected override void OnStart (string[] args)
{
Scheduler. Start ();

Writelog.logtxt ("Quartz service successfully started", "Scheduler");

}

protected override void OnStop ()
{
Scheduler. Shutdown ();
Writelog.logtxt ("Quartz service terminated successfully", "Scheduler");

}

protected override void OnPause ()
{
Scheduler. Pauseall ();

}

protected override void OnContinue ()
{
Scheduler. Resumeall ();
}
}
}

(ii) QUARTZ.CONFIG configuration

# can configure your scheduler in either <quartz> configuration sections
# or in Quartz properties file
# Configuration section has precedence

Quartz.scheduler.instanceName = Serverscheduler

# Configure thread Pool info
Quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, quartz
Quartz.threadPool.threadCount = 10
Quartz.threadPool.threadPriority = Normal

# Job Initialization plugin handles our XML reading, without it defaults is used
Quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, quartz
Quartz.plugin.xml.fileNames = ~/quartz_jobs.xml

# Export this server to remoting context
Quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, quartz
Quartz.scheduler.exporter.port = 555
Quartz.scheduler.exporter.bindName = Quartzscheduler
Quartz.scheduler.exporter.channelType = TCP
Quartz.scheduler.exporter.channelName = Httpquartz

Write Log method:

Using System.Text;
Using System.Windows.Forms;
Using System.IO;

Namespace WinNet.Log
{
public class Writelog
{
Public Writelog ()
{
}

public static void Logtxt (String Msg, string name)
{
String logPath = Path.getdirectoryname (Application.executablepath);
System.IO.StreamWriter SW = System.IO.File.AppendText (LogPath + "/" + name + "_" + DateTime.Now.ToString ("yyyy-mm-dd") + " . txt ");
Sw. WriteLine (DateTime.Now.ToString ("Yyyy-mm-dd HH:mm:ss") + MSG);
Sw. Close ();
Sw. Dispose ();
}
}
}

(iii) Creation of a task: Winnet.jobtask

Using Quartz;

Using WinNet.Log;

Namespace Winnet.jobtask
{
public class Jobgeneral:ijob
{
private static readonly Common.Logging.ILog logger = Common.Logging.LogManager.GetLogger ( System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);
#region IJob Members

public void Execute (Ijobexecutioncontext context)
{
Try
{

Writelog.logtxt ("DemoJob1 task starts running", "Scheduler");
for (int i = 0; i <; i++)
{
Writelog.logtxt ("DemoJob1 is running" + I, "scheduler");

}

Writelog.logtxt ("DemoJob1 Task Run End", "Scheduler");
}
catch (Exception ex)
{
Writelog.logtxt ("Error:" +ex. ToString (), "Scheduler");
}
}

#endregion
}
}

(iv ) installation and uninstallation of services

Installation Services, written as bat files

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe QuarzService.exe
Net Start Quarzservice
sc config quarzservice start= auto

Uninstalling the service, written as a bat file

%systemroot%\microsoft.net\framework\v4.0.30319\installutil.exe/u QuarzService.exe

Source code: HTTP//files. Cnblogs. Com/files/aran/serviceschedulerdesign.rar

The Windowservice of Quartz

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.