關於Quartz.NET
Quartz.NET是一個.net平台下的開源作業調度架構,它最先是從Java平台的Quartz移植過來的。官方網站是http://quartznet.sourceforge.net/,目前最新版本為1.0.3。它可應用於WinForm程式及asp.net程式。作業調度可以理解為計劃任務,例如我們工作日內每天9:00打卡,每月10號發工資,類似這種需要自動執行且無須幹預的任務,Quartz.NET是最佳應用環境,它既可以滿足類似每天打卡的簡單應用情境,也能實現複雜作業與觸發器對應,又由於它採用Regex的方式匹配時間間隔來定義作業調度時間,因此應用複雜時間間隔情境也相對簡單。
需求分析
根據條件定時發送郵件給註冊使用者,是網站應用程式經常要使用的功能,使用web方式在大資料情況下容易出現瀏覽器死掉以及伺服器逾時響應的情形,這時使用Windows服務是相對更適合這種情況。結合Quartz.NET將對程式穩定性。
實現
這裡我們使用的是Windows服務的方式來實現該應用,首先建立項目並添加Quartz.NET引用,注意這裡是建立Windows服務類型項目,重合名主檔案為ServicesMain.cs,現在在ServicesMain.cs中的onStart和onStop先不寫代碼。然後在視圖中右擊添加服務安裝類,系統為自動建立相應的安裝檔案ProjectInstaller.cs。在這個檔案中右鍵安裝檔案類屬性,可以設定它的一些屬性。
建立Quartz.NET的設定檔job.xml:
<?xml version="1.0" encoding="utf-8" ?>
<quartz xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" overwrite-existing-jobs="true">
<job>
<job-detail>
<name>SendMailJob</name>
<group>JobGroup</group>
<job-type>BatchSendMail.ServiceInvokeMain,BatchSendMail</job-type>
</job-detail>
<trigger>
<cron>
<name>SendMailJobTrigger</name>
<group>TriggerGroup</group>
<job-name>SendMailJob</job-name>
<job-group>JobGroup</job-group>
<start-time>2008-01-01T00:00:00</start-time>
<cron-expression>0/10 * * ? * *</cron-expression>
</cron>
</trigger>
</job>
</quartz>
關於其詳細介紹,可以參考官方文檔,這裡需要注意的是<cron-expression>這一節,它分為6個部分,依次代表秒、分、時、日、月、年;可以使用萬用字元來表示,比如*代表任意值,?表示不指定特殊的值。-用來表示一個範圍,如10-12用在month中表示10月到12月。我們這裡的代表每天每隔10秒鐘開始執行該服務。
建立發送郵件類:
public class SendMailClass { public string FromAddr {get;set; } public string FromUser { get; set; } public string Host { get; set; } public string LoginUser { get; set; } public string Password { get; set; } public string[] ToAddr { get; set; } public string Subject { get; set; } public string Body { get; set; } public bool IsBodyHtml { get; set; } public SendMailClass() { } public SendMailClass(string fromAddr, string fromUser, string host, string loginUser, string password, string[] toAddress, string strSubject, string strBody, bool isBodyHtml) { FromAddr = fromAddr; FromUser = fromUser; Host = host; LoginUser = loginUser; Password = password; ToAddr = toAddress; Subject = strSubject; Body = strBody; IsBodyHtml = isBodyHtml; } //發送郵件 public static string Send(SendMailClass mail) { SmtpClient smtpClient = new SmtpClient(); MailMessage message = new MailMessage(); string result = ""; try { MailAddress fromAddress = new MailAddress(mail.FromAddr, mail.FromUser); smtpClient.Host =mail.Host; smtpClient.Port = 25; message.From = fromAddress; foreach (string addr in mail.ToAddr) { message.To.Add(addr); } message.IsBodyHtml = mail.IsBodyHtml; message.Body = mail.Body; smtpClient.Send(message); MessageBox.Show("發送成功"); result = "1"; } catch (Exception ex) { MessageBox.Show(ex.Message); result = ex.Message; } return result; } }
可以將郵件賬戶統一儲存在xml檔案中,也可以寫死在代碼裡。
主程式執行的代碼:
public static void Start() { SendMailClass mail=new SendMailClass(); //.... SendMailClass.Send(mail); }
回到ServicesMain.cs中添加onStart()方法:
protected override void OnStart(string[] args){ ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new ServiceMain() }; ServiceBase.Run(ServicesToRun);}
然後編譯項目,產生相應的exe檔案,為了測試程式是否正常,我們先在自己電腦裡安裝這個伺服器測試一下。在exe檔案同目錄下建立兩個批次檔用於安裝和卸載服務。
安裝服務.bat
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe GoalBatchSendMail.exePAUSE
卸載服務.bat
@echo offC:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u GoalBatchSendMail.exePAUSE
運行安裝服務的批處理
開啟電腦服務,看到我們新安裝的服務: