Asp.net 怎样使用任务调治

打印 上一主题 下一主题

主题 898|帖子 898|积分 2694

起首需要再 Global文件中,创建调治器,哎呀,直接上代码吧
  1. public class MvcApplication : System.Web.HttpApplication
  2. {
  3.     private static IScheduler _scheduler;
  4.     private ScheduleJobsManager  _jobMana;
  5.     protected void Application_Start()
  6.     {
  7.         
  8.         var factory = new StdSchedulerFactory();
  9.       
  10.         _scheduler = factory.GetScheduler();
  11.         _scheduler.Start();
  12.         _jobMana = new ScheduleJobsManager(_scheduler);
  13.       
  14.         RegisterJobs();
  15.       
  16.     }
  17. //在网站启动的时候,将数据库中正在运行状态的任务,启动起来
  18. private  void RegisterJobs()
  19. {
  20.      var list = _jobMana.GetList(s => s.JobStatus == "Running");
  21.      foreach (var job in list)
  22.      {
  23.          _jobMana.AddJob(job);
  24.      }
  25. }
  26. //这个方法,是在其他地方使用的,相当于 单例模式,全局只有这一个调度器
  27. public static IScheduler GetScheduler()
  28. {
  29.      return _scheduler;
  30. }
  31. }
复制代码
接下来说下,管理任务调治的控制器和界面
控制器这样写:
  1. private  IScheduler _scheduler = MvcApplication.GetScheduler();//这个就是Global中的那个静态方法
  2. private ScheduleJobsManager _scheduleJobsManager; //这个是对任务调度的一个封装,也是操作数据的操作类,后面会给代码
  3. private JobLogsManager _jobLog = new JobLogsManager();  //这个是与数据库对用的Job日志操作类
  4. //初始化控制器,需要给调度管理进行初始化
  5. public JobController()
  6. {
  7.      _scheduleJobsManager = new ScheduleJobsManager(_scheduler);
  8. }
  9. public ActionResult Add(ScheduleJobs job)
  10. {
  11.      job.JobStatus = "Running";
  12.      job.CreateTime = DateTime.Now;
  13.      _scheduleJobsManager.Insert(job);
  14.      _scheduleJobsManager.AddJob(job);
  15.      return Json(new { code = 0, msg = "添加成功" });
  16. }
  17. public ActionResult Pause(int id)
  18. {
  19.      try
  20.      {
  21.         if( _scheduleJobsManager.PauseJob(id))
  22.          {
  23.              return Json(new { code = 0, msg = "暂停成功" });
  24.          }
  25.          else
  26.          {
  27.              return Json(new { code = 500, msg = "暂停失败" });
  28.          }
  29.         
  30.      }
  31.      catch (Exception ex)
  32.      {
  33.          return Json(new { code = 500, msg = "暂停失败:" + ex.Message });
  34.      }
  35. }
  36. [HttpPost]
  37. public ActionResult Resume(int id)
  38. {
  39.      try
  40.      {
  41.          if(_scheduleJobsManager.ResumeJob(id))
  42.          {
  43.              return Json(new { code = 0, msg = "恢复成功" });
  44.          }
  45.          return Json(new { code = 500, msg = "恢复失败"  });
  46.      }
  47.      catch (Exception ex)
  48.      {
  49.          return Json(new { code = 500, msg = "恢复失败:" + ex.Message });
  50.      }
  51. }
  52. [HttpPost]
  53. public ActionResult Delete(int Id)
  54. {
  55.      var info = _scheduleJobsManager.GetById(Id);
  56.      if (_scheduleJobsManager.Delete(s => s.Id == Id))
  57.      {
  58.          _scheduleJobsManager.DeleteJob(info);
  59.          return Json(new { code = 0, msg = "删除成功" });
  60.      }
  61.      else
  62.      {
  63.          return Json(new { code = -1, msg = "删除失败" });
  64.      }
  65. }
  66. [HttpPost]
  67. public ActionResult Edit(ScheduleJobs job)
  68. {
  69.      var existingJob =_scheduleJobsManager.GetById(job.Id);
  70.      if (existingJob == null)
  71.          throw new Exception("任务不存在");
  72.      try
  73.      {
  74.          // 删除旧的任务
  75.           _scheduler.DeleteJob(new JobKey(existingJob.JobName, existingJob.JobGroup));
  76.          // 创建新的任务
  77.          _scheduleJobsManager.AddJob(job);
  78.          existingJob.JobName = job.JobName;
  79.          existingJob.JobGroup = job.JobGroup;
  80.          existingJob.JobClassName = job.JobClassName;
  81.          existingJob.AssemblyName = job.AssemblyName;
  82.          existingJob.CronExpression = job.CronExpression;
  83.          existingJob.Description = job.Description;
  84.          
  85.          _scheduleJobsManager.Update(existingJob);
  86.          return Json(new { code = 0, msg = "更新成功" });
  87.      }
  88.      catch (Exception ex)
  89.      {
  90.          return Json(new { code = 500, msg = "更新失败:" + ex.Message });
  91.      }
  92. }
