gpt4 book ai didi

asp.net - 如何使用 Quartz.net 和 ASP.NET 来调度作业以触发邮件?

转载 作者:行者123 更新时间:2023-12-03 05:23:04 26 4
gpt4 key购买 nike

我不知道如何在 ASP.NET 中使用 Quartz.dll。我们应该在哪里编写调度作业以每天早上触发邮件的代码?

最佳答案

您有几个选项,具体取决于您想要执行的操作以及如何设置。例如,您可以将 Quartz.Net 服务器安装为独立的 Windows 服务,也可以将其嵌入到您的 asp.net 应用程序中。

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

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):

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();

一旦您引用了调度程序(无论是通过远程处理还是因为您有嵌入式实例),您就可以像这样安排作业:

// 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

关于asp.net - 如何使用 Quartz.net 和 ASP.NET 来调度作业以触发邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1356789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com