作业调度小软件

简介: 最近,由于工作需要,做了一个作业调度的小软件。先上两张效果图。 一、效果图 作业启动状态 作业停止状态 二、Quartz 调度的核心库用的是 Quartz.NET。

最近,由于工作需要,做了一个作业调度的小软件。先上两张效果图。

一、效果图

作业启动状

作业停止状态

二、Quartz

调度的核心库用的是 Quartz.NET。官方网站:http://quartznet.sourceforge.net/。这里有更多的中文介绍。

先创建一个类库,新建一个TaskJob类用来执行任务。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.Text;

using Quartz;

namespace MyJob
{
    public class TaskJob : IJob
    {
        public void Execute(JobExecutionContext context)
        {
            //todo:此处为执行的任务

             string path = AppDomain.CurrentDomain.BaseDirectory + "log\\Log.txt";
             Common.writeInLog("开始执行任务。", path);
             //。。。
             Common.writeInLog("执行已完成。", path);
        }
    }

}

正如你所见,一个继承了IJob的TaskJob,将要执行的任务统统放在Execute中即可!

三、作业调度
创建一个WinForm项目,并添加对Quartz.dll和类库的引用。

JobConfig.xml

新建一个JobConfig.xml文件,指定TaskJob并配置Job的触发时间。

可以指定具体的执行时间,如<cron-expression>0 50 9 ? * *</cron-expression> 表示每天9点50点触发。

也可以指定为一段时间内重复多少次,如 <cron-expression>0 0/1 8-20 ? * MON-FRI</cron-expression> 表示周一到周五每天的8点到20点,每一分钟触发一次。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?xml version="1.0" encoding="utf-8" ?>
<quartz xmlns="http://quartznet.sourceforge.net/JobSchedulingData"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 version="1.0"
                overwrite-existing-jobs="true">
    <job>
        <job-detail>
            <name>MyJob</name>
            <group>MyJob</group>
            <job-type>MyJob.TaskJob, MyJob</job-type>
        </job-detail>
        <trigger>
            <cron>
                <name>cronMyJob</name>
                <group>cronMyJob</group>
                <job-name>MyJob</job-name>
                <job-group>MyJob</job-group>
                <!--周一到周五每天的8点到20点,每一分钟触发一次-->
                <!--<cron-expression>0 0/1 8-20 ? * MON-FRI</cron-expression>-->
                <!--每天9点50点触发-->
                <cron-expression>0 50 9 ? * *</cron-expression>
            </cron>
        </trigger>
    </job>
</quartz>

HandleMask

创建一个类,用来控制Job的开启和停止。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;

using Quartz;
using Quartz.Impl;
using Quartz.Xml;

namespace WinApp
{
    public class HandleMask
    {
        IScheduler sched;
        public  void Start()
        {
            JobSchedulingDataProcessor processor;
            processor = new JobSchedulingDataProcessor(true, true);
            ISchedulerFactory sf = new StdSchedulerFactory();
            sched = sf.GetScheduler();
            string path = AppDomain.CurrentDomain.BaseDirectory + "JobConfig.xml";
            Stream s = new StreamReader(path, System.Text.Encoding.GetEncoding("UTF-8")).BaseStream;
            processor.ProcessStream(s, null);
            processor.ScheduleJobs(new Hashtable(), sched, false);
            sched.Start();
        }
        public void Stop()
        {  
            if (sched != null)
            {
                sched.Shutdown(true);
            }
        }
    }
}

start的时候会根据JobConfig.xml中的配置调度TaskJob中的Execute。

控制器

控制器的主要功能是启动和停止对作业的调度。

初始化

在下面的构造函数中用到了 mutex 。可见参考使用 Mutex实现会话状态下单实例运行和系统范围内单实例运行

同时,在窗体初始化时,就启动了作业。

