跳過到頁腳內容
.NET幫助

Quartz .NET (對開發者如何運作)

Quartz.NET 簡介

對於 .NET 應用程式,Quartz.NET 是廣受歡迎的開放原始碼任務排程工具組。 它為程式設計師打下了堅實的基礎,讓他們可以在預定的期間、間隔,或回應觸發條件時,規劃並執行工作、排程或流程。 Quartz.NET 使 .NET 應用程式更容易建構複雜的排程情境,無論是傳送通知、排程工作、產生報表或完成定期維護活動。 Quartz.NET 是完美的工作排程系統,適用於建立從小型應用程式到大型企業系統。

Quartz.NET 的最新排程版本可與各種支援的資料庫供應商進行無縫整合,讓開發人員能夠使用方便的擴充方法來擴充其功能,同時確保作為託管服務的任務執行的可靠性。

透過將 Quartz.NET 與 IronPDF for .NET 整合,可在 .NET 應用程式中建立具有強大 PDF 製作能力的可靠排程系統。 雖然 IronPDF 提供了一整套工具來建立、修改和顯示 PDF 文件,Quartz.NET 則提供了多功能且可靠的排程系統。 當這些工具結合起來時,就能讓開發人員自動化 PDF 製作作業,作為工作流程的一部分或排程背景工作,從而提高應用程式的實用性和功能性。

主要功能

彈性排程

Quartz.NET 可讓程式設計師建立排程,告訴程式在預定的間隔或時間執行(例如每小時、每天、每 30 分鐘)。 它與複雜的排程模式相容,例如 cron 表達式,可精確控制代碼、工作和服務的執行時間。

基於觸發的執行

在 Quartz.NET 中,工作可以由多種觸發器啟動。 這些工具包括基於日曆的觸發器(例如,不包括週末)、簡單觸發器(根據設定的時間表運作)、工作觸發器,以及依賴於外部情況或事件的定制觸發器。

工作持久性

Quartz.NET 具備以持續方式排程工作的能力,可將計劃中的工作及其完成記錄儲存於資料庫中。 保證工作排程在應用程式故障或重新啟動時的彈性,並使工作集群的高可用性和可擴展性成為可能。

並行控制

為了保證任務安全有效地完成,Quartz.NET 內建了並行控制工具。 為了控制作業執行的並行性,開發人員可以設定執行緒池、作業優先順序和執行限制。

工作鏈和依賴管理

由於 Quartz.NET 支援工作鏈和依賴性管理,因此工作可以按照指定的順序執行,而工作之間的關係也可以由開發人員定義。 這使得為背景工作、服務和程序建立複雜的協調情況成為可能。

錯誤處理與重試技巧

為了優雅地管理失敗,Quartz.NET 具有錯誤處理和重試技術。 在發生暫時故障或異常的情況下,開發人員可以設定重試原則和錯誤處理技術,以保證工作可以重試或重新排程。

聚類與可擴展性

由於 Quartz.NET 支援分散式作業排程中的群集,因此排程器的多個實體可以在伺服器群集中協調並執行任務。 這可透過實現水平擴充性和容錯,在分散式情境中保證可靠的工作排程和執行。

與 .NET Ecosystem 整合。

Quartz.NET 可輕鬆地與 .NET 生態系統銜接,其中包括訊息傳送系統 (Rebus.NET、MassTransit)、日誌框架 (Serilog、NLog) 以及流行的依賴注入框架 (Autofac、Microsoft.Extensions.DependencyInjection)。

建立與設定 Quartz.NET

定義工作和觸發器

針對您希望執行的任務,建立工作和觸發器。 要完成的任務由上下文物件和工作類別表示,工作執行的頻率和時間則由觸發器決定。

using Quartz;

// Define a job by implementing the IJob interface
public class MyJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        // Implement the logic for your job here
    }
}

// Build the job instance using JobBuilder
var job = JobBuilder.Create<MyJob>()
    .WithIdentity("myJob", "group1") // Assign a unique name and group to the job
    .Build();

