zoukankan      html  css  js  c++  java
  • Hangfire-执行定时任务框架

    Hangfire-执行定时任务框架

    1、新建netframwork console 控制台项目ConsoleHangfireTest,nuget程序包 添加Hangfire

    2、项目ConsoleHangfireTest右键,添加OWIN Startup 类

     代码内容:

    using System;
    using System.Threading.Tasks;
    using System.Web.Http;
    using Hangfire;
    using Microsoft.Owin;
    using Owin;
    
    [assembly: OwinStartup(typeof(ConsoleHangfireTest.Startup1))]
    
    namespace ConsoleHangfireTest
    {
        public class Startup1
        {
            public void Configuration(IAppBuilder app)
            {
    
                //HttpConfiguration configuration = new HttpConfiguration();
                //configuration.Routes.MapHttpRoute(
                //    name: "default",
                //    routeTemplate: "api/{controller}/{action}/{id}",
                //    defaults: new { id = RouteParameter.Optional }
                //    );
                //app.UseWebApi(configuration);
    
                // 有关如何配置应用程序的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkID=316888
                //定时任务的持久化使用Sqlserver进行,当然也可以使用mongodb,redis等其他的
                GlobalConfiguration.Configuration.UseSqlServerStorage("Data Source=MOP6EXV9E5J1M1F;Initial Catalog=mydb;Integrated Security=True;");
                //启用Hangfire服务
                app.UseHangfireServer();
                //启用Hangfire Dashboard面板
                app.UseHangfireDashboard();
            }
        }
    }

    3、项目ConsoleHangfireTest右键,添加TestHangfire

    代码如下:

    using Hangfire;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleHangfireTest
    {
        public static class TestHangfire
        {
            [Obsolete]
            public static void Start()
            {
                //支持基于队列的任务处理:任务执行不是同步的,而是放到一个持久化队列中,以便马上把请求控制权返回给调用者。
                var jobId = BackgroundJob.Enqueue(() => WriteLog("------Enqueue 队列任务"));
    
                //延迟任务执行:不是马上调用方法,而是设定一个未来时间点再来执行。
                BackgroundJob.Schedule(() => WriteLog("------Schedule 延时任务"), TimeSpan.FromSeconds(10));
    
                //循环任务执行:一行代码添加重复执行的任务,其内置了常见的时间循环模式,也可基于CRON表达式来设定复杂的模式。
                RecurringJob.AddOrUpdate(() => WriteLog("------AddOrUpdate 每分钟执行任务"), Cron.Minutely); //注意最小单位是分钟
    
                //延续性任务执行:类似于.NET中的Task,可以在第一个任务执行完之后紧接着再次执行另外的任务
                BackgroundJob.ContinueWith(jobId, () => WriteLog("------ContinueWith 连续任务"));
    
            }
            public static void WriteLog(string msg)
            {
                Trace.WriteLine($"Hangfire在 {DateTime.Now} 执行了任务:[{msg}]");
            }
        }
    }

    4、项目ConsoleHangfireTest的Program.cs内容如下:

    using Hangfire;
    using Microsoft.Owin.Hosting;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleHangfireTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("启动服务。。。");
                WebApp.Start<Startup1>("http://localhost:8090/");//注意之后的斜杠不要忘记了
    
                //string baseAddress = "http://127.0.0.1:8090/";
                //WebApp.Start<Startup>(url: baseAddress);
    
                Console.WriteLine("服务启动成功。。。");
    
                //TestHangfire.Start();
                Start();
                Console.ReadLine();
            }
            [Obsolete]
            public static void Start()
            {
                //支持基于队列的任务处理:任务执行不是同步的,而是放到一个持久化队列中,以便马上把请求控制权返回给调用者。
                var jobId = BackgroundJob.Enqueue(() => WriteLog("Enqueue 队列任务"));
    
                //延迟任务执行:不是马上调用方法,而是设定一个未来时间点再来执行。
                BackgroundJob.Schedule(() => WriteLog("Schedule 延时任务"), TimeSpan.FromSeconds(10));
    
                //循环任务执行:一行代码添加重复执行的任务,其内置了常见的时间循环模式,也可基于CRON表达式来设定复杂的模式。
                RecurringJob.AddOrUpdate(() => WriteLog("AddOrUpdate 每分钟执行任务"), Cron.Minutely); //注意最小单位是分钟
    
                //延续性任务执行:类似于.NET中的Task,可以在第一个任务执行完之后紧接着再次执行另外的任务
                BackgroundJob.ContinueWith(jobId, () => WriteLog("ContinueWith 连续任务"));
    
            }
            public static void WriteLog(string msg)
            {
                Debug.WriteLine($"Hangfire在 {DateTime.Now} 执行了任务:[{msg}]");
            }
        }
    }

    5、运行如下:

    浏览器地址栏输入:http://localhost:8090/hangfire/

    Hangfire的仪表盘显示

    数据库显示:

     

    注意:

        如果运行时报错误,“ System.MissingMemberException:找不到服务器工厂”

    System.MissingMemberException: The server factory could not be located for the given input: Microsoft.Owin.Host.HttpListener

    其实是缺少Microsoft.Owin.Host.HttpListener,使用NuGet添加一下即可
  • 相关阅读:
    Win7停止更新升Win10教程
    linux 进程管理
    linux vi/Vim编辑器
    linux 文件管理
    linux 目录管理
    [开发笔记]-C#判断文件类型
    [开发笔记]-C#获取pdf文档的页数
    [转载]每周问问你的团队这10个问题
    [转载]番茄时间管理法(Pomodoro Technique):一个番茄是如何让你工作更有效率的
    [开发笔记]-Linq to xml学习笔记
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/14046409.html
Copyright © 2011-2022 走看看