复制代码



ScheduleJobsManager.cs
  1. public class ScheduleJobsManager : DbContext<ScheduleJobs>
  2. {
  3.     //当前类已经继承了 DbContext增、删、查、改的方法
  4.     private readonly IScheduler _scheduler ;
  5.     public ScheduleJobsManager(IScheduler scheduler) {
  6.         _scheduler = scheduler ;
  7.     }
  8.     public void RunJobOnce(int id)
  9.     {
  10.         var job = GetById(id);
  11.         if (job == null)
  12.             throw new Exception("任务不存在");
  13.         var jobKey = new JobKey(job.JobName, job.JobGroup);
  14.         _scheduler.TriggerJob(jobKey);
  15.     }
  16.     private IJobDetail CreateJobDetail(ScheduleJobs jobInfo)
  17.     {
  18.         // 获取任务类型
  19.         Type jobType;
  20.         if (string.IsNullOrEmpty(jobInfo.AssemblyName))
  21.         {
  22.             // 从当前程序集获取类型
  23.             jobType = Type.GetType(jobInfo.JobClassName);
  24.         }
  25.         else
  26.         {
  27.             // 从指定程序集加载类型
  28.             var assembly = Assembly.Load(jobInfo.AssemblyName);
  29.             jobType = assembly.GetType(jobInfo.JobClassName);
  30.         }
  31.         if (jobType == null)
  32.         {
  33.             throw new Exception($"找不到任务类型:{jobInfo.JobClassName}");
  34.         }
  35.         // 验证类型是否实现IJob接口
  36.         if (!typeof(IJob).IsAssignableFrom(jobType))
  37.         {
  38.             throw new Exception($"任务类型 {jobInfo.JobClassName} 必须实现IJob接口");
  39.         }
  40.         // 创建任务详情
  41.         return JobBuilder.Create(jobType)
  42.             .WithIdentity(jobInfo.JobName, jobInfo.JobGroup)
  43.             .WithDescription(jobInfo.Description)
  44.             .UsingJobData("jobId", jobInfo.Id)  // 传递任务ID
  45.             .Build();
  46.     }
  47.     public void AddJob(ScheduleJobs job)
  48.     {
  49.         try
  50.         {
  51.             // 创建任务
  52.             var jobDetail = CreateJobDetail(job);
  53.             var trigger = TriggerBuilder.Create()
  54.                 .WithIdentity($"{job.JobName}_trigger", job.JobGroup)
  55.                 .WithCronSchedule(job.CronExpression)
  56.                 .Build();
  57.             // 添加到调度器
  58.             _scheduler.ScheduleJob(jobDetail, trigger);
  59.         }
  60.         catch (Exception ex)
  61.         {
  62.             throw new Exception($"添加任务失败: {ex.Message}", ex);
  63.         }
  64.     }
  65.     public bool PauseJob(int id)
  66.     {
  67.         
  68.         var job = GetById(id);
  69.          _scheduler.PauseJob(new JobKey(job.JobName, job.JobGroup));
  70.         job.JobStatus = "Paused";
  71.         return Update(job);
  72.     }
  73.     public bool ResumeJob(int id)
  74.     {
  75.       
  76.         
  77.             var job = GetById(id);
  78.             _scheduler.ResumeJob(new JobKey(job.JobName, job.JobGroup));
  79.             job.JobStatus = "Running";
  80.             return Update(job);
  81.            
  82.          
  83.         
  84.     }
  85.     public void DeleteJob(ScheduleJobs job)
  86.     {
  87.         
  88.       
  89.              _scheduler.DeleteJob(new JobKey(job.JobName, job.JobGroup));
  90.            
  91.         
  92.     }
  93.   
  94. }
复制代码
View界面就是简单的添加修改删除,用的layui框架做的,这个就做代码了,本身用本身的框架做吧

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表