// Create a trigger to define when the job should be executed
var trigger = TriggerBuilder.Create()
    .WithIdentity("myTrigger", "group1") // Assign a unique name and group to the trigger
    .WithCronSchedule("0 0/5 * * * ?") // Run every 5 minutes based on the cron expression
    .Build();
using Quartz;

// Define a job by implementing the IJob interface
public class MyJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        // Implement the logic for your job here
    }
}

// Build the job instance using JobBuilder
var job = JobBuilder.Create<MyJob>()
    .WithIdentity("myJob", "group1") // Assign a unique name and group to the job
    .Build();

// Create a trigger to define when the job should be executed
var trigger = TriggerBuilder.Create()
    .WithIdentity("myTrigger", "group1") // Assign a unique name and group to the trigger
    .WithCronSchedule("0 0/5 * * * ?") // Run every 5 minutes based on the cron expression
    .Build();
Imports Quartz

' Define a job by implementing the IJob interface
Public Class MyJob
	Implements IJob

	Public Async Function Execute(ByVal context As IJobExecutionContext) As Task
		' Implement the logic for your job here
	End Function
End Class

' Build the job instance using JobBuilder
Private job = JobBuilder.Create(Of MyJob)().WithIdentity("myJob", "group1").Build()

' Create a trigger to define when the job should be executed
Private trigger = TriggerBuilder.Create().WithIdentity("myTrigger", "group1").WithCronSchedule("0 0/5 * * * ?").Build()
$vbLabelText   $csharpLabel

設定與初始化排程

使用指定的組態設定工作排程器以排程背景工作、工作和觸發器後,啟動排程器開始規劃和執行工作。

using Quartz;
using Quartz.Impl;

// Create a scheduler factory and get a scheduler instance
var schedulerFactory = new StdSchedulerFactory();
var scheduler = await schedulerFactory.GetScheduler(); 

// Start the scheduler
await scheduler.Start();

// Schedule the job with its corresponding trigger
await scheduler.ScheduleJob(job, trigger);
using Quartz;
using Quartz.Impl;

// Create a scheduler factory and get a scheduler instance
var schedulerFactory = new StdSchedulerFactory();
var scheduler = await schedulerFactory.GetScheduler(); 

// Start the scheduler
await scheduler.Start();

// Schedule the job with its corresponding trigger
await scheduler.ScheduleJob(job, trigger);
Imports Quartz
Imports Quartz.Impl

' Create a scheduler factory and get a scheduler instance
Private schedulerFactory = New StdSchedulerFactory()
Private scheduler = await schedulerFactory.GetScheduler()

' Start the scheduler
Await scheduler.Start()

' Schedule the job with its corresponding trigger
Await scheduler.ScheduleJob(job, trigger)
$vbLabelText   $csharpLabel

工作持久性

設定 Quartz.NET,將工作和觸發元資料儲存於持久性儲存庫中,就像資料庫一樣。 這可確保可靠性,並允許工作在應用程式重新啟動後仍能繼續執行。

錯誤處理

將錯誤處理和重試邏輯納入工作執行邏輯,以優雅的方式處理失敗。 Quartz.NET 提供內建的方法來管理異常和重試任務。

聚類

在分散式環境中使用 Quartz.NET 時,若要保證高可用性和可擴展性,請設定集群。 由於群集的緣故,排程器實例可以在伺服器群集之間協同執行任務。

依賴注入

如果您的應用程式使用依賴注入框架,請設定 Quartz.NET 與您的依賴注入 (DI) 容器通訊,以管理作業依賴、組態和生命週期。

IronPDF

Quartz .NET (How It Works For Developers):圖 1 - IronPDF 網頁

IronPDF for .NET 這個著名的 .NET 套件讓在 .NET 程式中建立、修改和渲染 PDF 文件成為可能。 與 PDF 互動的功能眾多:從 HTML 內容、照片或未處理的資料建立 PDF; 將文字、影像和圖形附加到預先存在的 PDF 文件; 將 HTML 頁面轉換為 PDF; 以及從 PDF 中萃取文字和影像。

