码迷,mamicode.com
首页 > Web开发 > 详细

Quartz.NET学习系列(十一)--- Quartz.NET持久化及客户端服务器模式

时间:2015-03-15 09:32:13      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:quartz.net   任务框架   任务调度   定时执行   计划   

持久化

        Quartz.NET如果不进行数据库相关配置,则默认的执行模式为内存模式,优点是执行速度快,确定就是数据无法存储,宕机了需要重新开始。

        持久化只需要做如下配置(以SQLServer为例)

            NameValueCollection properties = new NameValueCollection();
            //存储类型
            properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
            //表明前缀
            properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
            //驱动类型
            properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz";
            //数据源名称
            properties["quartz.jobStore.dataSource"] = "myDS";
            //连接字符串
            properties["quartz.dataSource.myDS.connectionString"] = @"Data Source=(local);Initial Catalog=quartz;User ID=sa;Password=123";
            //sqlserver版本
            properties["quartz.dataSource.myDS.provider"] = "SqlServer-20";


客户端服务器模式
        Quartz.NET支持TCP的通信,所以可以有这样的架构场景:用服务器承载持久化下来的所有任务并持续执行,想添加任务可通过客户端管理工具来向服务器数据库添加任务(当然是调用Quartz.NET框架里的方法),这样就可以产生了任务调度服务。

        服务器代码例子:

        public class RemoteServerExample 
	{
		public static void Run()
		{
			ILog log = LogManager.GetLogger(typeof(RemoteServerExample));
			
                        NameValueCollection properties = new NameValueCollection();
                        <span style="font-family: Arial, Helvetica, sans-serif;">properties["quartz.scheduler.instanceName"] = "RemoteServer";</span>

                        properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
                        properties["quartz.threadPool.threadCount"] = "5";
                        properties["quartz.threadPool.threadPriority"] = "Normal";

                        // 设置服务器
                        properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz";
                        properties["quartz.scheduler.exporter.port"] = "555";
                        properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler";
                        properties["quartz.scheduler.exporter.channelType"] = "tcp";
                        properties["quartz.scheduler.exporter.channelName"] = "httpQuartz";
                        // 拒绝远程
                        //properties["quartz.scheduler.exporter.rejectRemoteRequests"] = "true";

                        //存储类型
                        properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
                        //表明前缀
                        properties["quartz.jobStore.tablePrefix"] = "QRTZ_";
                        //驱动类型
                        properties["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz";
                        //数据源名称
                        properties["quartz.jobStore.dataSource"] = "myDS";
                        //连接字符串
                        properties["quartz.dataSource.myDS.connectionString"] = @"Data Source=(local);Initial Catalog=quartz;User ID=sa;Password=123";
                        //sqlserver版本
                        properties["quartz.dataSource.myDS.provider"] = "SqlServer-20";

                        ISchedulerFactory sf = new StdSchedulerFactory(properties);
                        IScheduler sched = sf.GetScheduler();
			sched.Start();
			
			log.Info("------- 服务器启动 -----------------");
			Thread.Sleep(TimeSpan.FromMinutes(5));
                        //sched.Shutdown(true);
                        log.Info("------- 服务器关闭 -----------------");
			
			SchedulerMetaData metaData = sched.GetMetaData();
			log.Info("执行次数 " + metaData.NumberOfJobsExecuted );
		}

	}
        客户端代码例子
    [PersistJobDataAfterExecution]
    [DisallowConcurrentExecution]
    public class SimpleJob : IJob
    {
        public const string Message = "msg";
        private static readonly ILog log = LogManager.GetLogger(typeof (SimpleJob));

        public virtual void Execute(IJobExecutionContext context)
        {
            JobKey jobKey = context.JobDetail.Key;
            string message = context.JobDetail.JobDataMap.GetString(Message);
            log.InfoFormat("{0} 执行时间 {1}", jobKey, DateTime.Now.ToString());
            log.InfoFormat("msg: {0}", message);
        }
    }

        
    public class RemoteClientExample
    {
        public static void Run()
        {
            ILog log = LogManager.GetLogger(typeof (RemoteClientExample));

            NameValueCollection properties = new NameValueCollection();
            properties["quartz.scheduler.instanceName"] = "RemoteClient";

            // 设置线程池
            properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
            properties["quartz.threadPool.threadCount"] = "5";
            properties["quartz.threadPool.threadPriority"] = "Normal";

            // 设置远程连接
            properties["quartz.scheduler.proxy"] = "true";
            properties["quartz.scheduler.proxy.address"] = "tcp://127.0.0.1:555/QuartzScheduler";

            ISchedulerFactory sf = new StdSchedulerFactory(properties);
            IScheduler sched = sf.GetScheduler();


            IJobDetail job = JobBuilder.Create<SimpleJob>()
                .WithIdentity("remotelyAddedJob", "default")
                .Build();

            JobDataMap map = job.JobDataMap;
            map.Put("msg", "信息");

            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("remotelyAddedTrigger", "default")
                .ForJob(job.Key)
                .WithCronSchedule("/5 * * ? * *")
                .Build();
            sched.ScheduleJob(job, trigger);

            log.Info("向服务器添加计划任务");
        }

        public string Name
        {
            get { return null; }
        }
    }

服务器如果是单机运行的话,一旦宕机就可能会出现业务问题,所以Quartz.NET提供了集群配置,这将在下一篇学习。

Quartz.NET学习系列(十一)--- Quartz.NET持久化及客户端服务器模式

标签:quartz.net   任务框架   任务调度   定时执行   计划   

原文地址:http://blog.csdn.net/eye_cng/article/details/44263477

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!