The core component of my heart (pluggable AOP) ~ scheduling component quartz.net

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Back to the directory

Quartz.net is a scheduling component, it can flexibly set your debugging mode, by time, by date, by the cycle can be easily achieved, quartz can be used not only in web, but also can be deployed in WinForm, winservice, let's make a simple debugging services, we taking WEB as an example, the WEB end of the popular MVC3 implementation.

We need to introduce the three components of DLL

C5.DLL,log4net.DLL,Quartz.DLL,Common.Logging

For WEB projects, scheduling after injection we can put it in the global.asax, it can make our scheduling task start automatically when the application starts, automatically at the end of the application, this is the end, we need to see.

Global.asax content:

 IScheduler sched;
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            // By default on Entity Framework using LocalDB
            Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            log4net.Config.XmlConfigurator.Configure();
            string cronExpr = ConfigurationManager.AppSettings["cronExpr"];
            ISchedulerFactory sf = new Quartz.Impl.StdSchedulerFactory();
            sched = sf.GetScheduler();
            IJobDetail job = JobBuilder.Create<Callback>()
                .WithIdentity("job1", "group1")
                .Build();

            ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .WithCronSchedule(cronExpr)
                .Build();

            sched.ScheduleJob(job, trigger);
            sched.Start();
        }


        protected void Application_End(object sender, EventArgs e)
        {
            //   Applications running in the closure of the code
            if (sched != null)
            {
                sched.Shutdown(true);
            }
        }

Look at our scheduling code, the callback method that debugging, it must implement the IJob interface, this interface has a method of Execute, we put the thing to do here, and it has a parameter IJobExecutionContext, which passed is the current task (job) object.

   public class Callback : IJob
    {
        private readonly ILog logger = LogManager.GetLogger(typeof(Callback));

        #region IJob members

        public void Execute(IJobExecutionContext context)
        {
            logger.Info(context.JobDetail.Key.Name + DateTime.Now);
        }

        #endregion
    }

Below we look at the relevant information in the configuration file, which is composed of quartz nodes and log4 log configuration node.

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <quartz>
    <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
    <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.misfireThreshold" value="60000"/>
    <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
  </quartz>
  <log4net>
    <appender name="InfoFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="./log/" />
      <appendToFile value="true" />
      <param name="DatePattern" value="yyyyMMdd&quot;.txt&quot;" />
      <rollingStyle value="Date" />
      <maxSizeRollBackups value="100" />
      <maximumFileSize value="1024KB" />
      <staticLogFileName value="false" />
      <Encoding value="UTF-8" />
      <filter type="log4net.Filter.LevelRangeFilter">
        <param name="LevelMin" value="INFO" />
        <param name="LevelMax" value="INFO" />
      </filter>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %logger  - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="INFO" />
      <appender-ref ref="InfoFileAppender" />
    </root>
  </log4net>
  <appSettings>
    <add key="cronExpr" value="0/100 * * * * ?"/>
    <!- 100 seconds to perform a -->
</appSettings>

Well, now we can run WEB programs, then the root directory of your log directory should have our scheduling log, ha ha.

Back to the directory

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Rod at November 16, 2013 - 11:17 PM