IronPDF 擅長於 HTML 至 PDF 的轉換,可確保精確保留原始版面與樣式。 它非常適合從網頁內容(如報告、發票和文件)建立 PDF。 IronPDF 支援 HTML 檔案、URL 和原始 HTML 字串,可輕鬆製作高品質的 PDF 文件。

using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert an HTML string to a PDF document
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert an HTML file to a PDF document
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert a URL to a PDF document
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
using IronPdf;

class Program
{
    static void Main(string[] args)
    {
        var renderer = new ChromePdfRenderer();

        // 1. Convert an HTML string to a PDF document
        var htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>";
        var pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent);
        pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf");

        // 2. Convert an HTML file to a PDF document
        var htmlFilePath = "path_to_your_html_file.html"; // Specify the path to your HTML file
        var pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath);
        pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf");

        // 3. Convert a URL to a PDF document
        var url = "http://ironpdf.com"; // Specify the URL
        var pdfFromUrl = renderer.RenderUrlAsPdf(url);
        pdfFromUrl.SaveAs("URLToPDF.pdf");
    }
}
Imports IronPdf

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim renderer = New ChromePdfRenderer()

		' 1. Convert an HTML string to a PDF document
		Dim htmlContent = "<h1>Hello, IronPDF!</h1><p>This is a PDF from an HTML string.</p>"
		Dim pdfFromHtmlString = renderer.RenderHtmlAsPdf(htmlContent)
		pdfFromHtmlString.SaveAs("HTMLStringToPDF.pdf")

		' 2. Convert an HTML file to a PDF document
		Dim htmlFilePath = "path_to_your_html_file.html" ' Specify the path to your HTML file
		Dim pdfFromHtmlFile = renderer.RenderHtmlFileAsPdf(htmlFilePath)
		pdfFromHtmlFile.SaveAs("HTMLFileToPDF.pdf")

		' 3. Convert a URL to a PDF document
		Dim url = "http://ironpdf.com" ' Specify the URL
		Dim pdfFromUrl = renderer.RenderUrlAsPdf(url)
		pdfFromUrl.SaveAs("URLToPDF.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

IronPDF 的簡易性和易用性是其兩大優點。 開發人員可以利用其友善的 API 和廣泛的說明文件,輕鬆地在其 .NET 專案中開始產生 PDF。 IronPDF 的速度和效率是其進一步的特點,可讓開發人員快速有效地製作高品質的 PDF 文件。

IronPDF 核心功能

  • 從原始資料、HTML 和圖片建立 PDF。
  • 從 PDF 檔案中萃取文字和影像。
  • 允許您在 PDF 檔案中加入頁眉、頁腳和水印。
  • 建立具有密碼和加密安全性的 PDF 文件。
  • 提供填寫表格和數位簽名的工具。

使用 IronPDF 中的 Quartz 。

要開始在控制台或 ASP.NET Core 應用程式中使用 IronPDF 與 Quartz.NET,您可以使用 IronPDF 建立排程的背景工作,執行與 PDF 製作相關的任務。

安裝 Quartz 和 IronPDF 套件。

首先,確保您已透過 Visual Studio 套件管理員控制台,使用下列指令在 .NET 專案中安裝 IronPDF 和 Quartz.NET 所需的 NuGet 套件:

Install-Package Quartz
Install-Package IronPdf
Install-Package Quartz
Install-Package IronPdf
SHELL

程式碼範例

本節示範如何製作使用 IronPDF 建立 PDF 文件的 Quartz 作業。

using Quartz;
using IronPdf;

// Implementing a job that generates a PDF using IronPDF
public class PdfGenerationJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        // Generating PDF using IronPDF
        var htmlContent = "<h1>Hello, IronPDF!</h1>";
        var pdfRenderer = new HtmlToPdf();
        var pdfDocument = pdfRenderer.RenderHtmlAsPdf(htmlContent);

        // Save the generated PDF to a file
        pdfDocument.SaveAs("output.pdf");
    }
}
using Quartz;
using IronPdf;

