user2284306
user2284306

Reputation:

Quartz.NET scheduling

I am new to this Quartz.NET thing and I have some questions about it cause it don't seem to work as I want to.

Question 1: I am defining simple IJobDetail and ITrigger. [SOLVED]

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>

What this job actually does in reality is this: inserting only one row in the database, and after that it won't do anything. It waits to the .endAt and shows in console that the scheduler is closed. What is wrong with my code?

Note: I have all the required database tables for the scheduler to work in the background.

Question 2: Why this CRON is not recognized? .WithCronSchedule("0 1 0 ? * ?")

Visual Studio error says: '?' can only be specified for Day-of-Month -OR- Day-of-Week.

Upvotes: 0

Views: 629

Answers (2)

LeftyX
LeftyX

Reputation: 35597

Cron expressions in Quartz.Net are made up of 7 sub-expressions:

  1. Seconds
  2. Minutes
  3. Hours
  4. Day-of-Month
  5. Month
  6. Day-of-Week
  7. Year (optional field)

The last one is optional. Your cron expression is not valid. If you want to run to execute something every minute the right one is: 0 0/1 * 1/1 * ? *

I would advice you to use this tool to generate your cron expressions.
It's easier.

Upvotes: 1

user2284306
user2284306

Reputation:

Question 1 is solved. Just a short note, my mistake was that I close the scheduler too early in finally. If I comment that lane, everything works good.

But, I still need help about Question 2.

Upvotes: 0

Related Questions