batch = new HandleMask();
batch.Start();

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        HandleMask batch;
        bool closeTag = true;

        public Form1()
        {
            try
            {
                bool newMutexCreated = false;
                string mutexName = "Global\\" + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                Mutex mutex = null;
                try
                {
                    mutex = new Mutex(false, mutexName, out newMutexCreated);
                }
                catch (Exception ex)
                {
                    Environment.Exit(1);
                }
                if (newMutexCreated)
                {
                    InitializeComponent();
                    lblCurState.Text = "作业已启动,启动时间:" + DateTime.Now;
                    batch = new HandleMask();
                    batch.Start();
                }
                else
                {
                    Environment.Exit(1);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

作业启动 事件

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        private void tscbStart_Click(object sender, EventArgs e)
        {
            batch.Start();
            tscbStart.Enabled = false;
            tscbStop.Enabled = true;
            tscbExit.Enabled = true;
            lblCurState.Text = "作业已启动,启动时间:"+DateTime.Now;
        }

作业停止事件
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->private void tscbStop_Click(object sender, EventArgs e)
        {
            batch.Stop();
            tscbStart.Enabled = true;
            tscbStop.Enabled = false;
            tscbExit.Enabled = true;
            lblCurState.Text = "作业已暂停,停止时间:"+DateTime.Now;
        }

退出事件

 private void tscbExit_Click(object sender, EventArgs e)
        {
            batch.Stop();
            closeTag = false;
            Application.Exit();
        }

辅助功能
禁止最大化:MaximizeBox:false;
禁止调整窗体大小: FormBorderStyle:FixedSingle

点击关闭时最小化

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> private void Form1_Resize(object sender, EventArgs e)
        {
             if (this.WindowState == FormWindowState.Minimized)
             {
                 NormalToMinimized();
             }       
        }

        private void MinimizedToNormal()
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            notifyIcon1.Visible = false;

        }
        private void NormalToMinimized()
        {
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
            this.notifyIcon1.Visible = true;
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
             if (e.Button == MouseButtons.Left)
             {
                 this.MinimizedToNormal();
             }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (closeTag)
            {
               e.Cancel = true;
               NormalToMinimized();
           }
        }

四、源码下载
源码


原文地址:http://www.cnblogs.com/tenghoo/archive/2010/02/09/quartz.html

目录
相关文章
|
资源调度 分布式计算 安全
伏羲—阿里云分布式调度系统
在12月12日的云栖社区在线培训上,“飞天”分布式系统核心开发人员陶阳宇分享了《伏羲-阿里云分布式调度系统》。他主要从伏羲系统架构、任务调度、资源调度、容错机制、规模挑战、安全与性能隔离方面介绍了伏羲分布式系统架构和设计理念。
21576 0
|
4月前
|
中间件 测试技术 调度
设计一个简易版本的分布式任务调度系统
设计一个简易版本的分布式任务调度系统
91 0
|
9月前
|
存储 调度
开发团队调度系统
开发团队调度系统
|
11月前
|
存储 监控 并行计算
Slurm作业调度系统运行
Slurm作业调度系统运行
|
SQL 分布式计算 监控
开源分布式任务调度系统就选它!
开源分布式任务调度系统就选它!
|
分布式计算 前端开发 数据可视化
你只会用 xxl-job?一款更强大、新一代分布式任务调度框架来了,太强大了!
你只会用 xxl-job?一款更强大、新一代分布式任务调度框架来了,太强大了!
660 0
你只会用 xxl-job?一款更强大、新一代分布式任务调度框架来了,太强大了!
|
算法 IDE 调度
操作系统 作业调度实验报告
操作系统 作业调度实验报告
307 0
操作系统 作业调度实验报告
|
人工智能 自然语言处理 数据可视化
搭建批处理系统
搭建批处理系统
124 0
搭建批处理系统
|
存储 资源调度 分布式计算
大数据开发笔记(二):Yarn分布式集群操作系统
Apache Hadoop YARN 是 apache Software Foundation Hadoop的子项目,为分离Hadoop2.0资源管理和计算组件而引入。YARN的诞生缘于存储于HDFS的数据需要更多的交互模式,不单单是MapReduce模式。Hadoop2.0 的YARN 架构提供了更多的处理框架,不再强迫使用MapReduce框架。
89 0
大数据开发笔记(二):Yarn分布式集群操作系统
|
资源调度 大数据 调度
【大数据技术干货】阿里云伏羲(fuxi)调度器FuxiMaster功能简介(三) 针对在线服务的资源强稳定
转载自xingbao各位好,这是介绍阿里云伏羲(fuxi)调度器系列文章的第三篇,今天主要介绍针对在线服务的资源强稳定 一、FuxiMaster简介 FuxiMaster和Yarn非常相似,定位于分布式系统中资源管理与分配的角色:一个典型的资源分配流程图如下所示: 作为调度器,目前FuxiMas
4546 0