// Implementing a job that generates a PDF using IronPDF
public class PdfGenerationJob : IJob
{
    public async Task Execute(IJobExecutionContext context)
    {
        // Generating PDF using IronPDF
        var htmlContent = "<h1>Hello, IronPDF!</h1>";
        var pdfRenderer = new HtmlToPdf();
        var pdfDocument = pdfRenderer.RenderHtmlAsPdf(htmlContent);

        // Save the generated PDF to a file
        pdfDocument.SaveAs("output.pdf");
    }
}
Imports Quartz
Imports IronPdf

' Implementing a job that generates a PDF using IronPDF
Public Class PdfGenerationJob
	Implements IJob

	Public Async Function Execute(ByVal context As IJobExecutionContext) As Task
		' Generating PDF using IronPDF
		Dim htmlContent = "<h1>Hello, IronPDF!</h1>"
		Dim pdfRenderer = New HtmlToPdf()
		Dim pdfDocument = pdfRenderer.RenderHtmlAsPdf(htmlContent)

		' Save the generated PDF to a file
		pdfDocument.SaveAs("output.pdf")
	End Function
End Class
$vbLabelText   $csharpLabel

設定 Quartz.NET 以利用觸發器在預定的排程中執行 PDF 建立程序。

// Create and configure the Quartz scheduler
var schedulerFactory = new StdSchedulerFactory();
var scheduler = await schedulerFactory.GetScheduler();
await scheduler.Start();

// Define the job and bind it to our PdfGenerationJob class
var job = JobBuilder.Create<PdfGenerationJob>()
    .WithIdentity("pdfGenerationJob", "pdfGenerationGroup")
    .Build();

// Define a trigger to schedule the PDF generation job
var trigger = TriggerBuilder.Create()
    .WithIdentity("pdfGenerationTrigger", "pdfGenerationGroup")
    .WithSimpleSchedule(x => x
        .WithIntervalInMinutes(30) // Run every 30 minutes
        .RepeatForever())
    .Build();

// Schedule the job using the scheduler
await scheduler.ScheduleJob(job, trigger);
// Create and configure the Quartz scheduler
var schedulerFactory = new StdSchedulerFactory();
var scheduler = await schedulerFactory.GetScheduler();
await scheduler.Start();

// Define the job and bind it to our PdfGenerationJob class
var job = JobBuilder.Create<PdfGenerationJob>()
    .WithIdentity("pdfGenerationJob", "pdfGenerationGroup")
    .Build();

// Define a trigger to schedule the PDF generation job
var trigger = TriggerBuilder.Create()
    .WithIdentity("pdfGenerationTrigger", "pdfGenerationGroup")
    .WithSimpleSchedule(x => x
        .WithIntervalInMinutes(30) // Run every 30 minutes
        .RepeatForever())
    .Build();

// Schedule the job using the scheduler
await scheduler.ScheduleJob(job, trigger);
' Create and configure the Quartz scheduler
Dim schedulerFactory = New StdSchedulerFactory()
Dim scheduler = Await schedulerFactory.GetScheduler()
Await scheduler.Start()

' Define the job and bind it to our PdfGenerationJob class
Dim job = JobBuilder.Create(Of PdfGenerationJob)().WithIdentity("pdfGenerationJob", "pdfGenerationGroup").Build()

' Define a trigger to schedule the PDF generation job
Dim trigger = TriggerBuilder.Create().WithIdentity("pdfGenerationTrigger", "pdfGenerationGroup").WithSimpleSchedule(Function(x) x.WithIntervalInMinutes(30).RepeatForever()).Build()

' Schedule the job using the scheduler
Await scheduler.ScheduleJob(job, trigger)
$vbLabelText   $csharpLabel

要啟動 Quartz 排程器並開始執行計劃中的任務:

// Start the scheduler
await scheduler.Start();
// Optionally, monitor scheduler for job execution
// Start the scheduler
await scheduler.Start();
// Optionally, monitor scheduler for job execution
' Start the scheduler
Await scheduler.Start()
' Optionally, monitor scheduler for job execution
$vbLabelText   $csharpLabel

