CRM Development

Solutionist has designed the architecture for a new CRM system for use in the medical imaging industry.

Tuesday, 10 August 2010

Quartz.NET

  I haven't got round to trying Spring.NET yet, but had a play with another Java-to-.NET port: Quartz.NET.

I'm sure that there are other posts/tutorials about how to get it working, but I'll add in my twopennyworth as a step-by-step account of what I did.

1. Downloaded latest version of Quartz.NET - 1.0.2
2. Using VS2010, I created a Web Project and added references to the Quartz and Common.Logging dll's that are in the bin directory
3. For good measure I also added a reference to the log4net dll - I would be using this in the Quartz job.
4. For log4net config, I kept this separate from the application and placed a log4net.config file in project. I then referenced this in Global.asax as follows:

log4net.Config.DOMConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "log4net.config"));

My log4net config file looked like:





<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <root>
    <level value="DEBUG" />
    <appender-ref ref="RollingFileAppender"/>
  </root>

  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="logs\Logfile.log" />
    <appendToFile value="true" />
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <rollingStyle value="Composite"/>
    <datePattern value="yyyyMMdd"/>
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="1000KB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" />
    </layout>
  </appender>

</log4net>



5. Ran the database script (for SQL server) to create the relevant tables.

6. Updated the Web. Config file as follows:




  <configSections>

    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>







  <quartz>
    <add key="quartz.scheduler.instanceName" value="CommerceScheduler" />
    <!-- Configure Thread Pool -->
    <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
    <add key="quartz.threadPool.threadCount" value="10" />
    <add key="quartz.threadPool.threadPriority" value="Normal" />
    <!-- Configure Job Store -->
    <add key="quartz.jobStore.misfireThreshold" value="60000" />
    <!--<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />-->
    <add key="quartz.jobStore.type" value="Quartz.Impl.AdoJobStore.JobStoreTX, Quartz" />
    <add key="quartz.jobStore.dataSource" value="default" />
    <add key="quartz.jobStore.tablePrefix" value="QRTZ_" />
    <add key="quartz.jobStore.clustered" value="true" />
    <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.connectionString" value="Data Source=Server_Name;Initial Catalog=quartz;Integrated Security=True" />
    <add key="quartz.dataSource.default.provider" value="SqlServer-20" />
    <add key="quartz.jobStore.useProperties" value="true" />
         </quartz>



7. Created a test job:


public class QuartzJob1 : IJob
{
  protected static log4net.ILog log =      log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

  void IJob.Execute(JobExecutionContext context)
  {
    log.Debug("QuartzJob1 run");
  }
}

8. For testing purposes created a class with the following static method.

public static void InitQuartz()
{
    // construct a scheduler factory
    ISchedulerFactory schedFact = new StdSchedulerFactory();

    // get a scheduler
    IScheduler sched = schedFact.GetScheduler();
    sched.Start();

    // construct job info
    JobDetail jobDetail = new JobDetail("myJob", null, typeof(QuartzJob1));
    // fire every hour
    Trigger trigger = TriggerUtils.MakeMinutelyTrigger();
    // start on the next even hour
    trigger.StartTimeUtc = TriggerUtils.GetNextGivenMinuteDate(DateTime.UtcNow, 1);
    trigger.Name = "myTrigger";
    sched.ScheduleJob(jobDetail, trigger);
}

9. Global.asax added a call to InitQuartz()

10. Ran the application.

You should see a log message being written out every minute.
And you should see entries in the
QRTZ_SIMPLE_TRIGGERS and QRTZ_FIRED_TRIGGERS tables.




No comments:

Post a Comment