首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在ASP.NET中使用Quartz.net

如何在ASP.NET中使用Quartz.net
EN

Stack Overflow用户
提问于 2009-08-31 10:15:19
回答 2查看 43.3K关注 0票数 72

我不知道如何在ASP.NET中使用Quartz.dll。在哪里可以编写每天早上触发邮件的调度作业的代码?如果有人知道,请帮帮我……

编辑:我发现HOW TO use Quartz.NET in PRO way?非常有用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-09-02 17:27:18

您有几个选项,这取决于您想要做什么以及您想要如何设置它。例如,您可以将Quartz.Net服务器作为独立的windows服务安装,也可以将其嵌入到您的asp.net应用程序中。

如果你想嵌入式运行它,那么你可以从你的global.asax启动服务器,如下所示(来自源代码示例,示例#12):

代码语言:javascript
复制
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteServer";

// set thread pool info
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";

ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = sf.GetScheduler();
sched.Start();

如果您将其作为服务运行,您将像这样远程连接到它(来自示例#12):

代码语言:javascript
复制
NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteClient";

// set thread pool info
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";

// set remoting expoter
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.scheduler.proxy.address"] = "tcp://localhost:555/QuartzScheduler";
// First we must get a reference to a scheduler
ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = sf.GetScheduler();

一旦您有了对调度程序的引用(通过远程处理或因为您有一个嵌入式实例),您就可以像这样调度作业:

代码语言:javascript
复制
// define the job and ask it to run
JobDetail job = new JobDetail("remotelyAddedJob", "default", typeof(SimpleJob));
JobDataMap map = new JobDataMap();
map.Put("msg", "Your remotely added job has executed!");
job.JobDataMap = map;
CronTrigger trigger = new CronTrigger("remotelyAddedTrigger", "default", "remotelyAddedJob", "default", DateTime.UtcNow, null, "/5 * * ? * *");
// schedule the job
sched.ScheduleJob(job, trigger);

这是我为Quartz.Net入门人员写的一些帖子的链接:http://jvilalta.blogspot.com/2009/03/getting-started-with-quartznet-part-1.html

票数 77
EN

Stack Overflow用户

发布于 2013-04-15 16:46:29

几周前,我写了一篇关于在Windows Azure Worker角色中使用Quartz.Net来调度作业的文章。从那时起,我遇到了一个要求,促使我在Quartz.Net IScheduler周围创建一个包装器。JobSchedule负责从CloudConfigurationManager读取调度字符串并调度作业。

CloudConfigurationManager从角色的配置文件中读取设置,该文件可以通过云服务的配置部分下的Management管理门户进行编辑。

以下示例将安排一个作业,该作业需要在每天上午6点、8点、10点、12点30分和下午4点30点执行计划在角色设置中定义,可以通过Visual Studio进行编辑。若要访问角色设置,请转到您的Windows Azure云服务项目,并在角色文件夹下找到所需的角色配置。双击配置文件打开配置编辑器,然后导航到“Settings”选项卡。单击“Add setting”并将新设置命名为“JobDailySchedule”,并将其值设置为6:0;8:0;10:0;12:30;16:30;

代码语言:javascript
复制
The code from this Post is part of the Brisebois.WindowsAzure NuGet Package

To install Brisebois.WindowsAzure, run the following command in the Package Manager Console

PM> Install-Package Brisebois.WindowsAzure

Get more details about the Nuget Package.

然后使用JobSchedule使用在角色的配置文件中定义的计划来计划每天的作业。

代码语言:javascript
复制
var schedule = new JobSchedule();

schedule.ScheduleDailyJob("JobDailySchedule",
                            typeof(DailyJob));

DailyJob实现如下所示。由于这是一个演示,所以我不会向作业添加任何特定的逻辑。

代码语言:javascript
复制
public class DailyJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        //Do your daily work here
    }
}

JobSchedule包装Quartz.Net IScheduler。在上一篇文章中,我谈到了包装您的第三方工具的重要性,这是一个很好的例子,因为我包含了作业调度逻辑,并且我可以在不影响使用JobSchedule的代码的情况下潜在地更改此逻辑。