以下是上述程式碼產生的輸出。

Quartz .NET (How It Works For Developers):圖 2 - IronPDF 與 Quartz.NET 程式碼範例的輸出

結論

總而言之,Quartz.NET 與 IronPDF 的結合提供了一種強大的方式,可在 .NET 應用程式中自動執行與建立 PDF 相關的作業。 借助 Quartz.NET 功能強大且適應性強的排程系統,開發人員可以創建工作和觸發器,在預定的時間間隔或持續時間內執行活動。 另一方面,IronPDF 為開發人員提供了建立和處理 PDF 文件所需的所有工具。 開發人員可使用 HTML、圖形或原始資料,建立外觀專業的 PDF。

開發人員可透過整合 IronPDF 的 PDF 生成功能與 Quartz.NET 的排程功能,在分散式應用程式中自動執行典型的 PDF 生成作業,例如在預定的時間間隔或針對觸發器來建立報表、發票或文件。 此整合可簡化文件產生的工作流程、提高生產力並減少人工勞動,讓開發人員更輕鬆地製作並傳送高品質的 PDF 文件給客戶或客戶。

IronPDF 價格合理,若作為套件的一部分購買,則可獲得終身授權。 由於該套餐僅需 $999,即可為多個系統支付一次費用,因此具有極高的性價比。 它為授權持有人提供全天候的線上工程協助。 若要瞭解 Iron Software 所生產的產品的更多資訊,請造訪 Iron Software 網站上的 Iron Software 產品頁面。

常見問題解答

如何在 .NET 應用程式中自動生成 PDF?

您可以通過整合 Quartz.NET 與 IronPDF 自動生成 PDF。Quartz.NET 負責排程任務,而 IronPDF 允許您從 HTML、圖像或原始數據生成 PDF。

使用 Quartz.NET 排程任務的好處是什麼?

Quartz.NET 提供強大的排程任務框架,具有靈活的排程、基於觸發器的執行、任務持久性、並發控制、任務鏈接、錯誤處理、重試技術以及用於擴展性的集群功能。

IronPDF 如何簡化 .NET 中的 PDF 生成?

IronPDF 提供簡單易用的 API,將 HTML、圖像或原始數據轉換為 PDF 文件,適用於報告、發票和文件,保持原始佈局和樣式,確保高質量的輸出。

Quartz.NET 和 PDF 工具可以整合以增強工作流程自動化嗎?

可以,將 Quartz.NET 與像 IronPDF 這樣的 PDF 工具整合可以在 .NET 應用程式中排程和自動化 PDF 生成任務,提升效率和生產力。

什麼是 Quartz.NET 中的任務持久性,為什麼重要?

Quartz.NET 中的任務持久性是指能夠在資料庫中存儲排程的任務及其歷史,確保應用程式故障或重啟後的恢復能力。對於維護任務排程和實現任務集群至關重要。

如何在 .NET 應用程序中將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字串轉換為 PDF,或使用 RenderHtmlFileAsPdf 將 HTML 文件直接轉換為 PDF。

Quartz.NET 支持哪些類型的排程模式?

Quartz.NET 支持多種排程模式,包括 cron 表達式,這些表達式提供對執行時間的精確控制,並支持複雜的排程方案。

如何在我的 .NET 專案中安裝 Quartz.NET 和 IronPDF?

使用 Visual Studio 套件管理器控制台安裝 Quartz.NET 和 IronPDF,命令為:Install-Package QuartzInstall-Package IronPDF

IronPDF 用於 PDF 操作的核心功能是什麼?

IronPDF 提供如從 HTML、圖像或原始數據創建 PDF、提取文本和圖像、添加頁眉、頁腳和浮水印、以及密碼保護等安全選項。

Quartz.NET 和 IronPDF 的整合如何增強 .NET 應用程式?

整合 Quartz.NET 和 IronPDF 允許自動生成 PDF 並排程任務,簡化工作流程,提高 .NET 應用程式的生產力。

Jacob Mellor, Team Iron 首席技術官
首席技術官

Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。

Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。

他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我