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;
        }

    }

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值