Lingkungan :MS Visual Studio .Net 2010 (C#)
Instalasi
Instalasi dengan perintah install-package Quartz pada Nuget, install-package log4net, hal ini akan menambahkan Referensi Quartz dan log4net pada Solution.
Job
Buat sebuah folder untuk menyimpan Job, beri nama folder tersebut Job. Di dalam folder Job, buatlah sebuah Class baru, beri nama Job1 (file Job1.cs). Class Job1 harus mengimplementasi Interface IJob.
Interface Ijob memiliki satu method bernama Execute, yang bertipe void. Buat job dalam method ini, misalnya insert ke dalam database. Contoh:
Contoh:
using Quartz;
using QuartzSchedul.Models;
namespace QuartzSchedul.Job
{
public class Job1:IJob
{
public void Execute(JobExecutionContext context)
{
Tes tes1 = new Tes { Name = "Tes1" };
DataContext dcontext = new DataContext();
dcontext.Tes.Add(tes1);
dcontext.SaveChanges();
}
}
}
Konfigurasi
Pada Web.Config, tambahkan key sebagai berikut:
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
<section name="quartz" type="System.Configuration.NameValuesSectionHandler, System, Version=1.0.5000.0, Culture=neutral, Public Key Token=b77a5c561934e089" />
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<quartz>
<add key="quartz.scheduler.instanceName" value="QuartzScheduler" />
<add key="quartz.threadPool.threadCount" value="10" />
<add key="quartz.threadPool.threadPolicy" value="2" />
<add key="quartz.jobStore.misfireThreshold" value="6000" />
<add key="quartz.jobStore.type" value="Quartz.simpl.RAMJobStore,Quartz" />
</quartz>
<common>
<logging>
<factoryAdapter type="Common.Logging.Simple.ConsoleOutLoggerFactoryAdapter,CommonLogging">
<arg key="showLogName" value="true" />
<arg key="showDataName" value="true" />
<arg key="level" value="DEBUG" />
<arg key="dateFormat" value="HH:mm:ss:fff" />
</factoryAdapter>
</logging>
</common>
<add key=”job1” value=”5 * * * * ?”> dalam <AppSettings>, sehingga kurang lebih menjadi seperti ini:
<configuration>
<appSettings>
<add key="webpages:Version" value="1.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="job1" value="1 * * * * ?" />
</appSettings>
Pada Global.asax, tambahkan scheduler dan daftarkan Job yang telah dibuat pada scheduler.
IScheduler scheduler = null;
ISchedulerFactory factory = new StdSchedulerFactory();
scheduler = factory.GetScheduler();
JobDetail job1 = new JobDetail("job1","jobGroup",typeof(Job1));
string cronjob1 = ConfigurationManager.AppSettings["job1"];
CronTrigger trigger = new CronTrigger("trigger", "triggerGroup", "job", "jobGroup", cronjob1);
scheduler.AddJob(job1,true);
scheduler.ScheduleJob(trigger);