Quartz.NET调度

时间:2015-01-09 16:58:38

标签: quartz-scheduler quartz.net

我是Quartz.NET的新手,我对它有一些疑问,因为它似乎无法正常工作。

问题1:我正在定义简单的IJobDetailITrigger。的 [解决]



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();
            }
&#13;
&#13;
&#13;

HelloJobTestScheduling

&#13;
&#13;
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();
            }
            
        }
&#13;
&#13;
&#13;

的App.config

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

这项工作实际上实际上做的是:在数据库中只插入一行,之后它就不会做任何事情。它等待.endAt并在控制台中显示调度程序已关闭。我的代码出了什么问题?

注意 :我拥有调度程序在后台运行所需的所有数据库表。

问题2:为什么无法识别此CRON? .WithCronSchedule("0 1 0 ? * ?")

Visual Studio错误说: '?' can only be specified for Day-of-Month -OR- Day-of-Week.

2 个答案:

答案 0 :(得分:1)

Quartz.Net中的Cron表达式由7 sub-expressions

组成
  1. 分钟
  2. 小时
  3. 日的日
  4. 某一天的周
  5. (可选字段)
  6. 最后一个是可选的。 您的 cron 表达式无效。 如果你想每分钟运行一次,那么正确的是:0 0/1 * 1/1 * ? *

    我建议你使用这个tool来生成你的cron表达式 这很容易。

答案 1 :(得分:0)

问题1 已解决。只是一个简短的说明,我的错误是我在finally过早地关闭了调度程序。如果我评论那条车道,一切都很好。

但是,我仍然需要有关问题2的帮助

相关问题