C# Quartz.net 的简单工作示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8821535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:38:26  来源:igfitidea点击:

Simple, working example of Quartz.net

c#.netquartz.net

提问by Tomer

I am looking for a working simple example of Quartz.net for Console application (it can be any other application as long as it simple enough...). And while I am there, is there any wrapper that could help me avoid implementing IJobDetail, ITrigger, etc.

我正在为控制台应用程序寻找一个简单的 Quartz.net 示例(它可以是任何其他应用程序,只要它足够简单......)。当我在那里时,是否有任何包装器可以帮助我避免实现 IJobDetail、ITrigger 等。

回答by Jason Meckley

between the documentation and samples in the source code there should be enough to get you started. the only interface you must implement is IJobwhen creating custom jobs. all other interfaces are either already implemented for you, or they are not required for basic usage in quartz.net.

在源代码中的文档和示例之间,应该足以让您入门。您必须实现的唯一接口是IJob在创建自定义作业时。所有其他接口要么已经为您实现,要么在quartz.net 中的基本使用不需要它们。

to build up jobs and triggers to use the JobBuilder and TriggerBuilder helper objects.

构建作业和触发器以使用 JobBuilder 和 TriggerBuilder 辅助对象。

回答by Bj?rn Otto Vasbotten

There is a guy who made the exact same observation as you, and he has published a blog post with a simple working example of a Quartz.net Console application.

有一个人与您进行了完全相同的观察,他发表了一篇博客文章,其中包含 Quartz.net 控制台应用程序的简单工作示例。

The following is a working Quartz.net example that is built against Quartz.net 2.0 (Latest). What this job does is write a text message, “Hello Job is executed” in the console every 5 sec.

以下是针对 Quartz.net 2.0(最新)构建的工作 Quartz.net 示例。该作业的作用是每 5 秒在控制台中写入一条文本消息“Hello Job is execution”。

Start a Visual Studio 2012 project. Select Windows Console Application. Name it Quartz1or what ever you like.

启动 Visual Studio 2012 项目。选择Windows Console Application。将其命名为Quartz1或您喜欢的任何名称。

RequirementsDownload Quartz.NETassembly using NuGet. Right click on project, select “Manage Nuget Packages”. Then search for Quartz.NET. Once found select and install.

要求下载Quartz.NET组装使用NuGet。右键单击项目,选择“管理 Nuget 包”。然后搜索Quartz.NET. 一旦找到选择并安装。

using System;
using System.Collections.Generic;
using Quartz;
using Quartz.Impl;

namespace Quartz1
{
    class Program
    {
        static void Main(string[] args)
        {
        // construct a scheduler factory
        ISchedulerFactory schedFact = new StdSchedulerFactory();

        // get a scheduler, start the schedular before triggers or anything else
        IScheduler sched = schedFact.GetScheduler();
        sched.Start();

        // create job
        IJobDetail job = JobBuilder.Create<SimpleJob>()
                .WithIdentity("job1", "group1")
                .Build();

        // create trigger
        ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
            .Build();

        // Schedule the job using the job and trigger 
        sched.ScheduleJob(job, trigger);

        }
    }

    /// <summary>
    /// SimpleJOb is just a class that implements IJOB interface. It implements just one method, Execute method
    /// </summary>
    public class SimpleJob : IJob
    {
        void IJob.Execute(IJobExecutionContext context)
        {
        //throw new NotImplementedException();
        Console.WriteLine("Hello, JOb executed");
        }
    }
} 

Sources

来源