Quartz.Net Client Connect/Disconnect from Remote server.

最近做项目,用到例行性事物处理,就想到用Quartz.Net,开源的作业调度框架,我用的是 Quartz.dll 2.0.0.400,Runtime Version:v4.0.30379.

 

在使用过程中需要对Scheduler上的Job进行管理,其中包括断开与远程服务端的连接,在网上找也没找到这方面的分享,最后看了下源码,解决方案如下:

 

1. RemotingSchedulerProxyFactory

远程调度代理工厂,用来生产远程调度实体(RemoteScheduler)

Code:

var Address = string.Format("tcp://{0}:{1}/{2}", server, port, schedulerName);
RemotingSchedulerProxyFactory schedulerFactory = new RemotingSchedulerProxyFactory { Address = Address };

 

2.RemoteScheduler

远程调度实体.

Code:

IScheduler scheduler = new RemoteScheduler("RNNService", schedulerFactory);

 

3,IRemotableQuartzScheduler

可远程调度接口,绑定远程调度时用。

Code:

IRemotableQuartzScheduler remotableQuartzscheduler = schedulerFactory.GetProxy();

 

4.RemotingSchedulerExporter

远程调度导出,用来绑定和断开远程调度。

Code:

RemotingSchedulerExporter remotingSchedulerExporter = new RemotingSchedulerExporter();

// 绑定远程调度

remotingSchedulerExporter.Bind(remotableQuartzscheduler);

//断开远程调度,调这个时,必须调过绑定

remotingSchedulerExporter.UnBind(remotableQuartzscheduler);

 

完整代码如下:

 

    using System;
    using System.Collections.Specialized;

    using Quartz;
    using Quartz.Impl;
    using Quartz.Simpl;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class QuartzSchedulerUtility
    {
        private static RemotingSchedulerProxyFactory schedulerFactory;

        private static RemotingSchedulerExporter remotingSchedulerExporter;

        private static IRemotableQuartzScheduler remotableQuartzscheduler;

        private static IScheduler scheduler;

        public static string Address { get; private set; }

        public static void ConnectScheduler(string server, int port, string schedulerName)
        {
            Address = string.Format("tcp://{0}:{1}/{2}", server, port, schedulerName);
            schedulerFactory = new RemotingSchedulerProxyFactory { Address = Address };
            remotingSchedulerExporter = new RemotingSchedulerExporter();

            try
            {
                scheduler = new RemoteScheduler("RNNService", schedulerFactory);
                if (scheduler.IsStarted)
                {
                    remotableQuartzscheduler = schedulerFactory.GetProxy();
                    remotingSchedulerExporter.Bind(remotableQuartzscheduler);
                }
            }
            catch (Exception)
            {
                scheduler = null;
                string.Format("Unable to connect to the specified server {0}", Address).WriteLog(Log4NetType.Error);
            }
        }

 

        public static IScheduler GetScheduler()
       {
            return scheduler;
       }

        public static void DisconnectScheduler()
        {
            if (scheduler == null || remotingSchedulerExporter == null || remotableQuartzscheduler == null)
            {
                return;
            }

            remotingSchedulerExporter.UnBind(remotableQuartzscheduler);
            scheduler = null;
        }

    }

 

就到这,欢迎宝贵意见!~

 

你可能感兴趣的:(框架,Quartz.Net)