我是Quartz.NET的新手,我对它有一些疑问,因为它似乎不像我想要的那样起作用。
问题1: --我在定义简单的IJobDetail和ITrigger。解决了
NameValueCollection config = ConfigurationManager.GetSection("quartz") as NameValueCollection;
ISchedulerFactory schedFact = new StdSchedulerFactory(config);
IScheduler scheduler = schedFact.GetScheduler();
try
{
scheduler.Start();
IJobDetail job = JobBuilder.Create<HelloJobTestScheduling>()
.WithIdentity("job1", "group1")
.Build();
DateTimeOffset endDate = DateTime.Now.AddMinutes(5);
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(10)
.WithRepeatCount(2))
.EndAt(endDate)
.Build();
scheduler.ScheduleJob(job, trigger);
}
catch(SchedulerException se)
{
Console.WriteLine(se);
}
finally
{
scheduler.Shutdown();
}
HelloJobTestScheduling
public void Execute(IJobExecutionContext context)
{
JobKey key = context.JobDetail.Key;
JobDataMap dataMap = context.JobDetail.JobDataMap;
string connectionString = @"Data Source=localhost\dejan;Initial Catalog=QuartzTest;Integrated Security=True";
string query = "INSERT INTO test (id, datetime) " +
"VALUES (@id, @datetime) ";
// create connection and command
using (SqlConnection cn = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(query, cn))
{
// define parameters and their values
cmd.Parameters.Add("@id", SqlDbType.Int).Value = "1";
cmd.Parameters.Add("@datetime", SqlDbType.DateTime).Value = DateTime.Now;
// open connection, execute INSERT, close connection
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}
App.config
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<quartz>
<add key="quartz.scheduler.instanceName" value="MyScheduler" />
<add key="quartz.scheduler.instanceId" value="AUTO" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
<add key="quartz.threadPool.threadCount" value="30"/>
<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=localhost\dejan;Initial Catalog=QuartzTest;Integrated Security=True" />
<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.SqlServerDelegate, Quartz" />
<add key="quartz.dataSource.default.provider" value="SqlServer-20" />
<add key="quartz.jobStore.useProperties" value="false" />
</quartz>
这个工作实际上所做的是:在数据库中只插入一行,然后它什么也不做。它等待.endAt,并在控制台中显示调度程序已关闭。我的密码怎么了?
Note__:我有调度程序在后台工作所需的所有数据库表。
问题2:为什么这个CRON不被识别?.WithCronSchedule("0 1 0 ? * ?")
Visual错误说明:'?' can only be specified for Day-of-Month -OR- Day-of-Week.
发布于 2015-01-11 16:25:27
Quartz.Net中的Cron表达式由7个子表达式组成
最后一个是可选的。您的cron表达式无效。如果你想每分钟跑一次去执行某件事情,正确的方法是:0 0/1 * 1/1 * ? *
我建议您使用这个工具来生成cron表达式。
这更容易。
发布于 2015-01-10 00:52:08
问题1,被解决。只是一个简短的说明,我的错误是我在finally中过早地关闭了调度器。如果我评论那条小巷,一切都会好起来的。
但是,关于问题2,,,我仍然需要帮助。
https://stackoverflow.com/questions/27865474
复制相似问题