Closed. This question needs to be more focused. It is not currently accepting answers.
想要改善这个问题吗?通过编辑这篇文章更新问题,以便它仅关注于一个问题。
Closed 7 years ago.
什么是Quartz.net?如何开始使用它?
建议您从Quartz.Net的教程开始学习,网址为http://quartznet.sourceforge.net/tutorial/index.html。
您可以查看源文件夹中的Quartz.Examples的示例。
You need to first think of a few points, if will want to use the Quartz in memory or stored in a database,it will start a server, etc. After that u must create a class to manage your Schedule and another containing the Job you want to run, what I m saying is a simple example. I will demonstrate how to do this in the following scenario: Sqlite to store data, a Job class with log and everything else with Log. And will be run every day at 10:30 AM.
您的web.config
<add key="quartz.scheduler.instanceName" value="ServerScheduler" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="10" />
<add key="quartz.threadPool.threadPriority" value="2" />
<add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" />
<add key="quartz.jobStore.dataSource" value="default" />
<add key="quartz.dataSource.default.connectionString" value="Data Source=yourdb.db;Version=3;Foreign Keys=ON;" />
<add key="quartz.jobStore.tablePrefix" value="QRTZ_" />
<add key="quartz.jobStore.clustered" value="false" />
<add key="quartz.jobStore.lockHandler.type" value="Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz" />
<add key="quartz.jobStore.driverDelegateType" value="Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz" />
<add key="quartz.dataSource.default.provider" value="SQLite-1094" />
<add key="quartz.jobStore.useProperties" value="true" />
<add key="quartz.scheduler.exporter.type" value="Quartz.Simpl.RemotingSchedulerExporter, Quartz" />
<add key="quartz.scheduler.exporter.port" value="555" />
<add key="quartz.scheduler.exporter.bindName" value="QuartzScheduler" />
<add key="quartz.scheduler.exporter.channelType" value="tcp" />
创建一个名为Schedule.cs的类:
public static class Schedule
{
private static readonly ILog logger = LogManager.GetLogger(typeof(Schedule));
public static ITrigger BuildTrigger(int hour, int minute)
{
var trigger = TriggerBuilder.Create()
.StartNow()
.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(hour,minute))
.Build();
return trigger;
}
public static IScheduler InicializarScheduler()
{
var scheduleMigracao = Schedule.GetScheduler();
if (!scheduleMigracao.IsStarted)
scheduleMigracao.Start();
logger.Info("Scheduler started successfully");
return scheduleMigracao;
}
public static IJobDetail BuildJogDetail<T>(string WorkFlowId, string Group) where T : IJob
{
return JobBuilder.Create<T>()
.WithIdentity(WorkFlowId, Group)
.RequestRecovery()
.Build();
}
private static IScheduler GetScheduler()
{
try
{
IScheduler scheduler = SchedulerRepository.Instance.Lookup("DefaultQuartzScheduler");
if (scheduler == null)
{
var factoryScheduler = new StdSchedulerFactory();
return factoryScheduler.GetScheduler();
}
else
{
return scheduler;
}
}
catch (Exception ex)
{
logger.Error("Server initialization failed:" + ex.Message, ex);
throw;
}
}
}
创建一个名为 TestJob.cs 的类。
public class TestJob : IInterruptableJob
{
private static readonly ILog Log = LogManager.GetLogger(typeof(TestJob));
public void Interrupt()
{
}
public void Execute(IJobExecutionContext context)
{
try
{
Log.DebugFormat("{0}****{0}Job {1} Start @ {2} next scheduled for {3}{0}***{0}",
Environment.NewLine,
context.JobDetail.Key,
context.FireTimeUtc.Value.ToString("r"),
context.NextFireTimeUtc.Value.ToString("r"));
//implement your logic
Log.DebugFormat("{0}****{0}Job {1} executed with sucess at @ {2}",
Environment.NewLine,
context.JobDetail.Key,
context.FireTimeUtc.Value.ToString("r"));
}
catch (Exception ex)
{
Log.DebugFormat("{0}***{0}Failed: {1}{0}***{0}", Environment.NewLine, ex.Message);
}
}
}
在您的Global.asax文件中,放置:
public class Global : System.Web.HttpApplication
{
private static void BuildingQuartzMetadataSqLite1090()
{
var metaData = new DbMetadata();
metaData.AssemblyName = "System.Data.SQLite, Version = 1.0.94.0,Culture=neutral, PublicKeyToken=db937bc2d44ff139";
metaData.BindByName = true;
metaData.CommandBuilderType = typeof(System.Data.SQLite.SQLiteCommandBuilder);
metaData.CommandType = typeof(System.Data.SQLite.SQLiteCommand);
metaData.ConnectionType = typeof(System.Data.SQLite.SQLiteConnection);
metaData.ExceptionType = typeof(System.Data.SQLite.SQLiteException);
metaData.ParameterDbType = typeof(System.Data.SQLite.TypeAffinity);
metaData.ParameterDbTypePropertyName = "DbType";
metaData.ParameterNamePrefix = "@";
metaData.ParameterType = typeof(System.Data.SQLite.SQLiteParameter);
metaData.UseParameterNamePrefixInParameterCollection = true;
DbProvider.RegisterDbMetadata("SQLite-1094",metaData);
}
protected void Application_Start(object sender, EventArgs e)
{
BuildingQuartzMetadataSqLite1090();
Schedule.InicializarScheduler();
Schedule.ScheduleJob(Schedule.BuildJogDetail<TestJob>("TestJob","GrupoTest"), Schedule.BuildTrigger(10, 30));
}
}
任何问题请提出。