zoukankan      html  css  js  c++  java
  • quartz.net 执行后台任务

    ...

    https://www.cnblogs.com/zhangweizhong/category/771057.html

    https://www.cnblogs.com/lanxiaoke/category/973331.html

    宿主在控制台程序中

    using System;
    using System.Collections.Specialized;
    using System.IO;
    using System.Threading.Tasks;
    using Quartz;
    using Quartz.Impl;
    using Ace;
    using Microsoft.Extensions.Configuration;
    using Ace.Application.CS;

    namespace CS.QuartzJob
    {
    public class Program
    {
    private static void Main(string[] args)
    {
    var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("config.json", true, true)
    .Build();
    Globals.Configuration = configuration;

    RunProgram().GetAwaiter().GetResult();

    Console.WriteLine("Press any key to close the application");
    Console.Read();
    }

    private static async Task RunProgram()
    {
    try
    {
    // Grab the Scheduler instance from the Factory
    NameValueCollection props = new NameValueCollection
    {
    { "quartz.serializer.type", "binary" }
    };
    StdSchedulerFactory factory = new StdSchedulerFactory(props);
    IScheduler scheduler = await factory.GetScheduler();
    await scheduler.Start();

    var cron1 = Globals.Configuration["JobCron:Job1"];
    // 项目完成状态
    IJobDetail job = JobBuilder.Create<ProjectDoneJob>()
    .WithIdentity("job1", "group1")
    .Build();
    ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create()
    .WithIdentity("trigger1")
    .WithCronSchedule(cron1)//cron触发器
    .ForJob("job1", "group1")
    .Build();
    await scheduler.ScheduleJob(job, trigger);

    }
    catch (SchedulerException se)
    {
    await Console.Error.WriteLineAsync(se.ToString());
    }
    }
    }

    public class ProjectDoneJob : IJob
    {
    public async Task Execute(IJobExecutionContext context)
    {

    await Console.Out.WriteLineAsync( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    }
    }
    }

    config.json,设置始终复制

    {

    "JobCron": {
    "Job1": "0 0/1 * * * ?" //1分钟
    }

    }

  • 相关阅读:
    PS和AI软件差别
    JS实现转动随机数抽奖的特效代码
    项目---考评系统排课算法分析
    UVA 193 Graph Coloring 图染色 DFS 数据
    中英文对照 —— 法律
    中英文对照 —— 法律
    中英文对照 —— 电影与话剧、歌剧
    中英文对照 —— 电影与话剧、歌剧
    matlab 正态分布相关 API
    matlab 正态分布相关 API
  • 原文地址:https://www.cnblogs.com/langhaoabcd/p/10446229.html
Copyright © 2011-2022 走看看