应该在角色启动时配置JobSchedule,并且应该在角色的整个生命周期中维护JobSchedule实例。更改计划可以通过在云服务的配置部分下的Management管理门户中更改“JobDailySchedule”设置来实现。然后,若要应用新计划,请通过云服务的实例部分下的Management管理门户重新启动角色实例。

代码语言:javascript
复制
public class JobSchedule
{
    private readonly IScheduler sched;

    public JobSchedule()
    {
        var schedFact = new StdSchedulerFactory();

        sched = schedFact.GetScheduler();
        sched.Start();
    }

    /// <summary>
    /// Will schedule jobs in Eastern Standard Time
    /// </summary>
    /// <param name="scheduleConfig">Setting Key from your CloudConfigurations, 
    ///                              value format "hh:mm;hh:mm;"</param>
    /// <param name="jobType">must inherit from IJob</param>
    public void ScheduleDailyJob(string scheduleConfig, 
                                 Type jobType)
    {
        ScheduleDailyJob(scheduleConfig, 
                         jobType, 
                         "Eastern Standard Time");
    }

    /// <param name="scheduleConfig">Setting Key from your CloudConfigurations, 
    ///                              value format "hh:mm;hh:mm;"</param>
    /// <param name="jobType">must inherit from IJob</param>
    public void ScheduleDailyJob(string scheduleConfig, 
                                 Type jobType, 
                                 string timeZoneId)
    {
        var schedule = CloudConfigurationManager.GetSetting(scheduleConfig);
        if (schedule == "-")
            return;

        schedule.Split(';')
                .Where(s => !string.IsNullOrWhiteSpace(s))
                .ToList()
                .ForEach(h =>
        {
            var index = h.IndexOf(':');
            var hour = h.Substring(0, index);
            var minutes = h.Substring(index + 1, h.Length - (index + 1));

            var job = new JobDetailImpl(jobType.Name + hour + minutes, null,
                                        jobType);

            var dh = Convert.ToInt32(hour, CultureInfo.InvariantCulture);
            var dhm = Convert.ToInt32(minutes, CultureInfo.InvariantCulture);
            var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);

            var cronScheduleBuilder = CronScheduleBuilder
                                            .DailyAtHourAndMinute(dh, dhm)
                                            .InTimeZone(tz);
            var trigger = TriggerBuilder.Create()
                                        .StartNow()
                                        .WithSchedule(cronScheduleBuilder)
                                        .Build();

            sched.ScheduleJob(job, trigger);
        });
    }

    /// <summary>
    /// Will schedule jobs in Eastern Standard Time
    /// </summary>
    /// <param name="scheduleConfig">Setting Key from your CloudConfigurations, 
    ///                              value format "hh:mm;hh:mm;"</param>
    /// <param name="jobType">must inherit from IJob</param>
    public void ScheduleWeeklyJob(string scheduleConfig, 
                                  Type jobType)
    {
        ScheduleWeeklyJob(scheduleConfig, 
                          jobType, 
                          "Eastern Standard Time");
    }


    /// <param name="scheduleConfig">Setting Key from your CloudConfigurations,
    ///                              value format "hh:mm;hh:mm;"</param>
    /// <param name="jobType">must inherit from IJob</param>
    public void ScheduleWeeklyJob(string scheduleConfig, 
                                  Type jobType, 
                                  string timeZoneId)
    {
        var schedule = CloudConfigurationManager.GetSetting(scheduleConfig);

        schedule.Split(';')
                .Where(s => !string.IsNullOrWhiteSpace(s))
                .ToList()
                .ForEach(h =>
        {
            var index = h.IndexOf(':');
            var hour = h.Substring(0, index);
            var minutes = h.Substring(index + 1, h.Length - (index + 1));

            var job = new JobDetailImpl(jobType.Name + hour + minutes, null,
                                        jobType);

            var dh = Convert.ToInt32(hour, CultureInfo.InvariantCulture);
            var dhm = Convert.ToInt32(minutes, CultureInfo.InvariantCulture);
            var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
            var builder = CronScheduleBuilder
                            .WeeklyOnDayAndHourAndMinute(DayOfWeek.Monday, 
                                                         dh, 
                                                         dhm)
                            .InTimeZone(tz);

            var trigger = TriggerBuilder.Create()
                                        .StartNow()
                                        .WithSchedule(builder)
                                        .Build();

            sched.ScheduleJob(job, trigger);
        });
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1356789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档